From f8a23c59ecda444435eef11c4ccb8d05df949816 Mon Sep 17 00:00:00 2001 From: Braks <78412429+bcakmakoglu@users.noreply.github.com> Date: Mon, 13 Dec 2021 12:03:40 +0100 Subject: [PATCH] refactor(flow)!: Use watcher to change every prop as a single item * instead of replacing the whole state on each watch trigger, split into multiple watchers that set a single state * improve reacitivty and two-way binding for v-models * remove elements option -> only modelValue (old api) or nodes/edges Signed-off-by: Braks <78412429+bcakmakoglu@users.noreply.github.com> --- src/container/VueFlow/VueFlow.vue | 18 +-- src/container/VueFlow/watch.ts | 160 ++++++++++++++++++++++++++ src/container/ZoomPane/ZoomPane.vue | 4 +- src/store/actions.ts | 3 +- src/store/state.ts | 168 +++++++++++++++++----------- src/store/store.ts | 15 ++- src/types/flow.ts | 1 - src/types/node.ts | 5 +- src/types/store.ts | 1 + 9 files changed, 284 insertions(+), 91 deletions(-) create mode 100644 src/container/VueFlow/watch.ts diff --git a/src/container/VueFlow/VueFlow.vue b/src/container/VueFlow/VueFlow.vue index 8c114497..bc1d4203 100644 --- a/src/container/VueFlow/VueFlow.vue +++ b/src/container/VueFlow/VueFlow.vue @@ -3,6 +3,7 @@ import { createHooks, useHooks } from '../../store' import type { FlowProps } from '../../types/flow' import ZoomPane from '../ZoomPane/ZoomPane.vue' import { useVueFlow } from '../../composables' +import useWatch from './watch' const props = withDefaults(defineProps(), { snapToGrid: false, @@ -25,19 +26,10 @@ const { modelValue, nodes, edges } = useVModels(props, emit) const { store } = useVueFlow(props) useHooks(store.hooks, emit) -nextTick(() => { - modelValue && (modelValue.value = [...store.nodes, ...store.edges]) - nodes && (nodes.value = store.nodes) -}) -nextTick(() => { - modelValue && (modelValue.value = [...store.nodes, ...store.edges]) - edges && (edges.value = store.edges) -}) -watch( - () => props, - (v) => nextTick(() => store.setState(v)), - { flush: 'sync', deep: true, immediate: true }, -) +onMounted(() => useWatch(modelValue, nodes, edges, props, store)) +modelValue && (modelValue.value = [...store.nodes, ...store.edges]) +nodes && (nodes.value = store.nodes) +edges && (edges.value = store.edges)