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.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
refactor(workbench): generalize draft flushing
joshistoast18383ef9b763
diff --git a/invokeai/frontend/webv2/src/workbench/hooks/useMountEffect.ts b/invokeai/frontend/webv2/src/workbench/hooks/useMountEffect.tsnew file mode 100644index 0000000000..5c8d6f57a3--- /dev/null+++ b/invokeai/frontend/webv2/src/workbench/hooks/useMountEffect.ts@@ -0,0 +1,8 @@+import { useEffect, type EffectCallback } from 'react';++/** Explicit escape hatch for one-time external registration. */+export const useMountEffect = (effect: EffectCallback): void => {+ // The empty dependency list is the contract of this hook.+ // eslint-disable-next-line react-hooks/exhaustive-deps+ useEffect(effect, []);+};diff --git a/invokeai/frontend/webv2/src/workbench/widget-frame/WidgetActionsMenu.tsx b/invokeai/frontend/webv2/src/workbench/widget-frame/WidgetActionsMenu.tsxindex ce0677675d..f1dcde1d46 100644--- a/invokeai/frontend/webv2/src/workbench/widget-frame/WidgetActionsMenu.tsx+++ b/invokeai/frontend/webv2/src/workbench/widget-frame/WidgetActionsMenu.tsx@@ -13,6 +13,7 @@ import { IconButton } from '@workbench/components/ui';import { GraphPreviewDialog } from '@workbench/graph-preview';import { createGraphBearingSurface } from '@workbench/graphSurfaces';import { resolveWidgetLabel } from '@workbench/widgetLabels';+import { flushWorkbenchDrafts } from '@workbench/widgets/draftRegistry';import { shallowEqual, useActiveProjectSelector, useWorkbenchDispatch } from '@workbench/WorkbenchContext';import { compileProjectGraph } from '@workbench/workflows/buildGraph';import { getInvocationTemplatesSnapshot } from '@workbench/workflows/templates';@@ -116,7 +117,10 @@ export const WidgetActionsMenu = ({? createGraphBearingSurface(manifest, region, label): null;const surfaceSourceId = surface?.sourceId;- const handlePreview = useCallback(() => setIsPreviewOpen(true), []);+ const handlePreview = useCallback(() => {+ flushWorkbenchDrafts();+ setIsPreviewOpen(true);+ }, []);const positionHints =isPreviewOpen && surfaceSourceId === 'workflow'? Object.fromEntries(activeProject.projectGraph.nodes.map((node) => [node.id, node.position]))diff --git a/invokeai/frontend/webv2/src/workbench/widgetPlacementCommands.ts b/invokeai/frontend/webv2/src/workbench/widgetPlacementCommands.tsindex f12e9b62b4..5e0f2d345b 100644--- a/invokeai/frontend/webv2/src/workbench/widgetPlacementCommands.ts+++ b/invokeai/frontend/webv2/src/workbench/widgetPlacementCommands.ts@@ -13,6 +13,8 @@ import type { WidgetDragEndResolution } from './widgetDnd';import type { WidgetPlacementMeta } from './widgetRegionViewModel';import type { WorkbenchAction } from './workbenchState';+import { flushWorkbenchDrafts } from './widgets/draftRegistry';+export interface WidgetPlacementProject {projectId?: string;widgetInstances: WidgetPlacementMeta;@@ -141,6 +143,7 @@ export const closeWidgetPlacement = ({}}+ flushWorkbenchDrafts();dispatch({ projectId: project.projectId, region, type: 'toggleRegionWidget', widgetId: instanceId });return { ok: true, region };diff --git a/invokeai/frontend/webv2/src/workbench/widgets/draftRegistry.ts b/invokeai/frontend/webv2/src/workbench/widgets/draftRegistry.tsnew file mode 100644index 0000000000..b4f16b92b6--- /dev/null+++ b/invokeai/frontend/webv2/src/workbench/widgets/draftRegistry.ts@@ -0,0 +1,27 @@+import { useMountEffect } from '@workbench/hooks/useMountEffect';+import { useEffectEvent } from 'react';++type DraftFlusher = () => void;++const draftFlushers = new Set<DraftFlusher>();++/** Commits pending drafts from every mounted widget before a project snapshot boundary. */+export const flushWorkbenchDrafts = (): void => {+ for (const flushDraft of Array.from(draftFlushers)) {+ flushDraft();+ }+};++export const useRegisterDraftFlusher = (flushDraft: DraftFlusher): void => {+ const flushLatestDraft = useEffectEvent(flushDraft);++ useMountEffect(() => {+ const registeredFlushDraft = () => flushLatestDraft();++ draftFlushers.add(registeredFlushDraft);++ return () => {+ draftFlushers.delete(registeredFlushDraft);+ };+ });+};diff --git a/invokeai/frontend/webv2/src/workbench/widgets/generate/generateDraftRegistry.ts b/invokeai/frontend/webv2/src/workbench/widgets/generate/generateDraftRegistry.tsindex c837efc698..75d9547583 100644--- a/invokeai/frontend/webv2/src/workbench/widgets/generate/generateDraftRegistry.ts+++ b/invokeai/frontend/webv2/src/workbench/widgets/generate/generateDraftRegistry.ts@@ -1,27 +1,5 @@-import { useEffect, useEffectEvent } from 'react';--type DraftFlusher = () => void;--const draftFlushers = new Set<DraftFlusher>();--export const flushGenerateDrafts = (): void => {- const flushers = Array.from(draftFlushers);-- for (const flushDraft of flushers) {- flushDraft();- }-};--export const useRegisterGenerateDraftFlusher = (flushDraft: DraftFlusher): void => {- const flushLatestDraft = useEffectEvent(flushDraft);-- useEffect(() => {- const registeredFlushDraft = () => flushLatestDraft();-- draftFlushers.add(registeredFlushDraft);-- return () => {- draftFlushers.delete(registeredFlushDraft);- };- }, []);-};+/** @deprecated Import the source-agnostic registry from widgets/draftRegistry. */+export {+ flushWorkbenchDrafts as flushGenerateDrafts,+ useRegisterDraftFlusher as useRegisterGenerateDraftFlusher,+} from '@workbench/widgets/draftRegistry';