fix(core): use detached scope to dispose state and stop watcher
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
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, watch } from 'vue'
|
import { computed, effectScope, getCurrentScope, inject, provide, reactive, watch } from 'vue'
|
||||||
import { useActions, useGetters, useState } from '~/store'
|
import { useActions, useGetters, useState } from '~/store'
|
||||||
import type { EdgeChange, FlowOptions, FlowProps, NodeChange, State, VueFlowStore } from '~/types'
|
import type { EdgeChange, FlowOptions, FlowProps, NodeChange, State, VueFlowStore } from '~/types'
|
||||||
import { VueFlow } from '~/context'
|
import { VueFlow } from '~/context'
|
||||||
@@ -96,8 +96,6 @@ export function useVueFlow(options?: FlowProps): VueFlowStore {
|
|||||||
|
|
||||||
let vueFlow: Injection
|
let vueFlow: Injection
|
||||||
|
|
||||||
let isParentScope = false
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* check if we can get a store instance through injections
|
* check if we can get a store instance through injections
|
||||||
* this should be the regular way after initialization
|
* this should be the regular way after initialization
|
||||||
@@ -131,47 +129,31 @@ export function useVueFlow(options?: FlowProps): VueFlowStore {
|
|||||||
|
|
||||||
vueFlow = state
|
vueFlow = state
|
||||||
|
|
||||||
if (scope) {
|
const detachedScope = effectScope()
|
||||||
isParentScope = true
|
|
||||||
|
|
||||||
scope.run(() => {
|
detachedScope.run(() => {
|
||||||
watch(
|
watch(
|
||||||
state.applyDefault,
|
state.applyDefault,
|
||||||
(shouldApplyDefault) => {
|
(shouldApplyDefault) => {
|
||||||
const nodesChangeHandler = (changes: NodeChange[]) => {
|
const nodesChangeHandler = (changes: NodeChange[]) => {
|
||||||
state.applyNodeChanges(changes)
|
state.applyNodeChanges(changes)
|
||||||
}
|
}
|
||||||
|
|
||||||
const edgesChangeHandler = (changes: EdgeChange[]) => {
|
const edgesChangeHandler = (changes: EdgeChange[]) => {
|
||||||
state.applyEdgeChanges(changes)
|
state.applyEdgeChanges(changes)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (shouldApplyDefault) {
|
if (shouldApplyDefault) {
|
||||||
state.onNodesChange(nodesChangeHandler)
|
state.onNodesChange(nodesChangeHandler)
|
||||||
state.onEdgesChange(edgesChangeHandler)
|
state.onEdgesChange(edgesChangeHandler)
|
||||||
} else {
|
} else {
|
||||||
state.hooks.value.nodesChange.off(nodesChangeHandler)
|
state.hooks.value.nodesChange.off(nodesChangeHandler)
|
||||||
state.hooks.value.edgesChange.off(edgesChangeHandler)
|
state.hooks.value.edgesChange.off(edgesChangeHandler)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{ immediate: true },
|
{ immediate: true },
|
||||||
)
|
)
|
||||||
})
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// if composable was called with additional options after initialization, overwrite state with the options values
|
|
||||||
if (options) {
|
|
||||||
vueFlow.setState(options)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// always provide a fresh instance into context on call
|
|
||||||
if (scope) {
|
|
||||||
provide(VueFlow, vueFlow)
|
|
||||||
|
|
||||||
scope.vueFlowId = vueFlow.id
|
|
||||||
|
|
||||||
if (isParentScope) {
|
|
||||||
// dispose of state values and storage entry
|
// dispose of state values and storage entry
|
||||||
tryOnScopeDispose(() => {
|
tryOnScopeDispose(() => {
|
||||||
if (vueFlow) {
|
if (vueFlow) {
|
||||||
@@ -184,8 +166,20 @@ export function useVueFlow(options?: FlowProps): VueFlowStore {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
// if composable was called with additional options after initialization, overwrite state with the options values
|
||||||
|
if (options) {
|
||||||
|
vueFlow.setState(options)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// always provide a fresh instance into context on call
|
||||||
|
if (scope) {
|
||||||
|
provide(VueFlow, vueFlow)
|
||||||
|
|
||||||
|
scope.vueFlowId = vueFlow.id
|
||||||
|
}
|
||||||
|
|
||||||
return vueFlow
|
return vueFlow
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user