Skip to content

Commit f1cb6da

Browse files
LittleSoundDrJKL
authored andcommitted
feat: send onWidgets event
1 parent f1856b7 commit f1cb6da

File tree

3 files changed

+48
-12
lines changed

3 files changed

+48
-12
lines changed

src/extensions/dispatch.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ export async function importExtensionsByEvent(event: string) {
7373
await Promise.all([...callbacks].map((cb) => cb({ event })))
7474
}
7575

76+
export function extentionsImportEventHas(event: string) {
77+
return eventMap.has(event)
78+
}
79+
7680
function onceExtImportEvent(event: string, callback: EventCallback) {
7781
if (eventMap.has(event)) {
7882
eventMap.get(event)!.add(callback)

src/extensions/utils.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ export function normalizationActivationEvents(
8181

8282
if (activationEvents.includes('onWidgets:contributes')) {
8383
for (const contribute of contributes) {
84-
events.push(`onWidgets:${contribute.name}`)
8584
if (contribute.widgets) {
8685
for (const widget of contribute.widgets) {
8786
events.push(`onWidgets:${widget}`)

src/services/litegraphService.ts

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,44 @@ import {
5858
import { getOrderedInputSpecs } from '@/workbench/utils/nodeDefOrderingUtil'
5959

6060
import { useExtensionService } from './extensionService'
61+
import {
62+
extentionsImportEventHas,
63+
importExtensionsByEvent
64+
} from '@/extensions/dispatch'
6165

6266
export const CONFIG = Symbol()
6367
export const GET_CONFIG = Symbol()
6468

69+
function addInputsAndimportWidgetsAsNeeded(options: {
70+
orderedInputSpecs: InputSpec[]
71+
addInputSocket: (inputSpec: InputSpec) => void
72+
addInputWidget: (inputSpec: InputSpec) => void
73+
}) {
74+
const { orderedInputSpecs, addInputSocket, addInputWidget } = options
75+
const awaitedInputSpecs: InputSpec[] = []
76+
const syncInputSpecs: InputSpec[] = []
77+
const importJobs: Promise<void>[] = []
78+
for (const inputSpec of orderedInputSpecs) {
79+
const widgetType = inputSpec.widgetType ?? inputSpec.type
80+
if (extentionsImportEventHas(`onWidgets:${widgetType}`)) {
81+
importJobs.push(importExtensionsByEvent(`onWidgets:${widgetType}`))
82+
awaitedInputSpecs.push(inputSpec)
83+
} else {
84+
syncInputSpecs.push(inputSpec)
85+
}
86+
}
87+
88+
;(async () => {
89+
await Promise.all(importJobs)
90+
for (const inputSpec of awaitedInputSpecs) addInputSocket(inputSpec)
91+
for (const inputSpec of awaitedInputSpecs) addInputWidget(inputSpec)
92+
})()
93+
94+
// Create sockets and widgets in the determined order
95+
for (const inputSpec of syncInputSpecs) addInputSocket(inputSpec)
96+
for (const inputSpec of syncInputSpecs) addInputWidget(inputSpec)
97+
}
98+
6599
/**
66100
* Service that augments litegraph with ComfyUI specific functionality.
67101
*/
@@ -244,12 +278,11 @@ export const useLitegraphService = () => {
244278
// Use input_order if available to ensure consistent widget ordering
245279
const nodeDefImpl = ComfyNode.nodeData as ComfyNodeDefImpl
246280
const orderedInputSpecs = getOrderedInputSpecs(nodeDefImpl, inputs)
247-
248-
// Create sockets and widgets in the determined order
249-
for (const inputSpec of orderedInputSpecs)
250-
this.#addInputSocket(inputSpec)
251-
for (const inputSpec of orderedInputSpecs)
252-
this.#addInputWidget(inputSpec)
281+
addInputsAndimportWidgetsAsNeeded({
282+
orderedInputSpecs,
283+
addInputSocket: this.#addInputSocket.bind(this),
284+
addInputWidget: this.#addInputWidget.bind(this)
285+
})
253286
}
254287

255288
/**
@@ -521,11 +554,11 @@ export const useLitegraphService = () => {
521554
const nodeDefImpl = ComfyNode.nodeData as ComfyNodeDefImpl
522555
const orderedInputSpecs = getOrderedInputSpecs(nodeDefImpl, inputs)
523556

524-
// Create sockets and widgets in the determined order
525-
for (const inputSpec of orderedInputSpecs)
526-
this.#addInputSocket(inputSpec)
527-
for (const inputSpec of orderedInputSpecs)
528-
this.#addInputWidget(inputSpec)
557+
addInputsAndimportWidgetsAsNeeded({
558+
orderedInputSpecs,
559+
addInputSocket: this.#addInputSocket.bind(this),
560+
addInputWidget: this.#addInputWidget.bind(this)
561+
})
529562
}
530563

531564
/**

0 commit comments

Comments
 (0)