Line data Source code
1 : import type { PromotedWidgetSource } from '@/core/graph/subgraph/promotedWidgetTypes'
2 : import type { NodeId } from '@comfyorg/graph-model/nodeId'
3 :
4 : export interface SubgraphPseudoWidget {
5 : name: string
6 : }
7 :
8 : export interface SubgraphPseudoWidgetNode<
9 : TWidget extends SubgraphPseudoWidget
10 : > {
11 : id: string | number
12 : widgets?: TWidget[]
13 : }
14 :
15 : interface SubgraphPseudoWidgetCacheEntry<
16 : TNode extends SubgraphPseudoWidgetNode<TWidget>,
17 : TWidget extends SubgraphPseudoWidget
18 : > {
19 : sourceNodeId: NodeId
20 : sourceWidgetName: string
21 : node: TNode
22 : }
23 :
24 : export interface SubgraphPseudoWidgetCache<
25 : TNode extends SubgraphPseudoWidgetNode<TWidget>,
26 : TWidget extends SubgraphPseudoWidget
27 : > {
28 : promotions: readonly PromotedWidgetSource[]
29 : entries: SubgraphPseudoWidgetCacheEntry<TNode, TWidget>[]
30 : nodes: TNode[]
31 : }
32 :
33 : interface ResolveSubgraphPseudoWidgetCacheArgs<
34 : TNode extends SubgraphPseudoWidgetNode<TWidget>,
35 : TWidget extends SubgraphPseudoWidget
36 : > {
37 : cache: SubgraphPseudoWidgetCache<TNode, TWidget> | null
38 : promotions: readonly PromotedWidgetSource[]
39 : getNodeById: (nodeId: NodeId) => TNode | undefined
40 : isPreviewPseudoWidget: (widget: TWidget) => boolean
41 : }
42 :
43 : interface ResolveSubgraphPseudoWidgetCacheResult<
44 : TNode extends SubgraphPseudoWidgetNode<TWidget>,
45 : TWidget extends SubgraphPseudoWidget
46 : > {
47 : cache: SubgraphPseudoWidgetCache<TNode, TWidget>
48 : nodes: TNode[]
49 : }
50 :
51 539 : function isPseudoPromotion<TWidget extends SubgraphPseudoWidget>(
52 : widgetName: string,
53 : widgets: TWidget[] | undefined,
54 : isPreviewPseudoWidget: (widget: TWidget) => boolean
55 : ): boolean {
56 539 : const widget = widgets?.find((candidate) => candidate.name === widgetName)
57 539 : if (widget) return isPreviewPseudoWidget(widget)
58 539 : return widgetName.startsWith('$$')
59 : }
60 :
61 0 : function isCacheStillValid<
62 : TNode extends SubgraphPseudoWidgetNode<TWidget>,
63 : TWidget extends SubgraphPseudoWidget
64 : >(
65 : cache: SubgraphPseudoWidgetCache<TNode, TWidget>,
66 : getNodeById: (nodeId: NodeId) => TNode | undefined,
67 : isPreviewPseudoWidget: (widget: TWidget) => boolean
68 : ): boolean {
69 0 : return cache.entries.every((entry) => {
70 0 : const currentNode = getNodeById(entry.sourceNodeId)
71 0 : if (!currentNode || currentNode !== entry.node) return false
72 0 : return isPseudoPromotion(
73 : entry.sourceWidgetName,
74 : currentNode.widgets,
75 : isPreviewPseudoWidget
76 : )
77 : })
78 : }
79 :
80 837 : export function resolveSubgraphPseudoWidgetCache<
81 : TNode extends SubgraphPseudoWidgetNode<TWidget>,
82 : TWidget extends SubgraphPseudoWidget
83 : >(
84 : args: ResolveSubgraphPseudoWidgetCacheArgs<TNode, TWidget>
85 : ): ResolveSubgraphPseudoWidgetCacheResult<TNode, TWidget> {
86 837 : const { cache, promotions, getNodeById, isPreviewPseudoWidget } = args
87 :
88 837 : if (
89 : cache &&
90 : cache.promotions === promotions &&
91 : isCacheStillValid(cache, getNodeById, isPreviewPseudoWidget)
92 : )
93 0 : return { cache, nodes: cache.nodes }
94 :
95 837 : const entries = promotions.flatMap((promotion) => {
96 553 : const node = getNodeById(promotion.sourceNodeId)
97 553 : if (!node) return []
98 553 : if (
99 : !isPseudoPromotion(
100 : promotion.sourceWidgetName,
101 : node.widgets,
102 : isPreviewPseudoWidget
103 : )
104 : )
105 0 : return []
106 539 : return [{ ...promotion, node }]
107 : })
108 :
109 837 : const nextCache: SubgraphPseudoWidgetCache<TNode, TWidget> = {
110 : promotions,
111 : entries,
112 : nodes: entries.map((entry) => entry.node)
113 : }
114 837 : return {
115 : cache: nextCache,
116 : nodes: nextCache.nodes
117 : }
118 : }
|