From ad2c1a0d3e0771352766a17f28d68d8fc59d3d0c Mon Sep 17 00:00:00 2001 From: filtered <176114999+webfiltered@users.noreply.github.com> Date: Sat, 23 Nov 2024 13:49:13 +1100 Subject: [PATCH] Fix undo / redo filling with empty steps (#1649) --- src/scripts/changeTracker.ts | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/src/scripts/changeTracker.ts b/src/scripts/changeTracker.ts index f8cced23a..f7cc309be 100644 --- a/src/scripts/changeTracker.ts +++ b/src/scripts/changeTracker.ts @@ -10,15 +10,7 @@ import _ from 'lodash' import * as jsondiffpatch from 'jsondiffpatch' import log from 'loglevel' -function clone(obj: any) { - try { - if (typeof structuredClone !== 'undefined') { - return structuredClone(obj) - } - } catch (error) { - // structuredClone is stricter than using JSON.parse/stringify so fallback to that - } - +function clone(obj: T): T { return JSON.parse(JSON.stringify(obj)) } @@ -69,7 +61,7 @@ export class ChangeTracker { if (this.restoringState) return logger.debug('Reset State') - this.activeState = state ?? this.activeState + if (state) this.activeState = clone(state) this.initialState = clone(this.activeState) } @@ -112,9 +104,9 @@ export class ChangeTracker { checkState() { if (!this.app.graph || this.changeCount) return // @ts-expect-error zod types issue. Will be fixed after we enable ts-strict - const currentState = this.app.graph.serialize() as ComfyWorkflowJSON + const currentState = clone(this.app.graph.serialize()) as ComfyWorkflowJSON if (!this.activeState) { - this.activeState = clone(currentState) + this.activeState = currentState return } if (!ChangeTracker.graphEqual(this.activeState, currentState)) { @@ -124,7 +116,7 @@ export class ChangeTracker { } logger.debug('Diff detected. Undo queue length:', this.undoQueue.length) - this.activeState = clone(currentState) + this.activeState = currentState this.redoQueue.length = 0 api.dispatchEvent( new CustomEvent('graphChanged', { detail: this.activeState })