Skip to content

Commit e067437

Browse files
committed
[refactor] Type createNode options parameter (#9262)
## Summary Narrow `CreateNodeOptions` from `Partial<Omit<LGraphNode, ...>>` (exposing hundreds of properties/methods) to an explicit interface listing only creation-time properties. ## Changes - Replace `Partial<Omit<LGraphNode, 'constructor' | 'inputs' | 'outputs'>>` with explicit `CreateNodeOptions` interface containing only: `pos`, `size`, `properties`, `flags`, `mode`, `color`, `bgcolor`, `boxcolor`, `title`, `shape`, `inputs`, `outputs` - Rename local `CreateNodeOptions` in `createModelNodeFromAsset.ts` to `ModelNodeCreateOptions` to avoid collision ## Ecosystem verification GitHub code search across ~50 repos confirms only `pos` and `outputs` are used externally. All covered by the narrowed interface. Fixes #9276 Fixes #4740
1 parent c0368ca commit e067437

File tree

6 files changed

+37
-12
lines changed

6 files changed

+37
-12
lines changed

src/lib/litegraph/src/LGraphCanvas.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6573,7 +6573,7 @@ export class LGraphCanvas implements CustomEventDispatcher<LGraphCanvasEventMap>
65736573
const ySizeFix = opts.posSizeFix[1] * LiteGraph.NODE_SLOT_HEIGHT
65746574
const nodeX = opts.position[0] + opts.posAdd[0] + xSizeFix
65756575
const nodeY = opts.position[1] + opts.posAdd[1] + ySizeFix
6576-
const pos = [nodeX, nodeY]
6576+
const pos: [number, number] = [nodeX, nodeY]
65776577
const newNode = LiteGraph.createNode(nodeTypeStr, nodeNewOpts?.title, {
65786578
pos
65796579
})

src/lib/litegraph/src/LiteGraphGlobal.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ import { Reroute } from './Reroute'
1010
import { InputIndicators } from './canvas/InputIndicators'
1111
import { LabelPosition, SlotDirection, SlotShape, SlotType } from './draw'
1212
import { Rectangle } from './infrastructure/Rectangle'
13-
import type { Dictionary, ISlotType, Rect, WhenNullish } from './interfaces'
13+
import type {
14+
CreateNodeOptions,
15+
Dictionary,
16+
ISlotType,
17+
Rect,
18+
WhenNullish
19+
} from './interfaces'
1420
import { distance, isInsideRectangle, overlapBounding } from './measure'
1521
import { SubgraphIONodeBase } from './subgraph/SubgraphIONodeBase'
1622
import { SubgraphSlot } from './subgraph/SubgraphSlotBase'
@@ -525,7 +531,7 @@ export class LiteGraphGlobal {
525531
createNode(
526532
type: string,
527533
title?: string,
528-
options?: Dictionary<unknown>
534+
options?: CreateNodeOptions
529535
): LGraphNode | null {
530536
const base_class = this.registered_node_types[type]
531537
if (!base_class) {
@@ -561,10 +567,7 @@ export class LiteGraphGlobal {
561567

562568
// extra options
563569
if (options) {
564-
for (const i in options) {
565-
// @ts-expect-error #577 Requires interface
566-
node[i] = options[i]
567-
}
570+
Object.assign(node, options)
568571
}
569572

570573
// callback

src/lib/litegraph/src/interfaces.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ import type { CanvasPointerEvent } from '@/lib/litegraph/src/types/events'
33
import type { TWidgetValue } from '@/lib/litegraph/src/types/widgets'
44

55
import type { ContextMenu } from './ContextMenu'
6-
import type { LGraphNode, NodeId } from './LGraphNode'
6+
import type { LGraphNode, NodeId, NodeProperty } from './LGraphNode'
77
import type { LLink, LinkId } from './LLink'
88
import type { Reroute, RerouteId } from './Reroute'
99
import type { SubgraphInput } from './subgraph/SubgraphInput'
1010
import type { SubgraphInputNode } from './subgraph/SubgraphInputNode'
1111
import type { SubgraphOutputNode } from './subgraph/SubgraphOutputNode'
12-
import type { LinkDirection, RenderShape } from './types/globalEnums'
12+
import type {
13+
LGraphEventMode,
14+
LinkDirection,
15+
RenderShape
16+
} from './types/globalEnums'
1317
import type { IBaseWidget } from './types/widgets'
1418

1519
export type Dictionary<T> = { [key: string]: T }
@@ -373,6 +377,22 @@ export interface INodeOutputSlot extends INodeSlot {
373377
slot_index?: number
374378
}
375379

380+
/** Options for {@link LiteGraphGlobal.createNode}. Shallow-copied onto the new node. */
381+
export interface CreateNodeOptions {
382+
pos?: Point
383+
size?: Size
384+
properties?: Dictionary<NodeProperty | undefined>
385+
flags?: Partial<INodeFlags>
386+
mode?: LGraphEventMode
387+
color?: string
388+
bgcolor?: string
389+
boxcolor?: string
390+
title?: string
391+
shape?: RenderShape
392+
inputs?: Partial<INodeInputSlot>[]
393+
outputs?: Partial<INodeOutputSlot>[]
394+
}
395+
376396
/** Links */
377397
export interface ConnectingLink extends IInputOrOutput {
378398
node: LGraphNode

src/lib/litegraph/src/litegraph.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export { RecursionError } from './infrastructure/RecursionError'
9191
export type {
9292
CanvasColour,
9393
ColorOption,
94+
CreateNodeOptions,
9495
IContextMenuOptions,
9596
IContextMenuValue,
9697
INodeInputSlot,

src/platform/assets/utils/createModelNodeFromAsset.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { app } from '@/scripts/app'
1212
import { useLitegraphService } from '@/services/litegraphService'
1313
import { useModelToNodeStore } from '@/stores/modelToNodeStore'
1414

15-
interface CreateNodeOptions {
15+
interface ModelNodeCreateOptions {
1616
position?: Point
1717
}
1818

@@ -48,7 +48,7 @@ type Result<T, E> = { success: true; value: T } | { success: false; error: E }
4848
*/
4949
export function createModelNodeFromAsset(
5050
asset: AssetItem,
51-
options?: CreateNodeOptions
51+
options?: ModelNodeCreateOptions
5252
): Result<LGraphNode, NodeCreationError> {
5353
const validatedAsset = assetItemSchema.safeParse(asset)
5454

src/services/litegraphService.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
createBounds
2323
} from '@/lib/litegraph/src/litegraph'
2424
import type {
25+
CreateNodeOptions,
2526
GraphAddOptions,
2627
IContextMenuValue,
2728
Point,
@@ -885,7 +886,7 @@ export const useLitegraphService = () => {
885886

886887
function addNodeOnGraph(
887888
nodeDef: ComfyNodeDefV1 | ComfyNodeDefV2,
888-
options: Record<string, unknown> & { pos?: Point } = {},
889+
options: CreateNodeOptions = {},
889890
addOptions?: GraphAddOptions
890891
): LGraphNode | null {
891892
options.pos ??= getCanvasCenter()

0 commit comments

Comments
 (0)