Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions resources/js/lib/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,24 @@ export function graphEdges(
drawing: ReadonlySet<string> = new Set(),
): GraphEdge[] {
const byId = new Map(nodes.map((node) => [node.id, node]));
const repliesByParent = new Map<string, GraphNode[]>();
const successorsByAfter = new Map<string, GraphNode[]>();
const edges: GraphEdge[] = [];

for (const node of nodes) {
if (node.parent !== undefined) {
const replies = repliesByParent.get(node.parent) ?? [];
replies.push(node);
repliesByParent.set(node.parent, replies);
}

if (node.after !== undefined) {
const successors = successorsByAfter.get(node.after) ?? [];
successors.push(node);
successorsByAfter.set(node.after, successors);
}
}

for (const node of nodes) {
const previous =
node.after === undefined ? undefined : byId.get(node.after);
Expand All @@ -70,16 +86,12 @@ export function graphEdges(
node.parent === undefined ? undefined : byId.get(node.parent);

if (parent) {
const continues = nodes.some(
const continues = (successorsByAfter.get(parent.id) ?? []).some(
(other) =>
other.after === parent.id &&
other.y > node.y &&
!drawing.has(trunkId(parent, other)),
other.y > node.y && !drawing.has(trunkId(parent, other)),
);
const above = nodes
.filter(
(other) => other.parent === parent.id && other.y < node.y,
)
const above = (repliesByParent.get(parent.id) ?? [])
.filter((other) => other.y < node.y)
.reduce<GraphNode | undefined>(
(closest, other) =>
closest === undefined || other.y > closest.y
Expand Down
17 changes: 17 additions & 0 deletions tests/Unit/GraphEdges.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,3 +588,20 @@ void test('folds away the lines to and from folding replies, not the trunk runni
false,
);
});

void test('unrelated nodes do not affect the reply and continuation paths', () => {
const thread = [
node({ id: 'parent', y: 40 }),
node({ id: 'first', x: 100, y: 160, parent: 'parent' }),
node({ id: 'next', y: 400, after: 'parent' }),
];
const baseline = graphEdges(thread).map((edge) => [edge.id, edge.d]);
const unrelated = Array.from({ length: 200 }, (_, index) =>
node({ id: `unrelated-${index}`, y: index * 10 }),
);

assert.deepEqual(
graphEdges([...thread, ...unrelated]).map((edge) => [edge.id, edge.d]),
baseline,
);
});
Loading