index.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. import type { App } from 'vue'
  3. import { createPinia, type Pinia } from 'pinia'
  4. import type { UsedStore } from '@shared/types/store'
  5. declare module 'pinia' {
  6. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  7. export interface DefineStoreOptionsBase<S, Store> {
  8. requiresAuth?: boolean
  9. }
  10. }
  11. const usedStores = new Set<UsedStore>()
  12. const pinia: Pinia = createPinia()
  13. // Remember all stores, for example to cleanup the private stores after logout.
  14. pinia.use((context) => {
  15. usedStores.add({
  16. store: context.store,
  17. requiresAuth: context.options.requiresAuth ?? true,
  18. })
  19. })
  20. export default function initializeStore(app: App) {
  21. app.use(pinia)
  22. }
  23. export const resetAndDisposeStores = (requiresAuth?: boolean) => {
  24. usedStores.forEach((usedStore) => {
  25. if (requiresAuth !== undefined && usedStore.requiresAuth !== requiresAuth) {
  26. return
  27. }
  28. usedStore.store.$dispose()
  29. delete pinia.state.value[usedStore.store.$id]
  30. usedStores.delete(usedStore)
  31. })
  32. }
  33. export { pinia }