update: move watcher to onMounted hook

* remove default values for nodes edges from initial store

Signed-off-by: Braks <78412429+bcakmakoglu@users.noreply.github.com>
This commit is contained in:
Braks
2021-11-19 16:51:40 +01:00
parent ee95fbe34a
commit 8c4747a8a9
2 changed files with 32 additions and 36 deletions
+30 -34
View File
@@ -1,6 +1,5 @@
<script lang="ts" setup> <script lang="ts" setup>
import { CSSProperties, onBeforeUnmount } from 'vue' import { CSSProperties, onBeforeUnmount } from 'vue'
import { invoke } from '@vueuse/core'
import diff from 'microdiff' import diff from 'microdiff'
import { import {
ConnectionLineType, ConnectionLineType,
@@ -124,40 +123,37 @@ const init = (opts: typeof props) => {
} }
onBeforeUnmount(() => store?.$dispose()) onBeforeUnmount(() => store?.$dispose())
invoke(async () => { store.hooks.load.on(() => init(props))
await until(elements.value).toMatch((y) => y.length > 0) onMounted(() => {
init(props) watch(
elements,
(val, oldVal) => {
nextTick(() => {
const hasDiff = diff(val, oldVal)
if (hasDiff.length > 0) store.setElements(val)
})
},
{ flush: 'pre', deep: true },
)
watch(
() => store.elements,
(val, oldVal) => {
nextTick(() => {
const hasDiff = diff(val, oldVal)
if (hasDiff.length > 0) elements.value = val
})
},
{ flush: 'pre', deep: true },
)
watch(
() => props,
(val, oldVal) => {
const hasDiff = diff(val, oldVal)
if (hasDiff.length > 0) init(val)
},
{ flush: 'pre', deep: true },
)
}) })
watch(
elements,
(val, oldVal) => {
nextTick(() => {
const hasDiff = diff(val, oldVal)
if (hasDiff.length > 0) store.setElements(val)
})
},
{ flush: 'pre', deep: true },
)
watch(
() => store.elements,
(val, oldVal) => {
nextTick(() => {
const hasDiff = diff(val, oldVal)
if (hasDiff.length > 0) elements.value = val
})
},
{ flush: 'pre', deep: true },
)
watch(
() => props,
(val, oldVal) => {
const hasDiff = diff(val, oldVal)
if (hasDiff.length > 0) init(val)
},
{ flush: 'pre', deep: true },
)
</script> </script>
<template> <template>
<div class="vue-flow"> <div class="vue-flow">
+2 -2
View File
@@ -22,8 +22,8 @@ export const initialState = (): FlowState => ({
height: 0, height: 0,
}, },
transform: [0, 0, 1], transform: [0, 0, 1],
nodeTypes: defaultNodeTypes, nodeTypes: {},
edgeTypes: defaultEdgeTypes, edgeTypes: {},
elements: [], elements: [],
nodes: [], nodes: [],
edges: [], edges: [],