11import type { Ref , ToRefs } from 'vue'
22import type { WatchPausableReturn } from '@vueuse/core'
3- import type { FlowProps , GraphEdge , GraphNode , VueFlowStore } from '~/types'
3+ import { isFunction } from '@vueuse/core'
4+ import type { Connection , FlowProps , GraphEdge , GraphNode , VueFlowStore } from '~/types'
45
56const isDef = < T > ( val : T ) : val is NonNullable < T > => typeof val !== 'undefined'
67export default ( models : ToRefs < Pick < FlowProps , 'nodes' | 'edges' | 'modelValue' > > , props : FlowProps , store : VueFlowStore ) => {
@@ -171,8 +172,45 @@ export default (models: ToRefs<Pick<FlowProps, 'nodes' | 'edges' | 'modelValue'>
171172 } )
172173 }
173174
175+ const watchAutoConnect = ( ) => {
176+ scope . run ( ( ) => {
177+ watch (
178+ ( ) => props . autoConnect ,
179+ ( ) => {
180+ if ( isDef ( props . autoConnect ) ) {
181+ store . autoConnect . value = props . autoConnect
182+ }
183+ } ,
184+ { immediate : isDef ( props . autoConnect ) } ,
185+ )
186+
187+ watch (
188+ store . autoConnect ,
189+ ( ) => {
190+ const autoConnector = async ( params : Connection ) => {
191+ let connect : boolean | Connection = true
192+ if ( isFunction ( props . autoConnect ) ) {
193+ connect = await props . autoConnect ( params )
194+ }
195+
196+ if ( connect ) {
197+ store . addEdges ( [ params ] )
198+ }
199+ }
200+
201+ if ( store . autoConnect ) {
202+ store . onConnect ( autoConnector )
203+ } else {
204+ store . hooks . value . connect . off ( autoConnector )
205+ }
206+ } ,
207+ { immediate : true } ,
208+ )
209+ } )
210+ }
211+
174212 const watchRest = ( ) => {
175- const skip = [ 'id' , 'modelValue' , 'edges' , 'nodes' , 'maxZoom' , 'minZoom' , 'applyDefault' ]
213+ const skip = [ 'id' , 'modelValue' , 'edges' , 'nodes' , 'maxZoom' , 'minZoom' , 'applyDefault' , 'autoConnect' ]
176214 Object . keys ( props ) . forEach ( ( prop ) => {
177215 if ( ! skip . includes ( prop ) ) {
178216 const model = props [ prop as keyof typeof props ]
@@ -193,9 +231,16 @@ export default (models: ToRefs<Pick<FlowProps, 'nodes' | 'edges' | 'modelValue'>
193231 } )
194232 }
195233
196- ; [ watchModelValue , watchNodesValue , watchEdgesValue , watchMinZoom , watchMaxZoom , watchApplyDefault , watchRest ] . forEach (
197- ( watch ) => watch ( ) ,
198- )
234+ ; [
235+ watchModelValue ,
236+ watchNodesValue ,
237+ watchEdgesValue ,
238+ watchMinZoom ,
239+ watchMaxZoom ,
240+ watchApplyDefault ,
241+ watchAutoConnect ,
242+ watchRest ,
243+ ] . forEach ( ( watch ) => watch ( ) )
199244 } )
200245
201246 return ( ) => scope . stop ( )
0 commit comments