From f1cb6dad2048de0ffca8e8810f7a51d9190d5fa7 Mon Sep 17 00:00:00 2001 From: Rizumu Ayaka Date: Thu, 13 Nov 2025 20:51:11 +0800 Subject: [PATCH] feat: send onWidgets event --- src/extensions/dispatch.ts | 4 +++ src/extensions/utils.ts | 1 - src/services/litegraphService.ts | 55 +++++++++++++++++++++++++------- 3 files changed, 48 insertions(+), 12 deletions(-) diff --git a/src/extensions/dispatch.ts b/src/extensions/dispatch.ts index 6a94dd92a..ba88fab1c 100644 --- a/src/extensions/dispatch.ts +++ b/src/extensions/dispatch.ts @@ -73,6 +73,10 @@ export async function importExtensionsByEvent(event: string) { await Promise.all([...callbacks].map((cb) => cb({ event }))) } +export function extentionsImportEventHas(event: string) { + return eventMap.has(event) +} + function onceExtImportEvent(event: string, callback: EventCallback) { if (eventMap.has(event)) { eventMap.get(event)!.add(callback) diff --git a/src/extensions/utils.ts b/src/extensions/utils.ts index 3d773da53..0741e414d 100644 --- a/src/extensions/utils.ts +++ b/src/extensions/utils.ts @@ -81,7 +81,6 @@ export function normalizationActivationEvents( if (activationEvents.includes('onWidgets:contributes')) { for (const contribute of contributes) { - events.push(`onWidgets:${contribute.name}`) if (contribute.widgets) { for (const widget of contribute.widgets) { events.push(`onWidgets:${widget}`) diff --git a/src/services/litegraphService.ts b/src/services/litegraphService.ts index 35562533b..074226a3d 100644 --- a/src/services/litegraphService.ts +++ b/src/services/litegraphService.ts @@ -58,10 +58,44 @@ import { import { getOrderedInputSpecs } from '@/workbench/utils/nodeDefOrderingUtil' import { useExtensionService } from './extensionService' +import { + extentionsImportEventHas, + importExtensionsByEvent +} from '@/extensions/dispatch' export const CONFIG = Symbol() export const GET_CONFIG = Symbol() +function addInputsAndimportWidgetsAsNeeded(options: { + orderedInputSpecs: InputSpec[] + addInputSocket: (inputSpec: InputSpec) => void + addInputWidget: (inputSpec: InputSpec) => void +}) { + const { orderedInputSpecs, addInputSocket, addInputWidget } = options + const awaitedInputSpecs: InputSpec[] = [] + const syncInputSpecs: InputSpec[] = [] + const importJobs: Promise[] = [] + for (const inputSpec of orderedInputSpecs) { + const widgetType = inputSpec.widgetType ?? inputSpec.type + if (extentionsImportEventHas(`onWidgets:${widgetType}`)) { + importJobs.push(importExtensionsByEvent(`onWidgets:${widgetType}`)) + awaitedInputSpecs.push(inputSpec) + } else { + syncInputSpecs.push(inputSpec) + } + } + + ;(async () => { + await Promise.all(importJobs) + for (const inputSpec of awaitedInputSpecs) addInputSocket(inputSpec) + for (const inputSpec of awaitedInputSpecs) addInputWidget(inputSpec) + })() + + // Create sockets and widgets in the determined order + for (const inputSpec of syncInputSpecs) addInputSocket(inputSpec) + for (const inputSpec of syncInputSpecs) addInputWidget(inputSpec) +} + /** * Service that augments litegraph with ComfyUI specific functionality. */ @@ -244,12 +278,11 @@ export const useLitegraphService = () => { // Use input_order if available to ensure consistent widget ordering const nodeDefImpl = ComfyNode.nodeData as ComfyNodeDefImpl const orderedInputSpecs = getOrderedInputSpecs(nodeDefImpl, inputs) - - // Create sockets and widgets in the determined order - for (const inputSpec of orderedInputSpecs) - this.#addInputSocket(inputSpec) - for (const inputSpec of orderedInputSpecs) - this.#addInputWidget(inputSpec) + addInputsAndimportWidgetsAsNeeded({ + orderedInputSpecs, + addInputSocket: this.#addInputSocket.bind(this), + addInputWidget: this.#addInputWidget.bind(this) + }) } /** @@ -521,11 +554,11 @@ export const useLitegraphService = () => { const nodeDefImpl = ComfyNode.nodeData as ComfyNodeDefImpl const orderedInputSpecs = getOrderedInputSpecs(nodeDefImpl, inputs) - // Create sockets and widgets in the determined order - for (const inputSpec of orderedInputSpecs) - this.#addInputSocket(inputSpec) - for (const inputSpec of orderedInputSpecs) - this.#addInputWidget(inputSpec) + addInputsAndimportWidgetsAsNeeded({ + orderedInputSpecs, + addInputSocket: this.#addInputSocket.bind(this), + addInputWidget: this.#addInputWidget.bind(this) + }) } /**