fix: two timeline pagination bugs affecting message ordering

## 1. useTimelinePagination: finally-block resets fetchingRef prematurely

When a paginated page returns 1–4 events and more history exists, the
paginator fires a recursive continuation call (`paginate(backwards)`)
before the outer `finally` block runs.  The sequence was:

  1. outer try: `fetchingRef.current[dir] = false; paginate(backwards);`
     → inner paginate runs synchronously:
       checks lock (false) → sets it to `true` → hits `await` → suspends
  2. outer finally: `fetchingRef.current[dir] = false;`
     → overwrites the `true` that inner paginate just wrote

This left `fetchingRef.current[dir] = false` while the inner paginate
was mid-flight.  The `backwardStatus === 'loading'` guard on the scroll
handler masked the problem in most cases, but for a long chain of sparse
pages each returning 1–4 events, multiple overlapping paginations could
start.  If the SDK's internal per-timeline lock was also released (or a
forward-direction call raced), events could be requested from stale
pagination tokens and be inserted out of order.

Fix: add a `continuing` local boolean.  When a recursive continuation is
fired, `continuing = true` is set first.  The `finally` block skips the
reset when `continuing` is true, leaving the lock in the inner paginate's
hands.  The inner paginate's own `finally` releases it when it finishes.

## 2. getLinkedTimelines: O(N²) array copies in recursive accumulator

The previous recursive `collectTimelines(tl, dir, [...acc, tl])` created
N intermediate arrays for an N-timeline chain (sizes 1, 2, 3, … N),
giving O(N²) total allocations.  For a power user who paginates back
through hundreds of pages of history, this becomes measurable GC pressure.

Replace with a simple iterative loop (`while (current) { push; next }`)
that builds exactly one output array in O(N) time.
This commit is contained in:
Evie Gauthier committed 2026-03-24 23:32:24 -04:00
1 parent 28d8e942ac
commit c6b3293ecc
2 files changed
+26 -12

No files matched your search

+19 -1
View File
@@ -171,6 +171,13 @@ const useTimelinePagination = (
await to(decryptAllTimelineEvent(mx, fetchedTimeline));
}
// `continuing` tracks whether we hand the fetchingRef lock to a recursive
// continuation call below. The finally block must NOT reset the lock if
// the recursive call has already claimed it, otherwise there is a brief
// window where fetchingRef is false while the recursive paginate is in
// flight, allowing a third overlapping call to start on sparse pages.
let continuing = false;
if (alive()) {
recalibratePagination(lTimelines);
(backwards ? setBackwardStatus : setForwardStatus)('idle');
@@ -184,13 +191,24 @@ const useTimelinePagination = (
Direction.Backward
) === 'string';
if (stillHasToken) {
// Release lock so inner paginate can claim it, then mark continuing
// so the finally block below does NOT reset it after inner claims.
fetchingRef.current[directionKey] = false;
continuing = true;
paginate(backwards);
// At this point the inner paginate has synchronously set
// fetchingRef.current[directionKey] = true before hitting its own
// await. The finally below will skip the reset.
}
}
}
} finally {
fetchingRef.current[directionKey] = false;
// Only release the lock if we did NOT hand it to a recursive continuation.
// If `continuing` is true the recursive call owns the lock and will release
// it in its own finally block.
if (!continuing) {
fetchingRef.current[directionKey] = false;
}
}
};
}, [mx, alive, setTimeline, limit, setBackwardStatus, setForwardStatus]);
+7 -11
View File
@@ -22,18 +22,14 @@ export const getFirstLinkedTimeline = (
return current;
};
const collectTimelines = (
tl: EventTimeline | null,
dir: Direction,
acc: EventTimeline[] = []
): EventTimeline[] => {
if (!tl) return acc;
return collectTimelines(tl.getNeighbouringTimeline(dir), dir, [...acc, tl]);
};
export const getLinkedTimelines = (timeline: EventTimeline): EventTimeline[] => {
const firstTimeline = getFirstLinkedTimeline(timeline, Direction.Backward);
return collectTimelines(firstTimeline, Direction.Forward);
const result: EventTimeline[] = [];
let current: EventTimeline | null = getFirstLinkedTimeline(timeline, Direction.Backward);
while (current) {
result.push(current);
current = current.getNeighbouringTimeline(Direction.Forward);
}
return result;
};
export const timelineToEventsCount = (t: EventTimeline) => {