From 544a62ecc48bcc3708bcf8a32453066d85785fde Mon Sep 17 00:00:00 2001 From: Braks <78412429+bcakmakoglu@users.noreply.github.com> Date: Sun, 27 Mar 2022 17:03:15 +0200 Subject: [PATCH] update(flow): try fetching instance when out of scope --- src/composables/useVueFlow.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/composables/useVueFlow.ts b/src/composables/useVueFlow.ts index 280b3101..a4d40a79 100644 --- a/src/composables/useVueFlow.ts +++ b/src/composables/useVueFlow.ts @@ -48,13 +48,19 @@ type Scope = EffectScope & { vueFlowId: string } export default (options?: Partial>): UseVueFlow => { const storage = Storage.getInstance() const scope = getCurrentScope() as Scope + const vueFlowId = scope.vueFlowId || options?.id + let vueFlow: UseVueFlow | null | undefined + if (scope) { const injection = inject(VueFlow, null) - const id = scope.vueFlowId || options?.id if (typeof injection !== 'undefined' && injection !== null) vueFlow = injection - else if (id) vueFlow = storage.get(scope.vueFlowId) } + + if (!vueFlow) { + if (vueFlowId) vueFlow = storage.get(scope.vueFlowId) + } + if (!vueFlow || (vueFlow && options?.id && options.id !== vueFlow.id)) { const name = options?.id ?? `vue-flow-${id++}` @@ -72,6 +78,7 @@ export default (options?: Partial>): UseVueFlo } else { if (options) vueFlow.setState(options) } + if (!vueFlow) throw new Error('vue flow store instance not found.') return vueFlow