All files / src/composables useFlowScopedReset.ts

94.11% Statements 16/17
50% Branches 1/2
100% Functions 3/3
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43                                        8x 8x 8x 8x 8x 8x 8x   8x 10x   2x 2x 2x 2x 2x 2x 2x          
import { watch } from 'vue'
import { useAuthStore } from '@/stores/auth'
import { useEntitiesStore } from '@/stores/entities'
import { useItemsStore } from '@/stores/items'
import { useRolesStore } from '@/stores/roles'
import { useFlowsStore } from '@/stores/flows'
import { useApiKeysStore } from '@/stores/apiKeys'
import { useFeedStore } from '@/stores/feed'
 
/**
 * Clears every store holding data of the active flow as soon as that flow
 * changes. Without it, a flow whose role does not grant a `*:list` permission
 * never refetches, so its pages and sidebar badges keep showing the previous
 * flow's entities, roles or members.
 *
 * `flush: 'sync'` runs the reset the instant `currentFlow` is assigned — before
 * the sidebar's own watcher refetches for the new flow, and before the next
 * render — so no frame ever shows the old flow's data.
 */
export function useFlowScopedReset() {
  const authStore = useAuthStore()
  const entitiesStore = useEntitiesStore()
  const itemsStore = useItemsStore()
  const rolesStore = useRolesStore()
  const flowsStore = useFlowsStore()
  const apiKeysStore = useApiKeysStore()
  const feedStore = useFeedStore()
 
  watch(
    () => authStore.currentFlow,
    (flowId, previousFlowId) => {
      Iif (flowId === previousFlowId) return
      entitiesStore.reset()
      itemsStore.reset()
      rolesStore.resetRoles()
      flowsStore.resetMembers()
      apiKeysStore.reset()
      feedStore.reset()
    },
    { flush: 'sync' }
  )
}