Files
vue-flow/src/composables/useVueFlow.ts
T
Braks 39501d0bb0 refactor(flow): export useNodesState and useEdgesState separately from useVueFlow
* bind vueFlow instance to currentInstance when useVueFlow is called multiple times in a single setup call

Signed-off-by: Braks <78412429+bcakmakoglu@users.noreply.github.com>
2021-12-20 19:29:52 +01:00

29 lines
827 B
TypeScript

import { getCurrentInstance } from 'vue'
import { FlowOptions, UseVueFlow } from '~/types'
import { VueFlow } from '~/context'
import { useStore } from '~/store'
let id = 0
export default (options?: FlowOptions): UseVueFlow => {
const currentInstance = getCurrentInstance()
let vueFlow: UseVueFlow | false | undefined = currentInstance
? inject(VueFlow, undefined) ?? (currentInstance.vueFlow as UseVueFlow)
: false
if (!vueFlow || (vueFlow && options?.id && options.id !== vueFlow.id)) {
const name = options?.id ?? `vue-flow-${id++}`
const store = reactive(useStore(name, options))
vueFlow = {
id: name,
store,
...toRefs(store),
...store.hooksOn,
}
}
if (currentInstance) {
provide(VueFlow, vueFlow)
currentInstance.vueFlow = vueFlow
}
return vueFlow
}