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

View File

@@ -1,6 +1,5 @@
<script lang="ts" setup>
import { CSSProperties, onBeforeUnmount } from 'vue'
import { invoke } from '@vueuse/core'
import diff from 'microdiff'
import {
ConnectionLineType,
@@ -124,40 +123,37 @@ const init = (opts: typeof props) => {
}
onBeforeUnmount(() => store?.$dispose())
invoke(async () => {
await until(elements.value).toMatch((y) => y.length > 0)
init(props)
store.hooks.load.on(() => init(props))
onMounted(() => {
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>
<template>
<div class="vue-flow">

View File

@@ -22,8 +22,8 @@ export const initialState = (): FlowState => ({
height: 0,
},
transform: [0, 0, 1],
nodeTypes: defaultNodeTypes,
edgeTypes: defaultEdgeTypes,
nodeTypes: {},
edgeTypes: {},
elements: [],
nodes: [],
edges: [],