fix(core): use detached scope to dispose state and stop watcher

This commit is contained in:
braks
2023-07-24 23:46:59 +02:00
committed by Braks
parent 581f2b1fec
commit 45f18d5459
+15 -21
View File
@@ -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,10 +129,9 @@ 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) => {
@@ -156,22 +153,7 @@ export function useVueFlow(options?: FlowProps): VueFlowStore {
}, },
{ 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
} }