Line data Source code
1 1 : const STORAGE_KEY = 'Comfy.Agent.UnsyncedHumanOps'
2 :
3 1 : let unsyncedAtLoad: Set<string> | null = null
4 1 : const unsyncedByRecorder = new Map<string, ReadonlySet<string>>()
5 :
6 0 : function readStored(): string[] {
7 0 : try {
8 0 : const parsed: unknown = JSON.parse(
9 : sessionStorage.getItem(STORAGE_KEY) ?? '[]'
10 : )
11 0 : return Array.isArray(parsed)
12 : ? parsed.filter((id): id is string => typeof id === 'string')
13 : : []
14 : } catch {
15 0 : return []
16 : }
17 : }
18 :
19 0 : function writeStored(ids: readonly string[]): void {
20 0 : try {
21 0 : if (ids.length === 0) sessionStorage.removeItem(STORAGE_KEY)
22 0 : else sessionStorage.setItem(STORAGE_KEY, JSON.stringify(ids))
23 : } catch {
24 0 : return
25 : }
26 : }
27 :
28 0 : function loadOnce(): Set<string> {
29 0 : unsyncedAtLoad ??= new Set(readStored())
30 0 : return unsyncedAtLoad
31 : }
32 :
33 0 : function unsyncedNow(): Set<string> {
34 0 : return new Set([
35 : ...loadOnce(),
36 : ...[...unsyncedByRecorder.values()].flatMap((ids) => [...ids])
37 : ])
38 : }
39 :
40 : /** Records this recorder's workflows whose canvas holds edits the doc may lack. */
41 0 : export function recordUnsyncedHumanOps(
42 : recorder: string,
43 : workflowIds: Iterable<string>
44 : ): void {
45 0 : const ids = new Set(workflowIds)
46 0 : if (ids.size === 0) unsyncedByRecorder.delete(recorder)
47 0 : else unsyncedByRecorder.set(recorder, ids)
48 0 : writeStored([...unsyncedNow()])
49 : }
50 :
51 : /** Preserves a disposed recorder's warnings for its successor. */
52 0 : export function releaseUnsyncedHumanOpsRecorder(recorder: string): void {
53 0 : const recorded = unsyncedByRecorder.get(recorder)
54 0 : if (recorded === undefined) return
55 0 : for (const workflowId of recorded) loadOnce().add(workflowId)
56 0 : unsyncedByRecorder.delete(recorder)
57 0 : writeStored([...unsyncedNow()])
58 : }
59 :
60 : /** Whether `workflowId`'s canvas holds edits the shared document may lack. */
61 0 : export function hasUnsyncedHumanOps(workflowId: string): boolean {
62 0 : return unsyncedNow().has(workflowId)
63 : }
64 :
65 : /** Drops what a page before this one recorded for `workflowId`. */
66 0 : export function forgetUnsyncedAtLoad(workflowId: string | null): void {
67 0 : if (workflowId === null) return
68 0 : if (!loadOnce().delete(workflowId)) return
69 0 : writeStored([...unsyncedNow()])
70 : }
|