Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,28 @@ describe('useStringWidget (multiline)', () => {
expect(entries.every((s) => s.type === 'customtext')).toBe(true)
})

it('does not throw when a saved value is restored synchronously during construction', () => {
// Mirrors LGraphNode.addCustomWidget, which restores a saved widget
// value (from a loaded workflow) by invoking `options.setValue`
// synchronously, from inside `node.addDOMWidget(...)`, before that
// call returns.
const node = createMockDOMWidgetNode({
addDOMWidget: vi.fn(
(
name: string,
type: string,
element: HTMLElement,
options?: { setValue?: (v: string) => void }
) => {
options?.setValue?.('restored-value')
return { name, type, element, options: options ?? {}, value: '' }
}
)
})

expect(() => createStringWidget(node)).not.toThrow()
})

it('fires the widget callback on input', () => {
const { inputEl, callback } = setup()
inputEl.value = 'hello'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import { isStringInputSpec } from '@/schemas/nodeDef/nodeDefSchemaV2'
import type { InputSpec } from '@/schemas/nodeDef/nodeDefSchemaV2'
import { app } from '@/scripts/app'
import type { DOMWidgetOptions } from '@/scripts/domWidget'
import type { ComfyWidgetConstructorV2 } from '@/scripts/widgets'
import { useWidgetValueStore } from '@/stores/widgetValueStore'
import { widgetId } from '@/types/widgetId'
Expand All @@ -23,7 +24,11 @@ function addMultilineWidget(
opts.placeholder || name
)

const widget = node.addDOMWidget(name, 'customtext', inputEl, {
// Declared separately (rather than reading `widget.options` from the
// outer closure below) because `addDOMWidget` can invoke `setValue`
// synchronously, while restoring a saved value, before the `widget`
// binding it returns is initialized.
const widgetOptions: DOMWidgetOptions<string> = {
getValue(): string {
const graphId = resolveNodeRootGraphId(node, app.rootGraph.id)
const widgetState = widgetStore.getWidget(
Expand All @@ -44,10 +49,12 @@ function addMultilineWidget(
widgetStore.registerWidget(id, {
type: 'customtext',
value: v,
options: widget.options ?? {}
options: widgetOptions
})
}
})
}

const widget = node.addDOMWidget(name, 'customtext', inputEl, widgetOptions)

widget.element = inputEl

Expand Down
Loading