refactor(flow): set elements when initializing store

Signed-off-by: Braks <78412429+bcakmakoglu@users.noreply.github.com>
This commit is contained in:
Braks
2021-12-20 19:29:52 +01:00
parent f2fe93f1db
commit 2910731c3b
2 changed files with 9 additions and 4 deletions
+2 -2
View File
@@ -1,5 +1,5 @@
import { getCurrentInstance } from 'vue'
import { FlowOptions, State, UseVueFlow } from '~/types'
import { FlowOptions, UseVueFlow } from '~/types'
import { VueFlow } from '~/context'
import { useStore } from '~/store'
@@ -11,7 +11,7 @@ export default (options?: Partial<FlowOptions>): UseVueFlow => {
: false
if (!vueFlow || (vueFlow && options?.id && options.id !== vueFlow.id)) {
const name = options?.id ?? `vue-flow-${id++}`
const store = useStore(options as State)
const store = useStore(options)
vueFlow = {
id: name,
store: reactive(store),
+7 -2
View File
@@ -1,9 +1,9 @@
import useState from './state'
import useActions from './actions'
import useGetters from './getters'
import { FlowHooksOn, State, Store } from '~/types'
import { FlowHooksOn, FlowOptions, Store } from '~/types'
export default (preloadedState: State): Store => {
export default (preloadedState?: FlowOptions): Store => {
const state = reactive(useState(preloadedState))
const getters = useGetters(state)
const actions = useActions(state, getters)
@@ -13,6 +13,11 @@ export default (preloadedState: State): Store => {
hooksOn[<keyof FlowHooksOn>name] = h.on as any
})
actions.setState(state)
if (preloadedState) {
if (preloadedState.modelValue) actions.setElements(preloadedState.modelValue)
if (preloadedState.nodes) actions.setNodes(preloadedState.nodes)
if (preloadedState.edges) actions.setEdges(preloadedState.edges)
}
return {
state,