Line data Source code
1 : import type {
2 : LGraphConfig,
3 : LGraphExtra
4 : } from '@comfyorg/graph-model/entityRegistry'
5 : import { defineStore } from 'pinia'
6 : import { shallowReactive } from 'vue'
7 :
8 : import type { UUID } from '@/utils/uuid'
9 :
10 : interface GraphMetadata {
11 : revision: number
12 : config: LGraphConfig
13 : extra: LGraphExtra
14 : }
15 :
16 2356 : export const useGraphMetadataStore = defineStore('graphMetadata', () => {
17 2355 : const metadataByRoot = shallowReactive(
18 : new Map<UUID, Map<UUID, GraphMetadata>>()
19 : )
20 :
21 204051 : function get(rootGraphId: UUID, graphId: UUID = rootGraphId): GraphMetadata {
22 204051 : let metadata = metadataByRoot.get(rootGraphId)
23 204051 : if (!metadata) {
24 10950 : metadata = shallowReactive(new Map())
25 10950 : metadataByRoot.set(rootGraphId, metadata)
26 : }
27 204051 : const existing = metadata.get(graphId)
28 204051 : if (existing) return existing
29 :
30 12484 : const created = shallowReactive<GraphMetadata>({
31 : revision: 0,
32 : config: {},
33 : extra: {}
34 : })
35 12484 : metadata.set(graphId, created)
36 12484 : return created
37 : }
38 :
39 10907 : function rekeyRoot(previousId: UUID, nextId: UUID): void {
40 10907 : if (previousId === nextId) return
41 10907 : const metadata = metadataByRoot.get(previousId)
42 10907 : if (!metadata) return
43 724 : metadataByRoot.delete(previousId)
44 724 : const rootMetadata = metadata.get(previousId)
45 724 : if (rootMetadata) {
46 724 : metadata.delete(previousId)
47 724 : metadata.set(nextId, rootMetadata)
48 : }
49 724 : metadataByRoot.set(nextId, metadata)
50 : }
51 :
52 767 : function rekeyGraph(rootGraphId: UUID, previousId: UUID, nextId: UUID): void {
53 767 : if (previousId === nextId) return
54 767 : const metadata = metadataByRoot.get(rootGraphId)
55 767 : const existing = metadata?.get(previousId)
56 767 : if (!metadata || !existing) return
57 0 : metadata.delete(previousId)
58 0 : metadata.set(nextId, existing)
59 : }
60 :
61 1534 : function has(rootGraphId: UUID, graphId: UUID = rootGraphId): boolean {
62 1534 : return metadataByRoot.get(rootGraphId)?.has(graphId) ?? false
63 : }
64 :
65 10907 : function hasRoot(rootGraphId: UUID): boolean {
66 10907 : return metadataByRoot.has(rootGraphId)
67 : }
68 :
69 23868 : function clear(rootGraphId: UUID, graphId?: UUID): void {
70 23868 : if (graphId === undefined) {
71 0 : metadataByRoot.delete(rootGraphId)
72 0 : return
73 : }
74 23868 : const metadata = metadataByRoot.get(rootGraphId)
75 23868 : metadata?.delete(graphId)
76 23868 : if (metadata?.size === 0) metadataByRoot.delete(rootGraphId)
77 : }
78 :
79 2355 : return { clear, get, has, hasRoot, rekeyGraph, rekeyRoot }
80 : })
|