Line data Source code
1 : import { defineStore } from 'pinia'
2 : import { shallowReactive } from 'vue'
3 :
4 : import { createLGraphState } from '@/lib/litegraph/src/idAllocation'
5 : import type { LGraphState } from '@/lib/litegraph/src/idAllocation'
6 : import type { UUID } from '@/utils/uuid'
7 :
8 2356 : export const useEntityIdStore = defineStore('entityId', () => {
9 2355 : const states = shallowReactive(new Map<UUID, LGraphState>())
10 :
11 121522 : function get(rootGraphId: UUID): LGraphState {
12 121522 : const existing = states.get(rootGraphId)
13 121522 : if (existing) return existing
14 :
15 0 : const created = createLGraphState()
16 0 : states.set(rootGraphId, created)
17 0 : return created
18 : }
19 :
20 10183 : function set(rootGraphId: UUID, state: LGraphState): void {
21 10183 : states.set(rootGraphId, { ...state })
22 : }
23 :
24 10907 : function rekey(previousId: UUID, nextId: UUID): void {
25 10907 : if (previousId === nextId) return
26 10907 : const existing = states.get(previousId)
27 10907 : if (!existing) return
28 724 : states.delete(previousId)
29 724 : states.set(nextId, existing)
30 : }
31 :
32 10907 : function has(rootGraphId: UUID): boolean {
33 10907 : return states.has(rootGraphId)
34 : }
35 :
36 10183 : function clear(rootGraphId: UUID): void {
37 10183 : states.delete(rootGraphId)
38 : }
39 :
40 2355 : return { get, has, set, rekey, clear }
41 : })
|