fix(flow): check for scope before trying injection
This commit is contained in:
@@ -48,29 +48,31 @@ type Scope = EffectScope & { vueFlowId: string }
|
|||||||
export default <N = any, E = N>(options?: Partial<FlowOptions<N, E>>): UseVueFlow<N, E> => {
|
export default <N = any, E = N>(options?: Partial<FlowOptions<N, E>>): UseVueFlow<N, E> => {
|
||||||
const storage = Storage.getInstance()
|
const storage = Storage.getInstance()
|
||||||
const scope = getCurrentScope() as Scope
|
const scope = getCurrentScope() as Scope
|
||||||
let vueFlow: UseVueFlow | null | undefined =
|
let vueFlow: UseVueFlow | null | undefined
|
||||||
typeof inject(VueFlow, undefined) !== 'undefined'
|
if (scope) {
|
||||||
? inject(VueFlow, undefined)!
|
const injection = inject(VueFlow, null)
|
||||||
: scope && scope.vueFlowId
|
const id = scope.vueFlowId || options?.id
|
||||||
? storage.get(scope.vueFlowId)
|
if (typeof injection !== 'undefined' && injection !== null) vueFlow = injection
|
||||||
: undefined
|
else if (id) vueFlow = storage.get(scope.vueFlowId)
|
||||||
|
}
|
||||||
if (!vueFlow || (vueFlow && options?.id && options.id !== vueFlow.id)) {
|
if (!vueFlow || (vueFlow && options?.id && options.id !== vueFlow.id)) {
|
||||||
const name = options?.id ?? `vue-flow-${id++}`
|
const name = options?.id ?? `vue-flow-${id++}`
|
||||||
|
|
||||||
vueFlow = storage.create(name, options)
|
vueFlow = storage.create(name, options)
|
||||||
if (scope) {
|
|
||||||
provide(VueFlow, storage.get(name))
|
|
||||||
scope.vueFlowId = name
|
|
||||||
}
|
|
||||||
|
|
||||||
onScopeDispose(() => {
|
|
||||||
storage.remove(name)
|
|
||||||
vueFlow = null
|
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
if (options) vueFlow.setState(options)
|
if (options) vueFlow.setState(options)
|
||||||
}
|
}
|
||||||
if (!vueFlow) throw new Error('vue flow store instance not found.')
|
if (!vueFlow) throw new Error('vue flow store instance not found.')
|
||||||
|
|
||||||
|
if (scope) {
|
||||||
|
provide(VueFlow, storage.get(vueFlow.id))
|
||||||
|
scope.vueFlowId = vueFlow.id
|
||||||
|
|
||||||
|
onScopeDispose(() => {
|
||||||
|
storage.remove(vueFlow!.id)
|
||||||
|
vueFlow = null
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return vueFlow
|
return vueFlow
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user