Skip to content

Commit dd317d8

Browse files
EralChenDymoneLewis
authored andcommitted
feat(vue-node-registry): scope vueNodesMap per LogicFlow instance to support nested scenarios
1 parent 1948e0d commit dd317d8

2 files changed

Lines changed: 43 additions & 13 deletions

File tree

packages/vue-node-registry/src/registry.ts

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import LogicFlow from '@logicflow/core'
1+
import LogicFlow, { GraphModel } from '@logicflow/core'
22
import { VueNodeView } from './view'
33
import { VueNodeModel } from './model'
44

@@ -10,13 +10,32 @@ export type VueNodeConfig = {
1010
effect?: (keyof LogicFlow.PropertiesType)[]
1111
} & Partial<RegisterConfig>
1212

13-
export const vueNodesMap: Record<
14-
string,
15-
{
16-
component: any
17-
effect?: (keyof LogicFlow.PropertiesType)[]
18-
}
19-
> = {}
13+
type VueNodeEntry = {
14+
component: any
15+
effect?: (keyof LogicFlow.PropertiesType)[]
16+
}
17+
18+
// Per-instance map: automatically garbage-collected when the GraphModel is destroyed
19+
const vueNodesMaps = new WeakMap<GraphModel, Map<string, VueNodeEntry>>()
20+
21+
/**
22+
* @deprecated Use {@link getVueNodeConfig} instead for multi-instance support.
23+
* This global map is still populated for backward compatibility but does NOT
24+
* isolate registrations across different LogicFlow instances.
25+
*/
26+
export const vueNodesMap: Record<string, VueNodeEntry> = Object.create(
27+
null,
28+
) as Record<string, VueNodeEntry>
29+
30+
/**
31+
* Retrieve the Vue node configuration scoped to a specific LogicFlow instance.
32+
*/
33+
export function getVueNodeConfig(
34+
type: string,
35+
graphModel: GraphModel,
36+
): VueNodeEntry | undefined {
37+
return vueNodesMaps.get(graphModel)?.get(type)
38+
}
2039

2140
export function register(config: VueNodeConfig, lf: LogicFlow) {
2241
const {
@@ -30,10 +49,19 @@ export function register(config: VueNodeConfig, lf: LogicFlow) {
3049
if (!type) {
3150
throw new Error('You should specify type in config')
3251
}
33-
vueNodesMap[type] = {
34-
component,
35-
effect,
52+
53+
const entry: VueNodeEntry = { component, effect }
54+
55+
// Scope to this LogicFlow instance
56+
let map = vueNodesMaps.get(lf.graphModel)
57+
if (!map) {
58+
map = new Map<string, VueNodeEntry>()
59+
vueNodesMaps.set(lf.graphModel, map)
3660
}
61+
map.set(type, entry)
62+
63+
// Also populate global map for backward compatibility
64+
vueNodesMap[type] = entry
3765

3866
lf.register({
3967
type,

packages/vue-node-registry/src/view.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
isNumber,
1111
} from 'lodash-es'
1212
import { HtmlNode } from '@logicflow/core'
13-
import { vueNodesMap } from './registry'
13+
import { getVueNodeConfig } from './registry'
1414
import { isActive, connect, disconnect } from './teleport'
1515
import { Container } from './components/container'
1616

@@ -87,7 +87,9 @@ export class VueNodeView extends HtmlNode {
8787
})
8888

8989
if (root) {
90-
const { component } = vueNodesMap[model.type]
90+
const nodeConfig = getVueNodeConfig(model.type, graphModel)
91+
if (!nodeConfig) return
92+
const { component } = nodeConfig
9193
if (component) {
9294
if (isVue2) {
9395
const Vue = Vue2 as any

0 commit comments

Comments
 (0)