1
1
import { toRefs , tryOnScopeDispose } from '@vueuse/core'
2
2
import type { EffectScope } from 'vue'
3
- import { computed , getCurrentScope , inject , provide , reactive , watch } from 'vue'
3
+ import { computed , effectScope , getCurrentScope , inject , provide , reactive , watch } from 'vue'
4
4
import { useActions , useGetters , useState } from '~/store'
5
5
import type { EdgeChange , FlowOptions , FlowProps , NodeChange , State , VueFlowStore } from '~/types'
6
6
import { VueFlow } from '~/context'
@@ -96,8 +96,6 @@ export function useVueFlow(options?: FlowProps): VueFlowStore {
96
96
97
97
let vueFlow : Injection
98
98
99
- let isParentScope = false
100
-
101
99
/**
102
100
* check if we can get a store instance through injections
103
101
* this should be the regular way after initialization
@@ -131,47 +129,31 @@ export function useVueFlow(options?: FlowProps): VueFlowStore {
131
129
132
130
vueFlow = state
133
131
134
- if ( scope ) {
135
- isParentScope = true
136
-
137
- scope . run ( ( ) => {
138
- watch (
139
- state . applyDefault ,
140
- ( shouldApplyDefault ) => {
141
- const nodesChangeHandler = ( changes : NodeChange [ ] ) => {
142
- state . applyNodeChanges ( changes )
143
- }
144
-
145
- const edgesChangeHandler = ( changes : EdgeChange [ ] ) => {
146
- state . applyEdgeChanges ( changes )
147
- }
148
-
149
- if ( shouldApplyDefault ) {
150
- state . onNodesChange ( nodesChangeHandler )
151
- state . onEdgesChange ( edgesChangeHandler )
152
- } else {
153
- state . hooks . value . nodesChange . off ( nodesChangeHandler )
154
- state . hooks . value . edgesChange . off ( edgesChangeHandler )
155
- }
156
- } ,
157
- { immediate : true } ,
158
- )
159
- } )
160
- }
161
- } else {
162
- // if composable was called with additional options after initialization, overwrite state with the options values
163
- if ( options ) {
164
- vueFlow . setState ( options )
165
- }
166
- }
132
+ const detachedScope = effectScope ( )
167
133
168
- // always provide a fresh instance into context on call
169
- if ( scope ) {
170
- provide ( VueFlow , vueFlow )
134
+ detachedScope . run ( ( ) => {
135
+ watch (
136
+ state . applyDefault ,
137
+ ( shouldApplyDefault ) => {
138
+ const nodesChangeHandler = ( changes : NodeChange [ ] ) => {
139
+ state . applyNodeChanges ( changes )
140
+ }
171
141
172
- scope . vueFlowId = vueFlow . id
142
+ const edgesChangeHandler = ( changes : EdgeChange [ ] ) => {
143
+ state . applyEdgeChanges ( changes )
144
+ }
145
+
146
+ if ( shouldApplyDefault ) {
147
+ state . onNodesChange ( nodesChangeHandler )
148
+ state . onEdgesChange ( edgesChangeHandler )
149
+ } else {
150
+ state . hooks . value . nodesChange . off ( nodesChangeHandler )
151
+ state . hooks . value . edgesChange . off ( edgesChangeHandler )
152
+ }
153
+ } ,
154
+ { immediate : true } ,
155
+ )
173
156
174
- if ( isParentScope ) {
175
157
// dispose of state values and storage entry
176
158
tryOnScopeDispose ( ( ) => {
177
159
if ( vueFlow ) {
@@ -184,8 +166,20 @@ export function useVueFlow(options?: FlowProps): VueFlowStore {
184
166
}
185
167
}
186
168
} )
169
+ } )
170
+ } else {
171
+ // if composable was called with additional options after initialization, overwrite state with the options values
172
+ if ( options ) {
173
+ vueFlow . setState ( options )
187
174
}
188
175
}
189
176
177
+ // always provide a fresh instance into context on call
178
+ if ( scope ) {
179
+ provide ( VueFlow , vueFlow )
180
+
181
+ scope . vueFlowId = vueFlow . id
182
+ }
183
+
190
184
return vueFlow
191
185
}
0 commit comments