fix(core): watch applyDefault state in useVueFlow scope

This commit is contained in:
braks
2023-07-24 23:46:59 +02:00
committed by Braks
parent 4ed3acc021
commit 620852b860
2 changed files with 43 additions and 34 deletions
+29 -3
View File
@@ -1,8 +1,8 @@
import { toRefs, tryOnScopeDispose } from '@vueuse/core' import { toRefs, tryOnScopeDispose } from '@vueuse/core'
import type { EffectScope } from 'vue' import type { EffectScope } from 'vue'
import { computed, getCurrentScope, inject, provide, reactive } from 'vue' import { computed, getCurrentScope, inject, provide, reactive, watch } from 'vue'
import { useActions, useGetters, useState } from '~/store' import { useActions, useGetters, useState } from '~/store'
import type { FlowOptions, FlowProps, State, VueFlowStore } from '~/types' import type { EdgeChange, FlowOptions, FlowProps, NodeChange, State, VueFlowStore } from '~/types'
import { VueFlow } from '~/context' import { VueFlow } from '~/context'
import { warn } from '~/utils' import { warn } from '~/utils'
@@ -127,10 +127,36 @@ export function useVueFlow(options?: FlowProps): VueFlowStore {
if (!vueFlow || (vueFlow && id && id !== vueFlow.id)) { if (!vueFlow || (vueFlow && id && id !== vueFlow.id)) {
const name = id ?? storage.getId() const name = id ?? storage.getId()
vueFlow = storage.create(name, options) const state = storage.create(name, options)
vueFlow = state
if (scope) { if (scope) {
isParentScope = true isParentScope = true
scope.run(() => {
watch(
state.applyDefault,
(shouldApplyDefault) => {
const nodesChangeHandler = (changes: NodeChange[]) => {
state.applyNodeChanges(changes)
}
const edgesChangeHandler = (changes: EdgeChange[]) => {
state.applyEdgeChanges(changes)
}
if (shouldApplyDefault) {
state.onNodesChange(nodesChangeHandler)
state.onEdgesChange(edgesChangeHandler)
} else {
state.hooks.value.nodesChange.off(nodesChangeHandler)
state.hooks.value.edgesChange.off(edgesChangeHandler)
}
},
{ immediate: true },
)
})
} }
} else { } else {
// if composable was called with additional options after initialization, overwrite state with the options values // if composable was called with additional options after initialization, overwrite state with the options values
+14 -31
View File
@@ -213,25 +213,6 @@ export function useWatchProps(
} }
}, },
) )
watch(
store.applyDefault,
(_, __, onCleanup) => {
if (store.applyDefault.value) {
store.onNodesChange(store.applyNodeChanges)
store.onEdgesChange(store.applyEdgeChanges)
} else {
store.hooks.value.nodesChange.off(store.applyNodeChanges)
store.hooks.value.edgesChange.off(store.applyEdgeChanges)
}
onCleanup(() => {
store.hooks.value.nodesChange.off(store.applyNodeChanges)
store.hooks.value.edgesChange.off(store.applyEdgeChanges)
})
},
{ immediate: true },
)
}) })
} }
@@ -310,18 +291,20 @@ export function useWatchProps(
}) })
} }
;[ const runAll = () => {
watchModelValue, watchModelValue()
watchNodesValue, watchNodesValue()
watchEdgesValue, watchEdgesValue()
watchMinZoom, watchMinZoom()
watchMaxZoom, watchMaxZoom()
watchTranslateExtent, watchTranslateExtent()
watchNodeExtent, watchNodeExtent()
watchApplyDefault, watchApplyDefault()
watchAutoConnect, watchAutoConnect()
watchRest, watchRest()
].forEach((watch) => watch()) }
runAll()
}) })
return () => scope.stop() return () => scope.stop()