LCOV - code coverage report
Current view: top level - src/stores - rerouteStore.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 80.2 % 86 69
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { defineStore } from 'pinia'
       2              : import { computed, reactive, toRaw } from 'vue'
       3              : import type { ComputedRef } from 'vue'
       4              : 
       5              : import { useLinkStore } from '@/stores/linkStore'
       6              : import type {
       7              :   GraphScope,
       8              :   OwningGraphId,
       9              :   RootGraphId
      10              : } from '@comfyorg/graph-model/graphScopeId'
      11              : import type { LinkId } from '@comfyorg/graph-model/linkId'
      12              : import { isFloatingTopology } from '@comfyorg/graph-model/linkTopology'
      13              : import type { RerouteChain } from '@comfyorg/graph-model/rerouteChain'
      14              : import type { RerouteId } from '@comfyorg/graph-model/rerouteId'
      15              : 
      16              : /** The links whose chains pass through a reroute, split by link liveness. */
      17              : export interface RerouteMembership {
      18              :   linkIds: ReadonlySet<LinkId>
      19              :   floatingLinkIds: ReadonlySet<LinkId>
      20              : }
      21              : 
      22         2356 : export const EMPTY_MEMBERSHIP: Readonly<RerouteMembership> = {
      23              :   linkIds: new Set(),
      24              :   floatingLinkIds: new Set()
      25              : } as const
      26              : 
      27              : /**
      28              :  * Reroute chain store, holding each reroute's chain state (parent pointer
      29              :  * and floating slot marker) in root-and-owner-scoped buckets keyed by
      30              :  * `RerouteId`. Link membership is not stored; it is derived from the links'
      31              :  * parentId chains. See docs/architecture/reroute-chain-store.md.
      32              :  */
      33         2356 : export const useRerouteStore = defineStore('reroute', () => {
      34              :   interface RootRerouteBucket {
      35              :     chains: Map<RerouteId, RerouteChain>
      36              :     idsByOwner: Map<OwningGraphId, Set<RerouteId>>
      37              :     membershipByOwner: Map<
      38              :       OwningGraphId,
      39              :       ComputedRef<Map<RerouteId, RerouteMembership>>
      40              :     >
      41              :   }
      42              : 
      43         2355 :   const roots = reactive(new Map<RootGraphId, RootRerouteBucket>())
      44              : 
      45           33 :   function rootBucket(rootGraphId: RootGraphId): RootRerouteBucket {
      46           33 :     const existing = roots.get(rootGraphId)
      47           33 :     if (existing) return existing
      48           33 :     const created: RootRerouteBucket = {
      49              :       chains: reactive(new Map()),
      50              :       idsByOwner: reactive(new Map()),
      51              :       membershipByOwner: new Map()
      52              :     }
      53           33 :     roots.set(rootGraphId, created)
      54           33 :     return created
      55              :   }
      56              : 
      57              :   /**
      58              :    * Builds the reverse index from the links' parentId chains: a link is a
      59              :    * member of exactly the reroutes on the chain walked from its terminal
      60              :    * reroute upstream. A link with an unassigned endpoint is floating.
      61              :    */
      62           41 :   function buildMembershipIndex(
      63              :     scope: GraphScope
      64              :   ): Map<RerouteId, RerouteMembership> {
      65           41 :     const chains = roots.get(scope.rootGraphId)?.chains
      66           41 :     const index = new Map<
      67              :       RerouteId,
      68              :       { linkIds: Set<LinkId>; floatingLinkIds: Set<LinkId> }
      69              :     >()
      70           41 :     for (const topology of useLinkStore().graphTopologies(scope)) {
      71          194 :       const floating = isFloatingTopology(topology)
      72          194 :       const visited = new Set<RerouteId>()
      73          194 :       let rerouteId = topology.parentId
      74          194 :       while (rerouteId !== undefined && !visited.has(rerouteId)) {
      75           51 :         const parent = chains?.get(rerouteId)
      76           51 :         if (!parent || parent.graphId !== scope.owningGraphId) break
      77           51 :         visited.add(rerouteId)
      78           51 :         let entry = index.get(rerouteId)
      79           51 :         if (!entry) {
      80           47 :           entry = { linkIds: new Set(), floatingLinkIds: new Set() }
      81           47 :           index.set(rerouteId, entry)
      82              :         }
      83              :         const members = floating ? entry.floatingLinkIds : entry.linkIds
      84           51 :         members.add(topology.id)
      85           51 :         rerouteId = parent.parentId
      86              :       }
      87              :     }
      88           41 :     return index
      89              :   }
      90              : 
      91         1303 :   function graphMembership(
      92              :     scope: GraphScope,
      93              :     bucket: RootRerouteBucket
      94              :   ): ComputedRef<Map<RerouteId, RerouteMembership>> {
      95         1303 :     const existing = bucket.membershipByOwner.get(scope.owningGraphId)
      96         1303 :     if (existing) return existing
      97           35 :     const next = computed(() => buildMembershipIndex(scope))
      98           35 :     bucket.membershipByOwner.set(scope.owningGraphId, next)
      99           35 :     return next
     100              :   }
     101              : 
     102         1303 :   function getMembership(
     103              :     scope: GraphScope,
     104              :     rerouteId: RerouteId
     105              :   ): RerouteMembership {
     106         1303 :     const bucket = roots.get(scope.rootGraphId)
     107         1303 :     if (!bucket) return EMPTY_MEMBERSHIP
     108         1303 :     return (
     109              :       graphMembership(scope, bucket).value.get(rerouteId) ?? EMPTY_MEMBERSHIP
     110              :     )
     111              :   }
     112              : 
     113              :   /**
     114              :    * @returns The registered chain. Re-registering the same raw chain under the
     115              :    * same owner returns the incumbent; `undefined` means a distinct chain
     116              :    * occupies its ID.
     117              :    */
     118           41 :   function registerReroute(
     119              :     scope: GraphScope,
     120              :     chain: RerouteChain
     121              :   ): RerouteChain | undefined {
     122           41 :     const existingBucket = roots.get(scope.rootGraphId)
     123           41 :     const existing = existingBucket?.chains.get(chain.id)
     124           41 :     if (
     125              :       existing &&
     126              :       toRaw(existing) === toRaw(chain) &&
     127              :       existing.graphId === scope.owningGraphId
     128              :     )
     129            0 :       return existing
     130           41 :     if (existing) {
     131            0 :       console.error(
     132              :         `[rerouteStore] Reroute ${chain.id} belongs to graph ${existing.graphId}; graph ${scope.owningGraphId} cannot overwrite it.`
     133              :       )
     134            0 :       return undefined
     135              :     }
     136           41 :     const bucket = existingBucket ?? rootBucket(scope.rootGraphId)
     137           41 :     const owned = Object.assign(chain, { graphId: scope.owningGraphId })
     138           41 :     bucket.chains.set(owned.id, owned)
     139           41 :     const ownerIds = bucket.idsByOwner.get(scope.owningGraphId)
     140           41 :     if (ownerIds) ownerIds.add(owned.id)
     141              :     else
     142           35 :       bucket.idsByOwner.set(scope.owningGraphId, reactive(new Set([owned.id])))
     143           41 :     return bucket.chains.get(owned.id)
     144              :   }
     145              : 
     146            0 :   function getReroute(
     147              :     scope: GraphScope,
     148              :     rerouteId: RerouteId
     149              :   ): RerouteChain | undefined {
     150            0 :     const chain = roots.get(scope.rootGraphId)?.chains.get(rerouteId)
     151            0 :     return chain?.graphId === scope.owningGraphId ? chain : undefined
     152              :   }
     153              : 
     154            0 :   function* graphChains(scope: GraphScope): Generator<RerouteChain> {
     155            0 :     const bucket = roots.get(scope.rootGraphId)
     156            0 :     const ids = bucket?.idsByOwner.get(scope.owningGraphId)
     157            0 :     if (!bucket || !ids) return
     158            0 :     for (const id of ids) {
     159            0 :       const chain = bucket.chains.get(id)
     160            0 :       if (chain) yield chain
     161              :     }
     162              :   }
     163              : 
     164              :   /** Removes a chain's registration; only the registered state may vacate it. */
     165           16 :   function deleteReroute(scope: GraphScope, chain: RerouteChain): boolean {
     166           16 :     const bucket = roots.get(scope.rootGraphId)
     167           16 :     if (!bucket) return false
     168           16 :     if (chain.graphId !== scope.owningGraphId) return false
     169           16 :     if (toRaw(bucket.chains.get(chain.id)) !== toRaw(chain)) return false
     170           16 :     if (!bucket.chains.delete(chain.id)) return false
     171           16 :     const ownerIds = bucket.idsByOwner.get(chain.graphId)
     172           16 :     ownerIds?.delete(chain.id)
     173           16 :     if (ownerIds?.size === 0) {
     174           16 :       bucket.idsByOwner.delete(chain.graphId)
     175           16 :       bucket.membershipByOwner.delete(chain.graphId)
     176              :     }
     177           16 :     if (bucket.chains.size === 0) roots.delete(scope.rootGraphId)
     178           16 :     return true
     179              :   }
     180              : 
     181        11709 :   function clearGraph(graphId: RootGraphId): void {
     182        11709 :     roots.delete(graphId)
     183              :   }
     184              : 
     185          767 :   function clearOwner(scope: GraphScope): void {
     186          767 :     const bucket = roots.get(scope.rootGraphId)
     187          767 :     const ids = bucket?.idsByOwner.get(scope.owningGraphId)
     188          767 :     if (!bucket || !ids) return
     189            0 :     for (const id of ids) bucket.chains.delete(id)
     190            0 :     bucket.idsByOwner.delete(scope.owningGraphId)
     191            0 :     bucket.membershipByOwner.delete(scope.owningGraphId)
     192            0 :     if (bucket.chains.size === 0) roots.delete(scope.rootGraphId)
     193              :   }
     194              : 
     195         2355 :   return {
     196              :     registerReroute,
     197              :     getReroute,
     198              :     graphChains,
     199              :     deleteReroute,
     200              :     getMembership,
     201              :     clearOwner,
     202              :     clearGraph
     203              :   }
     204              : })
        

Generated by: LCOV version 2.0-1