Lay flows out so edges stop crossing where they need not

The canvas placed nodes with dagre, which orders nodes but knows nothing
about ports: an edge leaves a particular handle and lands on a particular
one, so two producers feeding a consumer in the other order crossed just
before they landed however well the nodes were arranged. The graph was not
a multigraph either, so two messages between one pair of nodes collapsed
into one before crossings were counted, and the endpoint labels were laid
out as 220-wide cards with every handle stacked on a single pixel.

ELK's layered algorithm replaces it. Every declared port is handed to it as
a fixed point on the node's rim, at the fraction `portFraction` puts the
handle at — which the node components now render from the same function —
so what the crossing count is minimised over is what ends up on the screen.
Measured on this installation's flows, with the labels included: `home`
goes from 51 crossings to 6 across and 70 to 29 down, `demo_training` 27 to
5 and 59 to 32, and every graph is the same size or smaller.

The engine answers asynchronously and is a chunk of its own, so positions
became state: the canvas draws nothing until the first layout lands, and an
edit keeps the arrangement it had rather than flashing through the corner.
The entry chunk is untouched and the flow route's own chunk came down 167
to 121 kB, since dagre used to be in it.

Two things the crossings made worse come with it. A hovered edge resolves
to full strength so one line can be followed through a busy rank, and a
feedback edge — one whose target the layout put behind its source — swings
out into a lane beside the graph instead of being drawn through everything
between its ends, which is what "House history" was reported for.
This commit is contained in:
2026-08-26 15:37:36 +02:00
parent 527809c771
commit 31d798b41f
9 changed files with 648 additions and 163 deletions
+61 -8
View File
@@ -3,6 +3,7 @@ import {
EdgeLabelRenderer,
type EdgeProps,
getBezierPath,
Position,
useStore,
} from "@xyflow/react"
import { memo, useEffect, useRef, useState } from "react"
@@ -15,6 +16,51 @@ import { useLiveValue } from "./liveStore"
/** Below this zoom the value chips would be unreadable, so they step aside. */
const CHIP_MIN_ZOOM = 0.5
/** How far a feedback edge stands off the ranks it has to get back past. */
const BOW = 60
/**
* The path a feedback edge takes.
*
* The layout ranks a flow in the direction it runs, so an edge closing a cycle
* is the one that goes the other way — a widget setting the window a node
* upstream of it reads. A curve drawn straight between those two handles is
* drawn straight through everything between them. This one leaves its handle
* the way every other edge does, swings out into a lane beside the graph, and
* comes back into its target from the far side, so what it passes it passes
* around.
*
* ponytail: how far out the lane sits is read off how far back the edge has to
* travel, not off what is actually in the way — this stands a cycle apart from
* the flow rather than guaranteeing it clears every node. The layout engine
* routes these properly and React Flow draws its own curves; taking its bend
* points is the real fix, and a bigger one.
*/
function cyclePath(
sourceX: number,
sourceY: number,
targetX: number,
targetY: number,
vertical: boolean,
): [string, number, number] {
const span = vertical
? Math.abs(targetY - sourceY)
: Math.abs(targetX - sourceX)
const lane = Math.max(BOW, span * 0.3)
const [c1x, c1y] = vertical
? [sourceX + lane, sourceY + BOW]
: [sourceX + BOW, sourceY + lane]
const [c2x, c2y] = vertical
? [targetX + lane, targetY - BOW]
: [targetX - BOW, targetY + lane]
return [
`M${sourceX},${sourceY} C${c1x},${c1y} ${c2x},${c2y} ${targetX},${targetY}`,
// Halfway along a cubic, which is where the chip rides.
(sourceX + 3 * c1x + 3 * c2x + targetX) / 8,
(sourceY + 3 * c1y + 3 * c2y + targetY) / 8,
]
}
function formatValue(value: unknown): string {
if (typeof value === "number") {
return Number.isInteger(value) ? String(value) : value.toFixed(2)
@@ -40,14 +86,21 @@ function LiveEdgeComponent({
const [pulsing, setPulsing] = useState(false)
const lastTs = useRef<number | null>(null)
const [path, labelX, labelY] = getBezierPath({
sourceX,
sourceY,
sourcePosition,
targetX,
targetY,
targetPosition,
})
// Every rank sits further along the flow than the one before it, so an edge
// whose target has ended up behind its source is one closing a cycle.
const vertical = sourcePosition === Position.Bottom
const backwards = vertical ? targetY < sourceY : targetX < sourceX
const [path, labelX, labelY] = backwards
? cyclePath(sourceX, sourceY, targetX, targetY, vertical)
: getBezierPath({
sourceX,
sourceY,
sourcePosition,
targetX,
targetY,
targetPosition,
})
// Restart the stroke animation whenever a newer message lands — but only
// for the producer that actually published it. A message can have several