Line data Source code
1 : import { defineStore } from 'pinia'
2 : import { computed, ref } from 'vue'
3 :
4 : import type { InputSpec as InputSpecV2 } from '@comfyorg/object-info-parser/schemas/nodeDefSchemaV2'
5 : import { getInputSpecType } from '@comfyorg/object-info-parser'
6 : import type { InputSpec as InputSpecV1 } from '@comfyorg/object-info-parser'
7 : import { ComfyWidgets } from '@/scripts/widgets'
8 : import type {
9 : ComfyWidgetConstructor,
10 : CustomComfyWidgetConstructor
11 : } from '@/types/comfy'
12 :
13 : type WidgetConstructor = ComfyWidgetConstructor | CustomComfyWidgetConstructor
14 :
15 2356 : export const useWidgetStore = defineStore('widget', () => {
16 2355 : const coreWidgets = ComfyWidgets
17 2355 : const customWidgets = ref<Map<string, CustomComfyWidgetConstructor>>(
18 : new Map()
19 : )
20 2355 : const widgets = computed<Map<string, WidgetConstructor>>(
21 : () => new Map([...customWidgets.value, ...Object.entries(coreWidgets)])
22 : )
23 :
24 1225 : function inputIsWidget(spec: InputSpecV2 | InputSpecV1) {
25 1225 : const type = Array.isArray(spec) ? getInputSpecType(spec) : spec.type
26 1225 : return widgets.value.has(type)
27 : }
28 :
29 16477 : function registerCustomWidgets(
30 : newWidgets: Record<string, CustomComfyWidgetConstructor> | null | undefined
31 : ) {
32 : // Extensions are untrusted code: `getCustomWidgets` is typed to return
33 : // `Record<string, ...>`, but in practice an extension can resolve it to
34 : // null/undefined. Guard here so a single misbehaving custom node can't
35 : // throw "Cannot convert undefined or null to object" and break app init.
36 16477 : if (!newWidgets) return
37 16477 : for (const [type, widget] of Object.entries(newWidgets)) {
38 28252 : customWidgets.value.set(type, widget)
39 : }
40 : }
41 :
42 2355 : return {
43 : coreWidgets,
44 : widgets,
45 : inputIsWidget,
46 : registerCustomWidgets
47 : }
48 : })
|