Repository navigation
refactor(compositor): make compositorWidgets a typed store boundary #15964
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
792dc9b
a6400a2
60abf3b
2a57489
6d5982e
773c39b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,81 +1,105 @@ | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import { fromPartial } from '@total-typescript/shoehorn' | ||
| import { describe, expect, it } from 'vitest' | ||
|
|
||
| import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' | ||
| import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' | ||
| import type { ICompositorWidget } from '@/lib/litegraph/src/types/widgets' | ||
| import { useWidgetValueStore } from '@/stores/widgetValueStore' | ||
| import { toNodeId } from '@/types/nodeId' | ||
| import { widgetId } from '@/types/widgetId' | ||
| import { createMockLGraphNode } from '@/utils/__tests__/litegraphTestUtils' | ||
|
|
||
| import { | ||
| getCompositorWidgetValue, | ||
| resetCompositorStateWidgets, | ||
| setCompositorWidgetValue | ||
| } from './compositorWidgets' | ||
|
|
||
| function makeNode() { | ||
| const compositorWidget = { | ||
| name: 'compositor', | ||
| value: { layers: [] }, | ||
| callback: vi.fn() | ||
| } as unknown as IBaseWidget | ||
| const otherWidget = { | ||
| name: 'other', | ||
| value: 42, | ||
| callback: vi.fn() | ||
| } as unknown as IBaseWidget | ||
| const node = { | ||
| widgets: [otherWidget, compositorWidget], | ||
| widgets_values: [42, { layers: [] }] | ||
| } as unknown as LGraphNode | ||
| return { node, compositorWidget, otherWidget } | ||
| const GRAPH_ID = 'compositor-test-graph' | ||
|
|
||
| let nodeCounter = 0 | ||
|
|
||
| function makeNode({ registered = true } = {}): LGraphNode { | ||
| nodeCounter += 1 | ||
| const nodeId = toNodeId(nodeCounter) | ||
| if (registered) { | ||
| useWidgetValueStore().registerWidget( | ||
| widgetId(GRAPH_ID, nodeId, 'compositor'), | ||
| { type: 'compositor', value: { layers: [] }, options: {} } | ||
| ) | ||
| } | ||
| return createMockLGraphNode({ | ||
| id: nodeId, | ||
| graph: { rootGraph: { id: GRAPH_ID } } | ||
| }) | ||
| } | ||
|
|
||
| function storedValue(node: LGraphNode): unknown { | ||
| return useWidgetValueStore().getWidget( | ||
| widgetId(GRAPH_ID, node.id, 'compositor') | ||
| )?.value | ||
| } | ||
|
|
||
| describe('setCompositorWidgetValue', () => { | ||
| it('writes the widget value, fires its callback, and syncs widgets_values', () => { | ||
| const { node, compositorWidget } = makeNode() | ||
| it('writes the widget value to the store', () => { | ||
| const node = makeNode() | ||
| const next = { canvas: { w: 8, h: 8 }, layers: [] } | ||
|
|
||
| setCompositorWidgetValue(node, next) | ||
|
|
||
| expect(compositorWidget.value).toEqual(next) | ||
| expect(compositorWidget.callback).toHaveBeenCalledWith(next) | ||
| expect(node.widgets_values?.[1]).toEqual(next) | ||
| expect(storedValue(node)).toEqual(next) | ||
| }) | ||
|
|
||
| it('is a no-op when the compositor widget is missing', () => { | ||
| const { node, otherWidget } = makeNode() | ||
| node.widgets = [otherWidget] | ||
| node.widgets_values = [42] | ||
| it('falls back to the widget object when the store has no entry', () => { | ||
| const node = makeNode({ registered: false }) | ||
| const widget = fromPartial<ICompositorWidget>({ | ||
| name: 'compositor', | ||
| type: 'compositor', | ||
| value: { layers: [] } | ||
| }) | ||
| node.widgets = [widget] | ||
| const next = { canvas: { w: 4, h: 4 }, layers: [] } | ||
|
|
||
| setCompositorWidgetValue(node, next) | ||
|
|
||
| expect(widget.value).toEqual(next) | ||
| expect(getCompositorWidgetValue(node)).toEqual(next) | ||
| }) | ||
|
|
||
| it('is a no-op when the widget exists nowhere', () => { | ||
| const node = makeNode({ registered: false }) | ||
|
|
||
| setCompositorWidgetValue(node, { layers: [] }) | ||
|
|
||
| expect(otherWidget.value).toBe(42) | ||
| expect(node.widgets_values).toEqual([42]) | ||
| expect(storedValue(node)).toBeUndefined() | ||
| }) | ||
| }) | ||
|
|
||
| describe('getCompositorWidgetValue', () => { | ||
| it('returns the widget value when it has the expected shape', () => { | ||
| const { node } = makeNode() | ||
| const node = makeNode() | ||
|
|
||
| expect(getCompositorWidgetValue(node)).toEqual({ layers: [] }) | ||
| }) | ||
|
|
||
| it('returns null for missing widgets or malformed values', () => { | ||
| const { node, compositorWidget } = makeNode() | ||
| compositorWidget.value = 'legacy-string' | ||
| const node = makeNode() | ||
| useWidgetValueStore().setValue( | ||
| widgetId(GRAPH_ID, node.id, 'compositor'), | ||
| 'legacy-string' | ||
| ) | ||
| expect(getCompositorWidgetValue(node)).toBeNull() | ||
|
|
||
| node.widgets = [] | ||
| expect(getCompositorWidgetValue(node)).toBeNull() | ||
| expect(getCompositorWidgetValue(makeNode({ registered: false }))).toBeNull() | ||
| }) | ||
| }) | ||
|
|
||
| describe('resetCompositorStateWidgets', () => { | ||
| it('resets the compositor widget to the empty value durably', () => { | ||
| const { node, compositorWidget } = makeNode() | ||
| const node = makeNode() | ||
| setCompositorWidgetValue(node, { canvas: { w: 8, h: 8 }, layers: [] }) | ||
|
|
||
| resetCompositorStateWidgets(node) | ||
|
|
||
| expect(compositorWidget.value).toEqual({}) | ||
| expect(compositorWidget.callback).toHaveBeenCalledWith({}) | ||
| expect(node.widgets_values).toEqual([42, {}]) | ||
| expect(storedValue(node)).toEqual({}) | ||
| }) | ||
| }) |
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,29 +1,38 @@ | ||||||||||||
| import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' | ||||||||||||
| import { | ||||||||||||
| emptyCompositorWidgetValue, | ||||||||||||
| isCompositorWidgetValue | ||||||||||||
| } from '@/renderer/extensions/compositor/components/types' | ||||||||||||
| import type { CompositorWidgetValue } from '@/renderer/extensions/compositor/components/types' | ||||||||||||
| import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' | ||||||||||||
| import { useWidgetValueStore } from '@/stores/widgetValueStore' | ||||||||||||
| import type { WidgetId } from '@/types/widgetId' | ||||||||||||
| import { widgetId } from '@/types/widgetId' | ||||||||||||
|
|
||||||||||||
| const COMPOSITOR_WIDGET = 'compositor' | ||||||||||||
|
|
||||||||||||
| function compositorWidgetId(node: LGraphNode): WidgetId | null { | ||||||||||||
| const graphId = node.graph?.rootGraph?.id | ||||||||||||
| return graphId ? widgetId(graphId, node.id, COMPOSITOR_WIDGET) : null | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| export function setCompositorWidgetValue( | ||||||||||||
| node: LGraphNode, | ||||||||||||
| value: CompositorWidgetValue | ||||||||||||
| ): void { | ||||||||||||
| const widget = node.widgets?.find((w) => w.name === 'compositor') | ||||||||||||
| if (!widget) return | ||||||||||||
| widget.value = value | ||||||||||||
| widget.callback?.(value) | ||||||||||||
| if (node.widgets_values && node.widgets) { | ||||||||||||
| const index = node.widgets.indexOf(widget) | ||||||||||||
| if (index >= 0) node.widgets_values[index] = value | ||||||||||||
| } | ||||||||||||
| const id = compositorWidgetId(node) | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably make a more universal way to do this so nodes don't have to write this each time. |
||||||||||||
| if (id && useWidgetValueStore().setValue(id, value)) return | ||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Blocker: This early return, together with the fallback below, removes the prior
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Re-evaluated against the production widget and CI failure: the compositor constructor intentionally supplies a null callback, and serialization rebuilds widget-value shadows from live values. Commit
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jtydhr88 Want to double check the expectations here? |
||||||||||||
| const widget = node.widgets?.find((w) => w.name === COMPOSITOR_WIDGET) | ||||||||||||
| if (widget) widget.value = value | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| export function getCompositorWidgetValue( | ||||||||||||
| node: LGraphNode | ||||||||||||
| ): CompositorWidgetValue | null { | ||||||||||||
| const widget = node.widgets?.find((w) => w.name === 'compositor') | ||||||||||||
| return isCompositorWidgetValue(widget?.value) ? widget.value : null | ||||||||||||
| const id = compositorWidgetId(node) | ||||||||||||
| const value = | ||||||||||||
| (id ? useWidgetValueStore().getWidget(id)?.value : undefined) ?? | ||||||||||||
| node.widgets?.find((w) => w.name === COMPOSITOR_WIDGET)?.value | ||||||||||||
|
Comment on lines
+32
to
+34
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
| return isCompositorWidgetValue(value) ? value : null | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| export function resetCompositorStateWidgets(node: LGraphNode): void { | ||||||||||||
|
|
||||||||||||
Uh oh!
There was an error while loading. Please reload this page.