fix(core): check if injected state matches options and scope id (#1562)

* fix(core): prefer options id when finding state

Signed-off-by: braks <78412429+bcakmakoglu@users.noreply.github.com>

* fix(core): check if injected state matches options id

Signed-off-by: braks <78412429+bcakmakoglu@users.noreply.github.com>

* chore(changeset): add

Signed-off-by: braks <78412429+bcakmakoglu@users.noreply.github.com>

---------

Signed-off-by: braks <78412429+bcakmakoglu@users.noreply.github.com>
This commit is contained in:
Braks
2024-07-27 20:23:46 +02:00
parent 6d9535a1a9
commit fff558fbc3
3 changed files with 15 additions and 5 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@vue-flow/core": patch
---
Check if injected vue flow state matches options id, otherwise create new state
+5
View File
@@ -0,0 +1,5 @@
---
"@vue-flow/core": patch
---
Prefer options id over scope id when finding vue flow internal state by id
+5 -5
View File
@@ -33,7 +33,7 @@ export function useVueFlow(idOrOpts?: any): VueFlowStore {
const options = isOptsObj ? idOrOpts : { id: idOrOpts } const options = isOptsObj ? idOrOpts : { id: idOrOpts }
const id = options.id const id = options.id
const vueFlowId = scope?.vueFlowId || id const vueFlowId = id ?? scope?.vueFlowId
let vueFlow: Injection let vueFlow: Injection
@@ -42,9 +42,9 @@ export function useVueFlow(idOrOpts?: any): VueFlowStore {
* this should be the regular way after initialization * this should be the regular way after initialization
*/ */
if (scope) { if (scope) {
const injection = inject(VueFlow, null) const injectedState = inject(VueFlow, null)
if (typeof injection !== 'undefined' && injection !== null) { if (typeof injectedState !== 'undefined' && injectedState !== null && (!vueFlowId || injectedState.id === vueFlowId)) {
vueFlow = injection vueFlow = injectedState
} }
} }
@@ -63,7 +63,7 @@ export function useVueFlow(idOrOpts?: any): VueFlowStore {
* _or_ if the store instance we found does not match up with provided ids * _or_ if the store instance we found does not match up with provided ids
* create a new store instance and register it in storage * create a new store instance and register it in storage
*/ */
if (!vueFlow || (vueFlow && id && id !== vueFlow.id)) { if (!vueFlow || (vueFlowId && vueFlow.id !== vueFlowId)) {
const name = id ?? storage.getId() const name = id ?? storage.getId()
const state = storage.create(name, options) const state = storage.create(name, options)