From e136c28f0e631c227c7b9e0740c93bd0e98d9668 Mon Sep 17 00:00:00 2001 From: Braks <78412429+bcakmakoglu@users.noreply.github.com> Date: Mon, 28 Mar 2022 19:35:48 +0200 Subject: [PATCH] fix(flow): check if scope exists before accessing id * add internal injection type * move id getter to storage class --- src/composables/useVueFlow.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/composables/useVueFlow.ts b/src/composables/useVueFlow.ts index a4d40a79..d7e66ebf 100644 --- a/src/composables/useVueFlow.ts +++ b/src/composables/useVueFlow.ts @@ -4,6 +4,7 @@ import { VueFlow } from '~/context' import { useStore } from '~/store' export class Storage { + public currentId = 0 public flows = new Map() static instance: Storage @@ -40,17 +41,21 @@ export class Storage { this.set(id, flow) return flow } + + public getId() { + return `vue-flow-${this.currentId++}` + } } -let id = 0 +type Injection = UseVueFlow | null | undefined +type Scope = (EffectScope & { vueFlowId: string }) | undefined -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 + const vueFlowId = scope?.vueFlowId || options?.id - let vueFlow: UseVueFlow | null | undefined + let vueFlow: Injection if (scope) { const injection = inject(VueFlow, null) @@ -58,11 +63,11 @@ export default (options?: Partial>): UseVueFlo } if (!vueFlow) { - if (vueFlowId) vueFlow = storage.get(scope.vueFlowId) + if (vueFlowId) vueFlow = storage.get(vueFlowId) } if (!vueFlow || (vueFlow && options?.id && options.id !== vueFlow.id)) { - const name = options?.id ?? `vue-flow-${id++}` + const name = options?.id ?? storage.getId() vueFlow = storage.create(name, options)