diff --git a/browser_tests/tests/workflowPersistence.spec.ts b/browser_tests/tests/workflowPersistence.spec.ts index ee2e3f42c46..8439d95a9a8 100644 --- a/browser_tests/tests/workflowPersistence.spec.ts +++ b/browser_tests/tests/workflowPersistence.spec.ts @@ -534,6 +534,42 @@ test.describe('Workflow Persistence', () => { await expect(comfyPage.toast.toastErrors).toHaveCount(0) }) + test('Flushes a pending draft edit before an immediate reload', async ({ + comfyPage + }) => { + test.info().annotations.push({ + type: 'regression', + description: + 'FE-1484 — refreshing inside the persistence debounce window lost the latest workflow edit' + }) + + await comfyPage.settings.setSetting('Comfy.Workflow.Persist', true) + await comfyPage.workflow.loadWorkflow('nodes/single_ksampler') + await fitToViewInstant(comfyPage) + + const firstNode = await getRequiredFirstNodeRef( + comfyPage, + 'First node should be available after loading single_ksampler' + ) + await firstNode.centerOnNode() + + const baselineSaveStartedAt = Date.now() + await firstNode.toggleCollapse() + await expect.poll(() => firstNode.isCollapsed()).toBe(true) + await comfyPage.workflow.waitForDraftIndexUpdatedSince( + baselineSaveStartedAt + ) + + await firstNode.toggleCollapse() + await comfyPage.workflow.reloadAndWaitForApp() + + const restoredNode = await getRequiredFirstNodeRef( + comfyPage, + 'First node should be restored after the immediate reload' + ) + await expect.poll(() => restoredNode.isCollapsed()).toBe(false) + }) + test('Closing an inactive tab with save preserves its own content', async ({ comfyPage }) => { diff --git a/src/platform/workflow/persistence/composables/useWorkflowPersistenceV2.test.ts b/src/platform/workflow/persistence/composables/useWorkflowPersistenceV2.test.ts index afe06e7b97f..c071b4c9bcc 100644 --- a/src/platform/workflow/persistence/composables/useWorkflowPersistenceV2.test.ts +++ b/src/platform/workflow/persistence/composables/useWorkflowPersistenceV2.test.ts @@ -622,6 +622,57 @@ describe('useWorkflowPersistenceV2', () => { }) }) + it('flushes a pending workflow edit when the page is unloaded', async () => { + const workflowStore = useWorkflowStore() + const workflow = await workflowStore.createTemporary('Draft.json').load() + workflowStore.activeWorkflow = workflow + mountWorkflowPersistence() + await nextTick() + + mocks.state.currentGraph = { + nodes: [], + extra: { marker: 'final-edit' } + } + mocks.state.graphChangedHandler?.() + + const payloadKey = StorageKeys.draftPayload(workflow.path, 'personal') + expect(localStorage.getItem(payloadKey)).toBeNull() + + window.dispatchEvent(new PageTransitionEvent('pagehide')) + + const payload = JSON.parse(localStorage.getItem(payloadKey)!) + expect(JSON.parse(payload.data)).toEqual({ + nodes: [], + extra: { marker: 'final-edit' } + }) + }) + + it('does not flush a pending workflow edit after disposal', async () => { + const workflowStore = useWorkflowStore() + const workflow = await workflowStore.createTemporary('Draft.json').load() + workflowStore.activeWorkflow = workflow + mountWorkflowPersistence() + + mocks.state.currentGraph = { nodes: [] } + const graphChangedHandler = mocks.state.graphChangedHandler + if (!graphChangedHandler) { + throw new Error('Graph change handler was not registered') + } + graphChangedHandler() + + const mounted = mountedApps.pop() + if (!mounted) throw new Error('Failed to find mounted persistence app') + mounted.app.unmount() + mounted.container.remove() + + window.dispatchEvent(new PageTransitionEvent('pagehide')) + await vi.runAllTimersAsync() + + expect( + localStorage.getItem(StorageKeys.draftPayload(workflow.path, 'personal')) + ).toBeNull() + }) + it('flushes the final source-workspace edit before blocking transition writes', async () => { const sourceWorkspaceId = 'workspace-a' const destinationWorkspaceId = 'workspace-b' diff --git a/src/platform/workflow/persistence/composables/useWorkflowPersistenceV2.ts b/src/platform/workflow/persistence/composables/useWorkflowPersistenceV2.ts index 5b7661ad692..b1dbba9a5f2 100644 --- a/src/platform/workflow/persistence/composables/useWorkflowPersistenceV2.ts +++ b/src/platform/workflow/persistence/composables/useWorkflowPersistenceV2.ts @@ -134,9 +134,15 @@ export function useWorkflowPersistenceV2() { // Debounced version for graphChanged events const debouncedPersist = debounce(persistCurrentWorkflow, PERSIST_DEBOUNCE_MS) - const unregisterPersistenceFlush = registerWorkflowPersistenceFlush(() => + + function flushPendingPersistence() { debouncedPersist.flush() + } + + const unregisterPersistenceFlush = registerWorkflowPersistenceFlush( + flushPendingPersistence ) + window.addEventListener('pagehide', flushPendingPersistence) const loadPreviousWorkflowFromStorage = async () => { const sessionPath = tabState.getActivePath() @@ -259,6 +265,7 @@ export function useWorkflowPersistenceV2() { // Clean up event listener when component unmounts tryOnScopeDispose(() => { api.removeEventListener('graphChanged', debouncedPersist) + window.removeEventListener('pagehide', flushPendingPersistence) unregisterPersistenceFlush() debouncedPersist.cancel() })