From 3f7fa17107cd99d6f41d88cfda8ed772a4bed089 Mon Sep 17 00:00:00 2001 From: relhamdi Date: Sat, 30 May 2026 19:04:41 +0200 Subject: [PATCH] Add new registerNode waitForWidget waitForWidgets functions --- web/utils.js | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/web/utils.js b/web/utils.js index 2eceb77..edc4f94 100644 --- a/web/utils.js +++ b/web/utils.js @@ -1,4 +1,5 @@ import { COLORS } from "./constants"; +import { app } from "/scripts/app.js"; // Hide ComfyUI widget component export const hideWidget = (widgetName) => { @@ -314,3 +315,49 @@ export const debounce = (fn, ms = 64) => { timer = setTimeout(() => fn(...args), ms); }; }; + +// --- Registration --- + +export const registerNode = (name, attachFn) => { + app.registerExtension({ + name, + beforeRegisterNodeDef(nodeType, nodeData) { + if (nodeData.name !== name) return; + const original = nodeType.prototype.onNodeCreated; + nodeType.prototype.onNodeCreated = function () { + if (original) original.call(this); + attachFn(this); + }; + }, + }); +}; + +export const waitForWidget = (node, widgetName, callback) => { + const guardKey = `_attached_${widgetName}`; + if (node[guardKey]) return; + node[guardKey] = true; + + const tryAttach = () => { + const widget = node.widgets?.find((w) => w.name === widgetName); + if (widget?.inputEl?.parentNode) { + callback(node); + } else { + requestAnimationFrame(tryAttach); + } + }; + requestAnimationFrame(tryAttach); +}; + +export const waitForWidgets = (node, callback) => { + if (node._attached) return; + node._attached = true; + + const tryAttach = () => { + if (node.widgets?.length) { + callback(node); + } else { + requestAnimationFrame(tryAttach); + } + }; + requestAnimationFrame(tryAttach); +};