From d1cadb41428e0cf9b2003f7fa5d0a5d953c9357a Mon Sep 17 00:00:00 2001 From: bymyself Date: Wed, 4 Mar 2026 17:02:14 -0800 Subject: [PATCH 01/10] fix: reduce per-frame allocations in canvas draw loop Amp-Thread-ID: https://ampcode.com/threads/T-019cbb69-3404-726a-8888-182193115b88 --- src/lib/litegraph/src/LGraphCanvas.ts | 55 ++++++++++++------- src/lib/litegraph/src/LGraphNode.test.ts | 66 ++++++++++++++++++++++- src/lib/litegraph/src/LGraphNode.ts | 69 ++++++++++++++---------- src/lib/litegraph/src/LLink.test.ts | 14 +++++ src/lib/litegraph/src/LLink.ts | 6 ++- 5 files changed, 160 insertions(+), 50 deletions(-) diff --git a/src/lib/litegraph/src/LGraphCanvas.ts b/src/lib/litegraph/src/LGraphCanvas.ts index cd79d803b7d..7ff1251017f 100644 --- a/src/lib/litegraph/src/LGraphCanvas.ts +++ b/src/lib/litegraph/src/LGraphCanvas.ts @@ -714,6 +714,9 @@ export class LGraphCanvas implements CustomEventDispatcher * performant than {@link visible_nodes} for visibility checks. */ private _visible_node_ids: Set = new Set() + + /** Cached per-frame link render context to avoid rebuilding per-link. */ + private _cachedLinkRenderContext: LinkRenderContext | null = null node_over?: LGraphNode node_capturing_input?: LGraphNode | null highlighted_links: Dictionary = {} @@ -5117,10 +5120,11 @@ export class LGraphCanvas implements CustomEventDispatcher getCurrentGraphNodesInFrameOrder(), this.visible_nodes ) - // Update visible node IDs - this._visible_node_ids = new Set( - this.visible_nodes.map((node) => node.id) - ) + // Update visible node IDs (reuse existing Set to avoid allocation) + this._visible_node_ids.clear() + for (const node of this.visible_nodes) { + this._visible_node_ids.add(node.id) + } // Arrange subgraph IO nodes const { subgraph } = this @@ -5352,7 +5356,7 @@ export class LGraphCanvas implements CustomEventDispatcher fromDirection, dragDirection, { - ...this.buildLinkRenderContext(), + ...(this._cachedLinkRenderContext ?? this.buildLinkRenderContext()), linkMarkerShape: LinkMarkerShape.None } ) @@ -6136,6 +6140,9 @@ export class LGraphCanvas implements CustomEventDispatcher this.renderedPaths.clear() if (this.links_render_mode === LinkRenderType.HIDDEN_LINK) return + // Cache the link render context once per frame + this._cachedLinkRenderContext = this.buildLinkRenderContext() + const { graph, subgraph } = this if (!graph) throw new NullGraphError() @@ -6414,19 +6421,28 @@ export class LGraphCanvas implements CustomEventDispatcher // Get all points this link passes through const reroutes = LLink.getReroutes(graph, link) - const points: [Point, ...Point[], Point] = [ - startPos, - ...reroutes.map((x) => x.pos), - endPos - ] - // Bounding box of all points (bezier overshoot on long links will be cut) - const pointsX = points.map((x) => x[0]) - const pointsY = points.map((x) => x[1]) - link_bounding[0] = Math.min(...pointsX) - link_bounding[1] = Math.min(...pointsY) - link_bounding[2] = Math.max(...pointsX) - link_bounding[0] - link_bounding[3] = Math.max(...pointsY) - link_bounding[1] + // Compute bounding box inline to avoid allocating temporary arrays + let minX = startPos[0] + let minY = startPos[1] + let maxX = minX + let maxY = minY + for (let i = 0; i < reroutes.length; i++) { + const pos = reroutes[i].pos + if (pos[0] < minX) minX = pos[0] + else if (pos[0] > maxX) maxX = pos[0] + if (pos[1] < minY) minY = pos[1] + else if (pos[1] > maxY) maxY = pos[1] + } + if (endPos[0] < minX) minX = endPos[0] + else if (endPos[0] > maxX) maxX = endPos[0] + if (endPos[1] < minY) minY = endPos[1] + else if (endPos[1] > maxY) maxY = endPos[1] + + link_bounding[0] = minX + link_bounding[1] = minY + link_bounding[2] = maxX - minX + link_bounding[3] = maxY - minY // skip links outside of the visible area of the canvas if (!overlapBounding(link_bounding, margin_area)) return @@ -6494,8 +6510,7 @@ export class LGraphCanvas implements CustomEventDispatcher // Skip the last segment if it is being dragged if (link._dragging) return - // Use runtime fallback; TypeScript cannot evaluate this correctly. - const segmentStartPos = points.at(-2) ?? startPos + const segmentStartPos = reroutes.at(-1)?.pos ?? startPos // Render final link segment this.renderLink( @@ -6616,7 +6631,7 @@ export class LGraphCanvas implements CustomEventDispatcher } = {} ): void { if (this.linkRenderer) { - const context = this.buildLinkRenderContext() + const context = this._cachedLinkRenderContext ?? this.buildLinkRenderContext() this.linkRenderer.renderLinkDirect( ctx, a, diff --git a/src/lib/litegraph/src/LGraphNode.test.ts b/src/lib/litegraph/src/LGraphNode.test.ts index 4cdc98acfca..17290877d7d 100644 --- a/src/lib/litegraph/src/LGraphNode.test.ts +++ b/src/lib/litegraph/src/LGraphNode.test.ts @@ -878,8 +878,72 @@ describe('LGraphNode', () => { }) }) }) + describe('_slotsDirty flag', () => { + test('starts dirty', () => { + const n = new LGraphNode('Test') + expect(n._slotsDirty).toBe(true) + }) + + test('is cleared by _setConcreteSlots', () => { + const n = new LGraphNode('Test') + n._setConcreteSlots() + expect(n._slotsDirty).toBe(false) + }) + + test('skips work when clean', () => { + const n = new LGraphNode('Test') + n.addInput('in', 'number') + n._setConcreteSlots() + expect(n._slotsDirty).toBe(false) + + const mapSpy = vi.spyOn(n.inputs, 'map') + n._setConcreteSlots() + expect(mapSpy).not.toHaveBeenCalled() + }) + + test('is set by addInput', () => { + const n = new LGraphNode('Test') + n._setConcreteSlots() + n.addInput('in', 'number') + expect(n._slotsDirty).toBe(true) + }) + + test('is set by removeInput', () => { + const n = new LGraphNode('Test') + n.addInput('in', 'number') + n._setConcreteSlots() + n.removeInput(0) + expect(n._slotsDirty).toBe(true) + }) + + test('is set by addOutput', () => { + const n = new LGraphNode('Test') + n._setConcreteSlots() + n.addOutput('out', 'number') + expect(n._slotsDirty).toBe(true) + }) + + test('is set by removeOutput', () => { + const n = new LGraphNode('Test') + n.addOutput('out', 'number') + n._setConcreteSlots() + n.removeOutput(0) + expect(n._slotsDirty).toBe(true) + }) + + test('is set by configure', () => { + const n = new LGraphNode('Test') + n._setConcreteSlots() + n.configure( + getMockISerialisedNode({ + inputs: [{ name: 'a', type: 'number', link: null }], + outputs: [{ name: 'b', type: 'number', links: null }] + }) + ) + expect(n._slotsDirty).toBe(true) + }) + }) -describe('snapToGrid', () => { beforeEach(() => { setActivePinia(createTestingPinia({ stubActions: false })) }) diff --git a/src/lib/litegraph/src/LGraphNode.ts b/src/lib/litegraph/src/LGraphNode.ts index 3919e4bb8d9..752b55ed6cb 100644 --- a/src/lib/litegraph/src/LGraphNode.ts +++ b/src/lib/litegraph/src/LGraphNode.ts @@ -406,6 +406,8 @@ export class LGraphNode private _concreteInputs: NodeInputSlot[] = [] private _concreteOutputs: NodeOutputSlot[] = [] + /** @internal Set when inputs/outputs change; cleared by {@link _setConcreteSlots}. */ + _slotsDirty: boolean = true get properties(): Dictionary { return this._state.properties @@ -1094,6 +1096,7 @@ export class LGraphNode this.inputs = this.inputs.map((input) => toClass(NodeInputSlot, input, this) ) + this._slotsDirty = true for (const [i, input] of this.inputs.entries()) { const serialisedLink = info.inputs?.[i]?.link const link = @@ -1886,6 +1889,7 @@ export class LGraphNode this.outputs ||= [] this.outputs.push(output) + this._slotsDirty = true const added = this.outputs.at(-1) as NodeOutputSlot & TProperties this.onOutputAdded?.(added) @@ -1907,6 +1911,7 @@ export class LGraphNode } const { outputs } = this outputs.splice(slot, 1) + this._slotsDirty = true // Only update link indices if node is part of a graph. Ascending order: // each decrement re-keys the link to an already-processed slot index. @@ -1953,6 +1958,7 @@ export class LGraphNode this.inputs ||= [] this.inputs.push(input) + this._slotsDirty = true const added = this.inputs.at(-1) as NodeInputSlot & TProperties this.expandToFitContent() @@ -1991,6 +1997,7 @@ export class LGraphNode } else { inputs.splice(slot, 1) } + this._slotsDirty = true this.onInputRemoved?.(slot, slotInfo) this.setDirtyCanvas(true, true) } @@ -4300,33 +4307,38 @@ export class LGraphNode ctx: CanvasRenderingContext2D, { fromSlot, colorContext, editorAlpha, lowQuality }: DrawSlotsOptions ) { - for (const slot of [...this._concreteInputs, ...this._concreteOutputs]) { - const isValidTarget = fromSlot && slot.isValidTarget(fromSlot) - const isMouseOverSlot = this._isMouseOverSlot(slot) - - // change opacity of incompatible slots when dragging a connection - const isValid = !fromSlot || isValidTarget - const highlight = isValid && isMouseOverSlot - - // Show slot if it's not a widget input slot - // or if it's a widget input slot and satisfies one of the following: - // - the mouse is over the widget - // - the slot is valid during link drop - // - the slot is connected - if ( - isMouseOverSlot || - isValidTarget || - !slot.isWidgetInputSlot || - this._isMouseOverWidget(this.getWidgetFromSlot(slot)) || - slot.isConnected || - slot.alwaysVisible - ) { - ctx.globalAlpha = isValid ? editorAlpha : 0.4 * editorAlpha - slot.draw(ctx, { - colorContext, - lowQuality, - highlight - }) + const slotArrays = [this._concreteInputs, this._concreteOutputs] + for (let a = 0; a < 2; a++) { + const slots = slotArrays[a] + for (let s = 0; s < slots.length; s++) { + const slot = slots[s] + const isValidTarget = fromSlot && slot.isValidTarget(fromSlot) + const isMouseOverSlot = this._isMouseOverSlot(slot) + + // change opacity of incompatible slots when dragging a connection + const isValid = !fromSlot || isValidTarget + const highlight = isValid && isMouseOverSlot + + // Show slot if it's not a widget input slot + // or if it's a widget input slot and satisfies one of the following: + // - the mouse is over the widget + // - the slot is valid during link drop + // - the slot is connected + if ( + isMouseOverSlot || + isValidTarget || + !slot.isWidgetInputSlot || + this._isMouseOverWidget(this.getWidgetFromSlot(slot)) || + slot.isConnected || + slot.alwaysVisible + ) { + ctx.globalAlpha = isValid ? editorAlpha : 0.4 * editorAlpha + slot.draw(ctx, { + colorContext, + lowQuality, + highlight + }) + } } } } @@ -4458,6 +4470,8 @@ export class LGraphNode * invalidate slot-array subscribers. */ _setConcreteSlots(): void { + if (!this._slotsDirty) return + const { inputs, outputs } = this this._concreteInputs = inputs.map((slot, i) => { const concrete = toClass(NodeInputSlot, slot, this) @@ -4469,6 +4483,7 @@ export class LGraphNode if (concrete !== slot) outputs[i] = concrete return concrete }) + this._slotsDirty = false } /** diff --git a/src/lib/litegraph/src/LLink.test.ts b/src/lib/litegraph/src/LLink.test.ts index 2dc8b382e78..65e66b0accc 100644 --- a/src/lib/litegraph/src/LLink.test.ts +++ b/src/lib/litegraph/src/LLink.test.ts @@ -35,4 +35,18 @@ describe('LLink', () => { type: 'INT' }) }) + + describe('getReroutes', () => { + test('returns the same empty array instance for links without reroutes', () => { + const network = { reroutes: new Map() } + const link1 = new LLink(toLinkId(1), 'float', 4, 2, 5, 3) + const link2 = new LLink(toLinkId(2), 'float', 4, 2, 5, 3) + + const result1 = LLink.getReroutes(network, link1) + const result2 = LLink.getReroutes(network, link2) + + expect(result1).toHaveLength(0) + expect(result1).toBe(result2) + }) + }) }) diff --git a/src/lib/litegraph/src/LLink.ts b/src/lib/litegraph/src/LLink.ts index 23d360de302..0015245f838 100644 --- a/src/lib/litegraph/src/LLink.ts +++ b/src/lib/litegraph/src/LLink.ts @@ -35,6 +35,8 @@ import type { Serialisable, SerialisableLLink } from './types/serialisation' import { toRaw } from 'vue' export type { LinkId } from '@/types/linkId' + +const EMPTY_REROUTES: Reroute[] = [] as Reroute[] export type SerialisedLLinkArray = [ id: number, origin_id: SerializedNodeId, @@ -326,8 +328,8 @@ export class LLink implements LinkSegment, Serialisable { network: Pick, linkSegment: LinkSegment ): Reroute[] { - if (linkSegment.parentId === undefined) return [] - return network.reroutes.get(linkSegment.parentId)?.getReroutes() ?? [] + if (linkSegment.parentId === undefined) return EMPTY_REROUTES + return network.reroutes.get(linkSegment.parentId)?.getReroutes() ?? EMPTY_REROUTES } static getFirstReroute( From 18c3df208ba2f43e7a08fe0eb23703570925772f Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Thu, 5 Mar 2026 01:04:50 +0000 Subject: [PATCH 02/10] [automated] Apply ESLint and Oxfmt fixes --- src/lib/litegraph/src/LGraphCanvas.ts | 3 ++- src/lib/litegraph/src/LLink.ts | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/lib/litegraph/src/LGraphCanvas.ts b/src/lib/litegraph/src/LGraphCanvas.ts index 7ff1251017f..68ae87e7610 100644 --- a/src/lib/litegraph/src/LGraphCanvas.ts +++ b/src/lib/litegraph/src/LGraphCanvas.ts @@ -6631,7 +6631,8 @@ export class LGraphCanvas implements CustomEventDispatcher } = {} ): void { if (this.linkRenderer) { - const context = this._cachedLinkRenderContext ?? this.buildLinkRenderContext() + const context = + this._cachedLinkRenderContext ?? this.buildLinkRenderContext() this.linkRenderer.renderLinkDirect( ctx, a, diff --git a/src/lib/litegraph/src/LLink.ts b/src/lib/litegraph/src/LLink.ts index 0015245f838..7b3d108e49b 100644 --- a/src/lib/litegraph/src/LLink.ts +++ b/src/lib/litegraph/src/LLink.ts @@ -329,7 +329,10 @@ export class LLink implements LinkSegment, Serialisable { linkSegment: LinkSegment ): Reroute[] { if (linkSegment.parentId === undefined) return EMPTY_REROUTES - return network.reroutes.get(linkSegment.parentId)?.getReroutes() ?? EMPTY_REROUTES + return ( + network.reroutes.get(linkSegment.parentId)?.getReroutes() ?? + EMPTY_REROUTES + ) } static getFirstReroute( From 8576f6b8a98abbc706c04b7bb2792587ced915c3 Mon Sep 17 00:00:00 2001 From: bymyself Date: Wed, 4 Mar 2026 18:50:17 -0800 Subject: [PATCH 03/10] fix: reset link render context cache at frame start Amp-Thread-ID: https://ampcode.com/threads/T-019cbb8f-0894-7504-87c1-057e8818587e --- src/lib/litegraph/src/LGraphCanvas.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/lib/litegraph/src/LGraphCanvas.ts b/src/lib/litegraph/src/LGraphCanvas.ts index 68ae87e7610..2754a59786c 100644 --- a/src/lib/litegraph/src/LGraphCanvas.ts +++ b/src/lib/litegraph/src/LGraphCanvas.ts @@ -5086,6 +5086,9 @@ export class LGraphCanvas implements CustomEventDispatcher if (!this.canvas || this.canvas.width == 0 || this.canvas.height == 0) return + // Reset per-frame caches so stale data is never used if a code path is skipped. + this._cachedLinkRenderContext = null + // fps counting const now = LiteGraph.getTime() this.render_time = (now - this.last_draw_time) * 0.001 From 9d7ba843b23430d60c9bdc17e45c53b5c996ef11 Mon Sep 17 00:00:00 2001 From: bymyself Date: Sun, 15 Mar 2026 22:15:58 -0700 Subject: [PATCH 04/10] fix: simplify slot iteration with for-of loop Replace indexed slotArrays loop with idiomatic for-of over the two concrete slot arrays. https://github.com/Comfy-Org/ComfyUI_frontend/pull/9405#discussion_r2887926847 --- src/lib/litegraph/src/LGraphNode.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/lib/litegraph/src/LGraphNode.ts b/src/lib/litegraph/src/LGraphNode.ts index 752b55ed6cb..cb3ae5e5d4e 100644 --- a/src/lib/litegraph/src/LGraphNode.ts +++ b/src/lib/litegraph/src/LGraphNode.ts @@ -4307,9 +4307,7 @@ export class LGraphNode ctx: CanvasRenderingContext2D, { fromSlot, colorContext, editorAlpha, lowQuality }: DrawSlotsOptions ) { - const slotArrays = [this._concreteInputs, this._concreteOutputs] - for (let a = 0; a < 2; a++) { - const slots = slotArrays[a] + for (const slots of [this._concreteInputs, this._concreteOutputs]) { for (let s = 0; s < slots.length; s++) { const slot = slots[s] const isValidTarget = fromSlot && slot.isValidTarget(fromSlot) From b51bb5bd14cc9f33a79a3863dacf76f5e1cadd2e Mon Sep 17 00:00:00 2001 From: bymyself Date: Sun, 15 Mar 2026 22:16:02 -0700 Subject: [PATCH 05/10] fix: remove redundant spread in renderDraggingLink call renderDraggingLink already applies linkMarkerShape: None internally, so the spread + override at the call site was unnecessary allocation. https://github.com/Comfy-Org/ComfyUI_frontend/pull/9405#discussion_r2887883450 --- src/lib/litegraph/src/LGraphCanvas.ts | 5 +---- src/lib/litegraph/src/LGraphNode.test.ts | 3 ++- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/lib/litegraph/src/LGraphCanvas.ts b/src/lib/litegraph/src/LGraphCanvas.ts index 2754a59786c..f8c2ccb0843 100644 --- a/src/lib/litegraph/src/LGraphCanvas.ts +++ b/src/lib/litegraph/src/LGraphCanvas.ts @@ -5358,10 +5358,7 @@ export class LGraphCanvas implements CustomEventDispatcher colour, fromDirection, dragDirection, - { - ...(this._cachedLinkRenderContext ?? this.buildLinkRenderContext()), - linkMarkerShape: LinkMarkerShape.None - } + this._cachedLinkRenderContext ?? this.buildLinkRenderContext() ) } if (renderLink instanceof MovingInputLink) this.setDirty(false, true) diff --git a/src/lib/litegraph/src/LGraphNode.test.ts b/src/lib/litegraph/src/LGraphNode.test.ts index 17290877d7d..36f1f700886 100644 --- a/src/lib/litegraph/src/LGraphNode.test.ts +++ b/src/lib/litegraph/src/LGraphNode.test.ts @@ -877,7 +877,6 @@ describe('LGraphNode', () => { expect(out[3]).toBe(120 + LiteGraph.NODE_TITLE_HEIGHT) }) }) -}) describe('_slotsDirty flag', () => { test('starts dirty', () => { const n = new LGraphNode('Test') @@ -943,7 +942,9 @@ describe('LGraphNode', () => { expect(n._slotsDirty).toBe(true) }) }) +}) +describe('snapToGrid', () => { beforeEach(() => { setActivePinia(createTestingPinia({ stubActions: false })) }) From 7e4c68690e9b83062a6092b1b3638c31a2104ff7 Mon Sep 17 00:00:00 2001 From: bymyself Date: Wed, 2 Sep 2026 04:37:10 +0000 Subject: [PATCH 06/10] test: assert clean slot projection behavior Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/9405#discussion_r3909183036 --- src/lib/litegraph/src/LGraphNode.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/litegraph/src/LGraphNode.test.ts b/src/lib/litegraph/src/LGraphNode.test.ts index 36f1f700886..c4a6f38e240 100644 --- a/src/lib/litegraph/src/LGraphNode.test.ts +++ b/src/lib/litegraph/src/LGraphNode.test.ts @@ -895,9 +895,9 @@ describe('LGraphNode', () => { n._setConcreteSlots() expect(n._slotsDirty).toBe(false) - const mapSpy = vi.spyOn(n.inputs, 'map') + const concreteInput = n.inputs[0] n._setConcreteSlots() - expect(mapSpy).not.toHaveBeenCalled() + expect(n.inputs[0]).toBe(concreteInput) }) test('is set by addInput', () => { From 0c2b9dcefbee31b93c657d6358871702719066cf Mon Sep 17 00:00:00 2001 From: bymyself Date: Wed, 2 Sep 2026 04:41:57 +0000 Subject: [PATCH 07/10] fix: invalidate concrete slots for direct mutations Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/9405#discussion_r3909183055 --- src/lib/litegraph/src/LGraphNode.test.ts | 61 +++++++++++++++++++ src/lib/litegraph/src/LGraphNode.ts | 3 +- .../litegraph/src/node/slotDescriptorView.ts | 49 +++++++++++---- 3 files changed, 101 insertions(+), 12 deletions(-) diff --git a/src/lib/litegraph/src/LGraphNode.test.ts b/src/lib/litegraph/src/LGraphNode.test.ts index c4a6f38e240..828753ad051 100644 --- a/src/lib/litegraph/src/LGraphNode.test.ts +++ b/src/lib/litegraph/src/LGraphNode.test.ts @@ -930,6 +930,67 @@ describe('LGraphNode', () => { expect(n._slotsDirty).toBe(true) }) + test('refreshes drawn slots after direct array mutations', () => { + const n = new LGraphNode('Test') + n.addInput('old input', 'number') + n.addOutput('old output', 'number') + n._setConcreteSlots() + + const oldInput = n.inputs[0] + const oldOutput = n.outputs[0] + expect(oldInput).toBeInstanceOf(NodeInputSlot) + expect(oldOutput).toBeInstanceOf(NodeOutputSlot) + if (!(oldInput instanceof NodeInputSlot)) throw new Error('Invalid input') + if (!(oldOutput instanceof NodeOutputSlot)) + throw new Error('Invalid output') + const oldInputDraw = vi.spyOn(oldInput, 'draw') + const oldOutputDraw = vi.spyOn(oldOutput, 'draw') + + n.inputs.splice( + 0, + 1, + new NodeInputSlot({ name: 'new input', type: 'number', link: null }, n) + ) + n.outputs.splice( + 0, + 1, + new NodeOutputSlot( + { name: 'new output', type: 'number', links: null }, + n + ) + ) + expect(n._slotsDirty).toBe(true) + + n._setConcreteSlots() + const newInput = n.inputs[0] + const newOutput = n.outputs[0] + expect(newInput).toBeInstanceOf(NodeInputSlot) + expect(newOutput).toBeInstanceOf(NodeOutputSlot) + if (!(newInput instanceof NodeInputSlot)) throw new Error('Invalid input') + if (!(newOutput instanceof NodeOutputSlot)) + throw new Error('Invalid output') + const newInputDraw = vi + .spyOn(newInput, 'draw') + .mockImplementation(() => {}) + const newOutputDraw = vi + .spyOn(newOutput, 'draw') + .mockImplementation(() => {}) + + n.drawSlots(createMockCanvasRenderingContext2D(), { + colorContext: { + getConnectedColor: () => '#fff', + getDisconnectedColor: () => '#fff' + }, + editorAlpha: 1, + lowQuality: false + }) + + expect(oldInputDraw).not.toHaveBeenCalled() + expect(oldOutputDraw).not.toHaveBeenCalled() + expect(newInputDraw).toHaveBeenCalledOnce() + expect(newOutputDraw).toHaveBeenCalledOnce() + }) + test('is set by configure', () => { const n = new LGraphNode('Test') n._setConcreteSlots() diff --git a/src/lib/litegraph/src/LGraphNode.ts b/src/lib/litegraph/src/LGraphNode.ts index cb3ae5e5d4e..6bf00c2bf55 100644 --- a/src/lib/litegraph/src/LGraphNode.ts +++ b/src/lib/litegraph/src/LGraphNode.ts @@ -63,6 +63,7 @@ import { } from './node/slotLinks' import { createInputSlotView, + createOutputSlotView, resolveInputSlotView } from './node/slotDescriptorView' import { initializeWidgetsView } from './node/widgetsView' @@ -1013,7 +1014,7 @@ export class LGraphNode initializeWidgetsView(this) this._state = createNodeShellState(title, type, this.title_mode) this._inputs = createInputSlotView(this, this._state.inputs) - this._outputs = this._state.outputs + this._outputs = createOutputSlotView(this, this._state.outputs) for (const property of [ 'inputs', 'outputs', diff --git a/src/lib/litegraph/src/node/slotDescriptorView.ts b/src/lib/litegraph/src/node/slotDescriptorView.ts index af519cd9f37..ac9a050353c 100644 --- a/src/lib/litegraph/src/node/slotDescriptorView.ts +++ b/src/lib/litegraph/src/node/slotDescriptorView.ts @@ -1,5 +1,8 @@ import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' -import type { INodeInputSlot } from '@/lib/litegraph/src/interfaces' +import type { + INodeInputSlot, + INodeOutputSlot +} from '@/lib/litegraph/src/interfaces' import { NodeInputSlot } from '@/lib/litegraph/src/node/NodeInputSlot' import { toClass } from '@/lib/litegraph/src/utils/type' @@ -13,21 +16,45 @@ export function createInputSlotView( inputs: INodeInputSlot[] ): INodeInputSlot[] { const assignedViews = new WeakMap() - const view = new Proxy(inputs, { - set(target, property, value: unknown, receiver) { - const input = - isArrayIndex(property) && isInputSlot(value) - ? toClass(NodeInputSlot, value, node) - : value - if (isInputSlot(value) && isInputSlot(input) && value !== input) - assignedViews.set(value, input) - return Reflect.set(target, property, input, receiver) - } + const view = createSlotView(node, inputs, (value) => { + const input = isInputSlot(value) ? toClass(NodeInputSlot, value, node) : value + if (isInputSlot(value) && isInputSlot(input) && value !== input) + assignedViews.set(value, input) + return input }) assignedInputViews.set(view, assignedViews) return view } +export function createOutputSlotView( + node: LGraphNode, + outputs: INodeOutputSlot[] +): INodeOutputSlot[] { + return createSlotView(node, outputs) +} + +function createSlotView( + node: LGraphNode, + slots: T[], + normalize: (value: unknown) => unknown = (value) => value +): T[] { + return new Proxy(slots, { + set(target, property, value: unknown, receiver) { + const nextValue = isArrayIndex(property) ? normalize(value) : value + const changed = Reflect.get(target, property) !== nextValue + const updated = Reflect.set(target, property, nextValue, receiver) + if (updated && changed) node._slotsDirty = true + return updated + }, + deleteProperty(target, property) { + const changed = Reflect.has(target, property) + const updated = Reflect.deleteProperty(target, property) + if (updated && changed) node._slotsDirty = true + return updated + } + }) +} + export function resolveInputSlotView( inputs: INodeInputSlot[], input: INodeInputSlot From 1cc1af37dfcb5257b5fb285016450789ae90989b Mon Sep 17 00:00:00 2001 From: bymyself Date: Wed, 2 Sep 2026 04:43:40 +0000 Subject: [PATCH 08/10] test: cover unresolved reroute parent fallback Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/9405#discussion_r3909183065 --- src/lib/litegraph/src/LLink.test.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lib/litegraph/src/LLink.test.ts b/src/lib/litegraph/src/LLink.test.ts index 65e66b0accc..8d8fd58c72c 100644 --- a/src/lib/litegraph/src/LLink.test.ts +++ b/src/lib/litegraph/src/LLink.test.ts @@ -3,6 +3,7 @@ import { describe, expect } from 'vitest' import { LLink } from '@/lib/litegraph/src/litegraph' import { toLinkId } from '@/types/linkId' import { toNodeId } from '@/types/nodeId' +import { toRerouteId } from '@/types/rerouteId' import { test } from './__fixtures__/testExtensions' @@ -37,10 +38,11 @@ describe('LLink', () => { }) describe('getReroutes', () => { - test('returns the same empty array instance for links without reroutes', () => { + test('returns the same empty array for missing reroutes', () => { const network = { reroutes: new Map() } const link1 = new LLink(toLinkId(1), 'float', 4, 2, 5, 3) const link2 = new LLink(toLinkId(2), 'float', 4, 2, 5, 3) + link2.parentId = toRerouteId(999) const result1 = LLink.getReroutes(network, link1) const result2 = LLink.getReroutes(network, link2) From 2829c7b9d1b5f313bf5c4fc28013b702e054946b Mon Sep 17 00:00:00 2001 From: bymyself Date: Wed, 2 Sep 2026 04:44:52 +0000 Subject: [PATCH 09/10] fix: make shared empty reroutes immutable Addresses review feedback: https://github.com/Comfy-Org/ComfyUI_frontend/pull/9405#discussion_r3909183077 --- src/lib/litegraph/src/LLink.test.ts | 1 + src/lib/litegraph/src/LLink.ts | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/lib/litegraph/src/LLink.test.ts b/src/lib/litegraph/src/LLink.test.ts index 8d8fd58c72c..86a45062abb 100644 --- a/src/lib/litegraph/src/LLink.test.ts +++ b/src/lib/litegraph/src/LLink.test.ts @@ -49,6 +49,7 @@ describe('LLink', () => { expect(result1).toHaveLength(0) expect(result1).toBe(result2) + expect(Object.isFrozen(result1)).toBe(true) }) }) }) diff --git a/src/lib/litegraph/src/LLink.ts b/src/lib/litegraph/src/LLink.ts index 7b3d108e49b..062dc7ee46e 100644 --- a/src/lib/litegraph/src/LLink.ts +++ b/src/lib/litegraph/src/LLink.ts @@ -36,7 +36,7 @@ import { toRaw } from 'vue' export type { LinkId } from '@/types/linkId' -const EMPTY_REROUTES: Reroute[] = [] as Reroute[] +const EMPTY_REROUTES: readonly Reroute[] = Object.freeze([]) export type SerialisedLLinkArray = [ id: number, origin_id: SerializedNodeId, @@ -327,7 +327,7 @@ export class LLink implements LinkSegment, Serialisable { static getReroutes( network: Pick, linkSegment: LinkSegment - ): Reroute[] { + ): readonly Reroute[] { if (linkSegment.parentId === undefined) return EMPTY_REROUTES return ( network.reroutes.get(linkSegment.parentId)?.getReroutes() ?? From 443961f86bf09560a24637d72909ff9365b3ae02 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Wed, 2 Sep 2026 20:03:41 +0000 Subject: [PATCH 10/10] [automated] Apply ESLint and Oxfmt fixes --- src/lib/litegraph/src/node/slotDescriptorView.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lib/litegraph/src/node/slotDescriptorView.ts b/src/lib/litegraph/src/node/slotDescriptorView.ts index ac9a050353c..0657e9c71a9 100644 --- a/src/lib/litegraph/src/node/slotDescriptorView.ts +++ b/src/lib/litegraph/src/node/slotDescriptorView.ts @@ -17,7 +17,9 @@ export function createInputSlotView( ): INodeInputSlot[] { const assignedViews = new WeakMap() const view = createSlotView(node, inputs, (value) => { - const input = isInputSlot(value) ? toClass(NodeInputSlot, value, node) : value + const input = isInputSlot(value) + ? toClass(NodeInputSlot, value, node) + : value if (isInputSlot(value) && isInputSlot(input) && value !== input) assignedViews.set(value, input) return input