Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

fix(webv2): flush pending library autosave edits on dispose

joshistoast0e36f8e418b9
diff --git a/invokeai/frontend/webv2/src/features/workflow/ui/library/libraryAutosave.test.ts b/invokeai/frontend/webv2/src/features/workflow/ui/library/libraryAutosave.test.ts
index a1777dd69a..70c034f6ae 100644
--- a/invokeai/frontend/webv2/src/features/workflow/ui/library/libraryAutosave.test.ts
+++ b/invokeai/frontend/webv2/src/features/workflow/ui/library/libraryAutosave.test.ts
@@ -97,12 +97,90 @@ describe('createLibraryAutosaver', () => {
expect(h.statuses.at(-1)).toBe('saved');
});
- it('dispose cancels pending work', () => {
+ it('flushes a pending edit on dispose instead of dropping it', () => {
const h = createHarness();
h.autosaver.notifyGraphChanged();
+ expect(h.save).not.toHaveBeenCalled();
+
h.autosaver.dispose();
- h.manual.fire();
+
+ expect(h.save).toHaveBeenCalledTimes(1);
+ expect(h.manual.pendingCount()).toBe(0);
+ });
+
+ it('does not save on dispose when nothing is pending', () => {
+ const h = createHarness();
+ h.autosaver.dispose();
+ expect(h.save).not.toHaveBeenCalled();
+ });
+
+ it('a second dispose() is a no-op', () => {
+ const h = createHarness();
+ h.autosaver.notifyGraphChanged();
+ h.autosaver.dispose();
+ expect(h.save).toHaveBeenCalledTimes(1);
+
+ h.autosaver.dispose();
+
+ expect(h.save).toHaveBeenCalledTimes(1);
+ expect(h.manual.pendingCount()).toBe(0);
+ });
+
+ it('flush() after dispose() performs no save and resolves', async () => {
+ const h = createHarness();
+ h.autosaver.dispose();
+
+ const result = h.autosaver.flush();
+
expect(h.save).not.toHaveBeenCalled();
+ await expect(result).resolves.toBeUndefined();
+ });
+
+ it('dispose does not emit status callbacks for the flush save', async () => {
+ const h = createHarness();
+ h.autosaver.notifyGraphChanged();
+ h.statuses.length = 0; // drop the 'dirty' status from notifyGraphChanged
+ h.autosaver.dispose();
+ await vi.waitFor(() => expect(h.save).toHaveBeenCalledTimes(1));
+ expect(h.statuses).toEqual([]);
+ });
+
+ it('disposing while a save is in flight with a newer edit pending still runs the chained rerun', async () => {
+ const h = createHarness();
+ let resolveSave: () => void;
+ const deferred = new Promise<void>((resolve) => {
+ resolveSave = resolve;
+ });
+ h.save.mockReturnValueOnce(deferred);
+ h.autosaver.notifyGraphChanged();
+ h.manual.fire();
+ // First save is now in flight (unresolved).
+ h.setCurrent({ libraryWorkflowId: 'wf-1', serialized: { nodes: [2] } });
+ h.autosaver.notifyGraphChanged();
+
+ h.autosaver.dispose();
+ // The pending debounce timer was flushed into a chained rerun queued behind
+ // the in-flight save, so no new timer should remain.
+ expect(h.manual.pendingCount()).toBe(0);
+
+ resolveSave!();
+ // The in-flight promise is never cancelled by dispose; it settles, then the
+ // chained runSave() picks up the newer edit and saves it too.
+ await vi.waitFor(() => expect(h.save).toHaveBeenCalledTimes(2));
+ expect(h.save).toHaveBeenLastCalledWith('wf-1', { nodes: [2] });
+ });
+
+ it('dispose with a pending edit whose save rejects does not throw or emit status', async () => {
+ const h = createHarness();
+ h.save.mockRejectedValueOnce(new Error('offline'));
+ h.autosaver.notifyGraphChanged();
+ h.statuses.length = 0;
+
+ h.autosaver.dispose();
+ await vi.waitFor(() => expect(h.save).toHaveBeenCalledTimes(1));
+ // Give the rejection handler's microtask a chance to run before asserting silence.
+ await Promise.resolve();
+ expect(h.statuses).toEqual([]);
});
it('a change made during an in-flight save is saved afterward', async () => {
diff --git a/invokeai/frontend/webv2/src/features/workflow/ui/library/libraryAutosave.ts b/invokeai/frontend/webv2/src/features/workflow/ui/library/libraryAutosave.ts
index 19027bac6d..a045aa38af 100644
--- a/invokeai/frontend/webv2/src/features/workflow/ui/library/libraryAutosave.ts
+++ b/invokeai/frontend/webv2/src/features/workflow/ui/library/libraryAutosave.ts
@@ -4,6 +4,12 @@
* serialized JSON string) prevents echo saves; a failed save parks in 'error'
* and is retried by the next graph change or an explicit flush(). No automatic
* retry loop — persistent failures must not spin (cf. bitmapStore anti-spin).
+ *
+ * dispose() flushes rather than drops: a pending debounced edit is an unsaved
+ * change, so unmount collapses it into one final save (status callbacks stay
+ * muted). Known limitation: a hard tab close inside the debounce window can
+ * still lose the final write — there is no pagehide/keepalive path. Fixing
+ * that needs a transport-level keepalive decision and is out of scope here.
*/
export type LibrarySyncStatus = 'idle' | 'dirty' | 'saving' | 'saved' | 'error';
@@ -51,18 +57,22 @@ export const createLibraryAutosaver = (deps: LibraryAutosaverDeps) => {
const { libraryWorkflowId, serialized } = deps.read();
- if (disposed || !libraryWorkflowId) {
+ if (!libraryWorkflowId) {
return Promise.resolve();
}
const json = JSON.stringify(serialized);
if (json === lastSavedJson) {
- deps.onStatus('saved');
+ if (!disposed) {
+ deps.onStatus('saved');
+ }
return Promise.resolve();
}
- deps.onStatus('saving');
+ if (!disposed) {
+ deps.onStatus('saving');
+ }
inFlight = deps
.save(libraryWorkflowId, serialized)
.then(() => {
@@ -85,10 +95,23 @@ export const createLibraryAutosaver = (deps: LibraryAutosaverDeps) => {
return {
dispose: (): void => {
+ if (disposed) {
+ return;
+ }
disposed = true;
+ // A pending debounce is an unsaved edit; losing it on unmount is data
+ // loss, so the timer collapses into one immediate final save. Status
+ // callbacks stay muted (disposed) — only the write itself survives.
+ const hasPendingEdit = timerHandle !== null;
clearTimer();
+ if (hasPendingEdit) {
+ void runSave();
+ }
},
flush: (): Promise<void> => {
+ if (disposed) {
+ return Promise.resolve();
+ }
clearTimer();
return runSave();
},