Line data Source code
1 : import { debounce } from 'es-toolkit'
2 :
3 : import type { CompositorLayerStateSession } from '@/renderer/extensions/layerEditor/compositor/compositorSave'
4 : import { saveCompositorLayerState } from '@/renderer/extensions/layerEditor/compositor/compositorSave'
5 : import { Dirty } from '@comfyorg/layer-editor/history'
6 : import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
7 :
8 2 : const AUTO_SAVE_DEBOUNCE_MS = 300
9 :
10 : interface CompositorAutoSaveSession extends CompositorLayerStateSession {
11 : editor: {
12 : history: { onChange(listener: (mask: number) => void): () => void }
13 : }
14 : }
15 :
16 0 : export function useCompositorAutoSave(
17 : session: CompositorAutoSaveSession,
18 : node: LGraphNode
19 : ) {
20 0 : const save = debounce(() => {
21 0 : saveCompositorLayerState(session, node)
22 : }, AUTO_SAVE_DEBOUNCE_MS)
23 0 : const unsubscribe = session.editor.history.onChange((mask) => {
24 0 : if ((mask & ~Dirty.SELECTION) !== 0) save()
25 : })
26 :
27 0 : function stop(): void {
28 0 : unsubscribe()
29 0 : save.cancel()
30 : }
31 :
32 0 : return { stop }
33 : }
|