From c30e1faa22b8aa3ff712e0fa4c7007f175072cd9 Mon Sep 17 00:00:00 2001 From: bymyself Date: Thu, 13 Nov 2025 10:05:29 -0800 Subject: [PATCH] refactor --- src/composables/useCoreCommands.ts | 6 +-- .../graph/subgraph/proxyWidgetUtils.ts | 6 +-- src/services/litegraphService.ts | 29 +------------ src/services/widgetPromotionHandlers.ts | 41 +++++++++++++++++++ 4 files changed, 47 insertions(+), 35 deletions(-) create mode 100644 src/services/widgetPromotionHandlers.ts diff --git a/src/composables/useCoreCommands.ts b/src/composables/useCoreCommands.ts index a0b9d04b4..7a18fe00f 100644 --- a/src/composables/useCoreCommands.ts +++ b/src/composables/useCoreCommands.ts @@ -35,10 +35,8 @@ import { selectionBounds } from '@/renderer/core/layout/utils/layoutMath' import { api } from '@/scripts/api' import { app } from '@/scripts/app' import { useDialogService } from '@/services/dialogService' -import { - invokeToggleWidgetPromotion, - useLitegraphService -} from '@/services/litegraphService' +import { useLitegraphService } from '@/services/litegraphService' +import { invokeToggleWidgetPromotion } from '@/services/widgetPromotionHandlers' import type { ComfyCommand } from '@/stores/commandStore' import { useExecutionStore } from '@/stores/executionStore' import { useHelpCenterStore } from '@/stores/helpCenterStore' diff --git a/src/renderer/graph/subgraph/proxyWidgetUtils.ts b/src/renderer/graph/subgraph/proxyWidgetUtils.ts index 2b33adf15..162ae44ee 100644 --- a/src/renderer/graph/subgraph/proxyWidgetUtils.ts +++ b/src/renderer/graph/subgraph/proxyWidgetUtils.ts @@ -13,10 +13,8 @@ import type { SubgraphNode } from '@/lib/litegraph/src/subgraph/SubgraphNode' import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets.ts' import { useToastStore } from '@/platform/updates/common/toastStore' import { useCanvasStore } from '@/renderer/core/canvas/canvasStore' -import { - registerWidgetPromotionHandlers, - useLitegraphService -} from '@/services/litegraphService' +import { useLitegraphService } from '@/services/litegraphService' +import { registerWidgetPromotionHandlers } from '@/services/widgetPromotionHandlers' import { useSubgraphNavigationStore } from '@/stores/subgraphNavigationStore' type PartialNode = Pick diff --git a/src/services/litegraphService.ts b/src/services/litegraphService.ts index 1d61d3e50..33ab9197f 100644 --- a/src/services/litegraphService.ts +++ b/src/services/litegraphService.ts @@ -21,7 +21,6 @@ import type { Point, Subgraph } from '@/lib/litegraph/src/litegraph' -import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' import type { ExportedSubgraphInstance, ISerialisableNodeInput, @@ -58,31 +57,11 @@ import { import { getOrderedInputSpecs } from '@/utils/nodeDefOrderingUtil' import { useExtensionService } from './extensionService' +import { addWidgetPromotionOptions as invokeAddWidgetPromotionOptions } from './widgetPromotionHandlers' export const CONFIG = Symbol() export const GET_CONFIG = Symbol() -type WidgetPromotionHandlers = { - addWidgetPromotionOptions?: ( - options: (IContextMenuValue | null)[], - widget: IBaseWidget, - node: LGraphNode - ) => void - tryToggleWidgetPromotion?: () => void -} - -let widgetPromotionHandlers: WidgetPromotionHandlers = {} - -export const registerWidgetPromotionHandlers = ( - handlers: WidgetPromotionHandlers -) => { - widgetPromotionHandlers = handlers -} - -export const invokeToggleWidgetPromotion = () => { - widgetPromotionHandlers.tryToggleWidgetPromotion?.() -} - /** * Service that augments litegraph with ComfyUI specific functionality. */ @@ -847,11 +826,7 @@ export const useLitegraphService = () => { const [x, y] = canvas.graph_mouse const overWidget = this.getWidgetOnPos(x, y, true) if (overWidget) { - widgetPromotionHandlers.addWidgetPromotionOptions?.( - options, - overWidget, - this - ) + invokeAddWidgetPromotionOptions(options, overWidget, this) } } diff --git a/src/services/widgetPromotionHandlers.ts b/src/services/widgetPromotionHandlers.ts new file mode 100644 index 000000000..8a1a2e1e1 --- /dev/null +++ b/src/services/widgetPromotionHandlers.ts @@ -0,0 +1,41 @@ +import { shallowRef } from 'vue' + +import type { + IContextMenuValue, + LGraphNode +} from '@/lib/litegraph/src/litegraph' +import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' + +type WidgetPromotionHandlers = { + addWidgetPromotionOptions?: ( + options: (IContextMenuValue | null)[], + widget: IBaseWidget, + node: LGraphNode + ) => void + tryToggleWidgetPromotion?: () => void +} + +const handlersRef = shallowRef({}) + +export const registerWidgetPromotionHandlers = ( + handlers: WidgetPromotionHandlers +) => { + handlersRef.value = handlers + return () => { + if (handlersRef.value === handlers) { + handlersRef.value = {} + } + } +} + +export const invokeToggleWidgetPromotion = () => { + handlersRef.value.tryToggleWidgetPromotion?.() +} + +export const addWidgetPromotionOptions = ( + options: (IContextMenuValue | null)[], + widget: IBaseWidget, + node: LGraphNode +) => { + handlersRef.value.addWidgetPromotionOptions?.(options, widget, node) +}