diff --git a/src/lib/litegraph/src/LGraph.test.ts b/src/lib/litegraph/src/LGraph.test.ts index 21d3b31dca..b120023a2d 100644 --- a/src/lib/litegraph/src/LGraph.test.ts +++ b/src/lib/litegraph/src/LGraph.test.ts @@ -1,6 +1,6 @@ import { createTestingPinia } from '@pinia/testing' import { setActivePinia } from 'pinia' -import { beforeEach, describe, expect, it, vi } from 'vitest' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import type { NodeId, Subgraph } from '@/lib/litegraph/src/litegraph' import { @@ -529,6 +529,52 @@ describe('Subgraph Definition Garbage Collection', () => { }) }) +describe('beforeChange deprecated onBeforeChange shim', () => { + beforeEach(() => { + LiteGraph.onDeprecationWarning = [] + LiteGraph.alwaysRepeatWarnings = true + }) + + afterEach(() => { + LiteGraph.alwaysRepeatWarnings = false + }) + + it('still invokes a listener assigned to onBeforeChange', () => { + const graph = new LGraph() + const node = new LGraphNode('test') + const onBeforeChange = vi.fn() + graph.onBeforeChange = onBeforeChange + + graph.beforeChange(node) + + expect(onBeforeChange).toHaveBeenCalledWith(graph, node) + }) + + it('warns that onBeforeChange is deprecated when used', () => { + const graph = new LGraph() + const deprecationCallback = vi.fn() + LiteGraph.onDeprecationWarning = [deprecationCallback] + graph.onBeforeChange = vi.fn() + + graph.beforeChange() + + expect(deprecationCallback).toHaveBeenCalledWith( + expect.stringContaining('LGraph.onBeforeChange is deprecated'), + undefined + ) + }) + + it('does not warn when no listener is assigned', () => { + const graph = new LGraph() + const deprecationCallback = vi.fn() + LiteGraph.onDeprecationWarning = [deprecationCallback] + + graph.beforeChange() + + expect(deprecationCallback).not.toHaveBeenCalled() + }) +}) + describe('Legacy LGraph Compatibility Layer', () => { test('can be extended via prototype', ({ expect, minimalGraph }) => { // @ts-expect-error Should always be an error. diff --git a/src/lib/litegraph/src/LGraph.ts b/src/lib/litegraph/src/LGraph.ts index ae8413873a..cc4e1d68be 100644 --- a/src/lib/litegraph/src/LGraph.ts +++ b/src/lib/litegraph/src/LGraph.ts @@ -38,7 +38,6 @@ import type { DefaultConnectionColors, Dictionary, HasBoundingRect, - IContextMenuValue, INodeInputSlot, INodeOutputSlot, LinkNetwork, @@ -56,6 +55,7 @@ import { createBounds, snapPoint } from './measure' +import { warnDeprecated } from './utils/feedback' import { SubgraphInput } from './subgraph/SubgraphInput' import { SubgraphInputNode } from './subgraph/SubgraphInputNode' import { SubgraphOutput } from './subgraph/SubgraphOutput' @@ -331,16 +331,16 @@ export class LGraph onNodeAdded?(node: LGraphNode): void onNodeRemoved?(node: LGraphNode): void onTrigger?: LGraphTriggerHandler + /** + * @deprecated Assign a listener to {@link LGraphCanvas.onBeforeChange} instead. + * This graph-level hook will be removed in a future version. + */ onBeforeChange?(graph: LGraph, info?: LGraphNode): void onAfterChange?(graph: LGraph, info?: LGraphNode | null): void onConnectionChange?(node: LGraphNode): void on_change?(graph: LGraph): void onSerialize?(data: ISerialisedGraph | SerialisableGraph): void onConfigure?(data: ISerialisedGraph | SerialisableGraph): void - onGetNodeMenuOptions?( - options: (IContextMenuValue | null)[], - node: LGraphNode - ): void // @ts-expect-error - Private property type needs fixing private _input_nodes?: LGraphNode[] @@ -1357,7 +1357,12 @@ export class LGraph // used for undo, called before any change is made to the graph beforeChange(info?: LGraphNode): void { - this.onBeforeChange?.(this, info) + if (this.onBeforeChange) { + warnDeprecated( + 'LGraph.onBeforeChange is deprecated and will be removed in a future version. Assign a listener to LGraphCanvas.onBeforeChange instead.' + ) + this.onBeforeChange(this, info) + } this.canvasAction((c) => c.onBeforeChange?.(this)) } diff --git a/src/lib/litegraph/src/LGraphCanvas.ts b/src/lib/litegraph/src/LGraphCanvas.ts index 6269f0f422..9dfc53b250 100644 --- a/src/lib/litegraph/src/LGraphCanvas.ts +++ b/src/lib/litegraph/src/LGraphCanvas.ts @@ -8642,8 +8642,6 @@ export class LGraphCanvas implements CustomEventDispatcher callback: LGraphCanvas.onMenuNodeRemove }) - node.graph?.onGetNodeMenuOptions?.(options, node) - return options }