index.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { createPinia, type Pinia } from 'pinia'
  3. import type { UsedStore } from '#shared/types/store.ts'
  4. import type { App } from 'vue'
  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 }