diff --git a/.changeset/smooth-pillows-laugh.md b/.changeset/smooth-pillows-laugh.md new file mode 100644 index 000000000..8b70ab4fa --- /dev/null +++ b/.changeset/smooth-pillows-laugh.md @@ -0,0 +1,5 @@ +--- +"@vue-flow/core": patch +--- + +Defer connection lookup on edge update diff --git a/packages/core/src/store/actions.ts b/packages/core/src/store/actions.ts index 882aac2ef..a88412282 100644 --- a/packages/core/src/store/actions.ts +++ b/packages/core/src/store/actions.ts @@ -1,5 +1,6 @@ import { zoomIdentity } from 'd3-zoom' import type { ComputedRef } from 'vue' +import { nextTick } from 'vue' import { until } from '@vueuse/core' import type { Actions, @@ -509,7 +510,13 @@ export function useActions(state: State, nodeLookup: ComputedRef, ed } const updateEdge: Actions['updateEdge'] = (oldEdge, newConnection, shouldReplaceId = true) => { - const prevEdge = findEdge(oldEdge.id)! + const prevEdge = findEdge(oldEdge.id) + + if (!prevEdge) { + return false + } + + const prevEdgeIndex = state.edges.indexOf(prevEdge) const newEdge = updateEdgeAction(oldEdge, newConnection, prevEdge, shouldReplaceId, state.hooks.error.trigger) @@ -525,9 +532,13 @@ export function useActions(state: State, nodeLookup: ComputedRef, ed state.edges, ) - state.edges.splice(state.edges.indexOf(prevEdge), 1, validEdge) + nextTick(() => { + state.edges = state.edges.map((edge, index) => (index === prevEdgeIndex ? validEdge : edge)) - updateConnectionLookup(state.connectionLookup, edgeLookup.value, [validEdge]) + nextTick(() => { + updateConnectionLookup(state.connectionLookup, edgeLookup.value, [validEdge]) + }) + }) return validEdge }