From 8a3f06e2b707ba8dd78800be3e2d66c8774d6aa3 Mon Sep 17 00:00:00 2001 From: Glary-Bot Date: Wed, 6 May 2026 20:10:43 +0000 Subject: [PATCH] fix: preserve canvas receiver when invoking deferred onSelectionChange MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Synchronous callsites use this.onSelectionChange?.(...) which binds the LGraphCanvas instance as the callback's receiver. The deferred RAF invocation called the saved reference as a bare function, so this would be undefined inside the callback under strict mode — silently changing the contract for an extension-facing callback only on the drag-start path. Use Function.prototype.call so the deferred invocation matches every other onSelectionChange call in the file. Adds a test that pins the receiver contract. --- .../src/LGraphCanvas.dragStartDeferral.test.ts | 12 ++++++++++++ src/lib/litegraph/src/LGraphCanvas.ts | 4 +++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/lib/litegraph/src/LGraphCanvas.dragStartDeferral.test.ts b/src/lib/litegraph/src/LGraphCanvas.dragStartDeferral.test.ts index 281a5b748f..8ec909336d 100644 --- a/src/lib/litegraph/src/LGraphCanvas.dragStartDeferral.test.ts +++ b/src/lib/litegraph/src/LGraphCanvas.dragStartDeferral.test.ts @@ -132,6 +132,18 @@ describe('_startDraggingItems defers onSelectionChange', () => { expect(onSelectionChange).not.toHaveBeenCalled() }) + it('invokes the deferred onSelectionChange with the canvas as receiver', () => { + const receivedThis: unknown[] = [] + canvas.onSelectionChange = function (this: unknown) { + receivedThis.push(this) + } + + canvas['_startDraggingItems'](node, pointer, true) + vi.advanceTimersByTime(16) + + expect(receivedThis).toEqual([canvas]) + }) + it('restores onSelectionChange even when processSelect throws', () => { const onSelectionChange = vi.fn() canvas.onSelectionChange = onSelectionChange diff --git a/src/lib/litegraph/src/LGraphCanvas.ts b/src/lib/litegraph/src/LGraphCanvas.ts index 5c320b9466..8193c392b8 100644 --- a/src/lib/litegraph/src/LGraphCanvas.ts +++ b/src/lib/litegraph/src/LGraphCanvas.ts @@ -3629,7 +3629,9 @@ export class LGraphCanvas implements CustomEventDispatcher this.onSelectionChange = onSelectionChange } if (onSelectionChange && selectionNotified) { - requestAnimationFrame(() => onSelectionChange(this.selected_nodes)) + requestAnimationFrame(() => { + onSelectionChange.call(this, this.selected_nodes) + }) } this.isDragging = true