Skip to content

Commit 620852b

Browse files
committed
fix(core): watch applyDefault state in useVueFlow scope
1 parent 4ed3acc commit 620852b

File tree

2 files changed

+43
-34
lines changed

2 files changed

+43
-34
lines changed

packages/core/src/composables/useVueFlow.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { toRefs, tryOnScopeDispose } from '@vueuse/core'
22
import type { EffectScope } from 'vue'
3-
import { computed, getCurrentScope, inject, provide, reactive } from 'vue'
3+
import { computed, getCurrentScope, inject, provide, reactive, watch } from 'vue'
44
import { useActions, useGetters, useState } from '~/store'
5-
import type { FlowOptions, FlowProps, State, VueFlowStore } from '~/types'
5+
import type { EdgeChange, FlowOptions, FlowProps, NodeChange, State, VueFlowStore } from '~/types'
66
import { VueFlow } from '~/context'
77
import { warn } from '~/utils'
88

@@ -127,10 +127,36 @@ export function useVueFlow(options?: FlowProps): VueFlowStore {
127127
if (!vueFlow || (vueFlow && id && id !== vueFlow.id)) {
128128
const name = id ?? storage.getId()
129129

130-
vueFlow = storage.create(name, options)
130+
const state = storage.create(name, options)
131+
132+
vueFlow = state
131133

132134
if (scope) {
133135
isParentScope = true
136+
137+
scope.run(() => {
138+
watch(
139+
state.applyDefault,
140+
(shouldApplyDefault) => {
141+
const nodesChangeHandler = (changes: NodeChange[]) => {
142+
state.applyNodeChanges(changes)
143+
}
144+
145+
const edgesChangeHandler = (changes: EdgeChange[]) => {
146+
state.applyEdgeChanges(changes)
147+
}
148+
149+
if (shouldApplyDefault) {
150+
state.onNodesChange(nodesChangeHandler)
151+
state.onEdgesChange(edgesChangeHandler)
152+
} else {
153+
state.hooks.value.nodesChange.off(nodesChangeHandler)
154+
state.hooks.value.edgesChange.off(edgesChangeHandler)
155+
}
156+
},
157+
{ immediate: true },
158+
)
159+
})
134160
}
135161
} else {
136162
// if composable was called with additional options after initialization, overwrite state with the options values

packages/core/src/composables/useWatchProps.ts

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -213,25 +213,6 @@ export function useWatchProps(
213213
}
214214
},
215215
)
216-
217-
watch(
218-
store.applyDefault,
219-
(_, __, onCleanup) => {
220-
if (store.applyDefault.value) {
221-
store.onNodesChange(store.applyNodeChanges)
222-
store.onEdgesChange(store.applyEdgeChanges)
223-
} else {
224-
store.hooks.value.nodesChange.off(store.applyNodeChanges)
225-
store.hooks.value.edgesChange.off(store.applyEdgeChanges)
226-
}
227-
228-
onCleanup(() => {
229-
store.hooks.value.nodesChange.off(store.applyNodeChanges)
230-
store.hooks.value.edgesChange.off(store.applyEdgeChanges)
231-
})
232-
},
233-
{ immediate: true },
234-
)
235216
})
236217
}
237218

@@ -310,18 +291,20 @@ export function useWatchProps(
310291
})
311292
}
312293

313-
;[
314-
watchModelValue,
315-
watchNodesValue,
316-
watchEdgesValue,
317-
watchMinZoom,
318-
watchMaxZoom,
319-
watchTranslateExtent,
320-
watchNodeExtent,
321-
watchApplyDefault,
322-
watchAutoConnect,
323-
watchRest,
324-
].forEach((watch) => watch())
294+
const runAll = () => {
295+
watchModelValue()
296+
watchNodesValue()
297+
watchEdgesValue()
298+
watchMinZoom()
299+
watchMaxZoom()
300+
watchTranslateExtent()
301+
watchNodeExtent()
302+
watchApplyDefault()
303+
watchAutoConnect()
304+
watchRest()
305+
}
306+
307+
runAll()
325308
})
326309

327310
return () => scope.stop()

0 commit comments

Comments
 (0)