Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): only trigger store watcher immediately when els were set #545

Merged
merged 3 commits into from Dec 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/dirty-ants-divide.md
@@ -0,0 +1,5 @@
---
'@vue-flow/core': patch
---

Remove immediate watch of VueFlow props and set prop values via state initalizer
5 changes: 5 additions & 0 deletions .changeset/violet-squids-peel.md
@@ -0,0 +1,5 @@
---
'@vue-flow/core': patch
---

Only trigger store watcher immediate when elements were set, otherwise wait for changes in store to overwrite model-value
2 changes: 1 addition & 1 deletion packages/core/src/container/VueFlow/VueFlow.vue
Expand Up @@ -100,7 +100,7 @@ const modelValue = useVModel(props, 'modelValue', emit)
const modelNodes = useVModel(props, 'nodes', emit)
const modelEdges = useVModel(props, 'edges', emit)

const { vueFlowRef, id, hooks, getNodeTypes, getEdgeTypes, $reset, ...rest } = useVueFlow({ id: props.id })
const { vueFlowRef, id, hooks, getNodeTypes, getEdgeTypes, $reset, ...rest } = useVueFlow(props)

const dispose = useWatch({ modelValue, nodes: modelNodes, edges: modelEdges }, props, {
vueFlowRef,
Expand Down
74 changes: 43 additions & 31 deletions packages/core/src/utils/watch.ts
Expand Up @@ -17,6 +17,7 @@ export const useWatch = (
let pauseStore: WatchPausableReturn

const immediate = !!(models.modelValue && models.modelValue.value)
let immediateStore = !!(store.nodes.value.length || store.edges.value.length)

// eslint-disable-next-line prefer-const
pauseModel = watchPausable(
Expand All @@ -27,7 +28,9 @@ export const useWatch = (

store.setElements(elements)

pauseStore?.resume()
// only trigger store watcher immediately if we actually set any elements to the store
if (!pauseStore && !immediateStore && elements.length) immediateStore = true
else pauseStore?.resume()
}
},
{ immediate },
Expand All @@ -46,6 +49,7 @@ export const useWatch = (
})
}
},
{ immediate: immediateStore },
)

onScopeDispose(() => {
Expand All @@ -61,6 +65,7 @@ export const useWatch = (
let pauseStore: WatchPausableReturn

const immediate = !!(models.nodes && models.nodes.value)
let immediateStore = !!store.nodes.value.length

// eslint-disable-next-line prefer-const
pauseModel = watchPausable(
Expand All @@ -71,23 +76,29 @@ export const useWatch = (

store.setNodes(nodes)

pauseStore?.resume()
// only trigger store watcher immediately if we actually set any elements to the store
if (!pauseStore && !immediateStore && nodes.length) immediateStore = true
else pauseStore?.resume()
}
},
{ immediate },
)

pauseStore = watchPausable([store.nodes, () => store.nodes.value.length], ([nodes]) => {
if (models.nodes?.value && Array.isArray(models.nodes.value)) {
pauseModel?.pause()
pauseStore = watchPausable(
[store.nodes, () => store.nodes.value.length],
([nodes]) => {
if (models.nodes?.value && Array.isArray(models.nodes.value)) {
pauseModel?.pause()

models.nodes.value = [...nodes]
models.nodes.value = [...nodes]

nextTick(() => {
pauseModel?.resume()
})
}
})
nextTick(() => {
pauseModel?.resume()
})
}
},
{ immediate: immediateStore },
)

onScopeDispose(() => {
pauseModel?.stop()
Expand All @@ -102,6 +113,7 @@ export const useWatch = (
let pauseStore: WatchPausableReturn

const immediate = !!(models.edges && models.edges.value)
let immediateStore = !!store.edges.value.length

// eslint-disable-next-line prefer-const
pauseModel = watchPausable(
Expand All @@ -112,23 +124,29 @@ export const useWatch = (

store.setEdges(edges)

pauseStore?.resume()
// only trigger store watcher immediately if we actually set any elements to the store
if (!pauseStore && !immediateStore && edges.length) immediateStore = true
else pauseStore?.resume()
}
},
{ immediate },
)

pauseStore = watchPausable([store.edges, () => store.edges.value.length], ([edges]) => {
if (models.edges?.value && Array.isArray(models.edges.value)) {
pauseModel?.pause()
pauseStore = watchPausable(
[store.edges, () => store.edges.value.length],
([edges]) => {
if (models.edges?.value && Array.isArray(models.edges.value)) {
pauseModel?.pause()

models.edges.value = [...edges]
models.edges.value = [...edges]

nextTick(() => {
pauseModel?.resume()
})
}
})
nextTick(() => {
pauseModel?.resume()
})
}
},
{ immediate: immediateStore },
)

onScopeDispose(() => {
pauseModel?.stop()
Expand All @@ -146,7 +164,6 @@ export const useWatch = (
store.setMaxZoom(props.maxZoom)
}
},
{ immediate: isDef(props.maxZoom) },
)
})
}
Expand All @@ -160,7 +177,6 @@ export const useWatch = (
store.setMinZoom(props.minZoom)
}
},
{ immediate: isDef(props.minZoom) },
)
})
}
Expand All @@ -174,7 +190,6 @@ export const useWatch = (
store.setTranslateExtent(props.translateExtent)
}
},
{ immediate: isDef(props.translateExtent) },
)
})
}
Expand All @@ -188,7 +203,6 @@ export const useWatch = (
store.setNodeExtent(props.nodeExtent)
}
},
{ immediate: isDef(props.nodeExtent) },
)
})
}
Expand All @@ -202,7 +216,6 @@ export const useWatch = (
store.applyDefault.value = props.applyDefault
}
},
{ immediate: isDef(props.applyDefault) },
)

watch(
Expand Down Expand Up @@ -247,7 +260,6 @@ export const useWatch = (
store.autoConnect.value = props.autoConnect
}
},
{ immediate: isDef(props.autoConnect) },
)

watch(
Expand Down Expand Up @@ -290,12 +302,12 @@ export const useWatch = (
scope.run(() => {
watch(
model,
() => {
if (isDef(model)) {
storedValue.value = model.value
(nextValue) => {
if (isDef(nextValue)) {
storedValue.value = nextValue
}
},
{ immediate: isDef(model) },
{ flush: 'pre' },
)
})
}
Expand Down