Skip to content

fix(timeline): stable callback refs & prevent spurious scroll-to-bottom on sync gap#277

Closed
Just-Insane wants to merge 3 commits intoSableClient:devfrom
Just-Insane:fix/timeline-refresh-scroll
Closed

fix(timeline): stable callback refs & prevent spurious scroll-to-bottom on sync gap#277
Just-Insane wants to merge 3 commits intoSableClient:devfrom
Just-Insane:fix/timeline-refresh-scroll

Conversation

@Just-Insane
Copy link
Contributor

@Just-Insane Just-Insane commented Mar 15, 2026

Five related fixes that address scroll stability and event-listener accumulation triggered by sync error/retry cycles.

@Just-Insane Just-Insane requested a review from a team March 15, 2026 04:26
Evie Gauthier and others added 3 commits March 15, 2026 00:30
When a sliding-sync limited response fires RoomEvent.TimelineReset (or
the app fires RoomEvent.TimelineRefresh) the useLiveTimelineRefresh
callback was unconditionally calling setAtBottom(true) and incrementing
scrollToBottomRef, scrolling the user to the bottom even if they had
scrolled up to read history.

Three coordinated changes:

1. Add RoomEvent.TimelineReset handler to useLiveTimelineRefresh.
   The SDK emits TimelineReset on the EventTimelineSet (not the Room)
   when a limited sync response replaces the live EventTimeline. Without
   this listener the stored linkedTimelines reference the old detached
   chain; back-pagination silently no-ops, freezing the room.

2. Gate the viewport scroll on atBottomRef.current (prior position).
   Capture wasAtBottom before calling setTimeline(getInitialTimeline).
   Only call setAtBottom(true) and increment scrollToBottomRef when the
   user was already at the bottom. When scrolled up we still reinit the
   timeline (the old chain is gone) but avoid the forced scroll.

3. Add timelineJustResetRef to allow the self-heal effect to run when
   atBottom=false after a reset. The SDK fires TimelineReset before
   populating the fresh timeline, so getInitialTimeline sees range.end=0.
   Without the atBottom guard bypass the range stays at {0,0} as events
   arrive, leaving the virtual paginator rendering nothing. The ref is
   cleared on first successful heal, after which normal atBottom-gated
   behaviour resumes.
useLiveEventArrive, useRelationUpdate, useLiveTimelineRefresh, and
useThreadUpdate each passed their callback argument into the useEffect
dependency array. This caused the listener to be removed and re-added
every time the callback identity changed (e.g. when unreadInfo toggled
or when any other parent-scope value included in the useCallback deps
changed).

Under normal conditions React's synchronous effect cleanup guarantees
one listener at a time. But during sliding-sync error/retry cycles the
client emits MaxListenersExceededWarning (11 session_ended, 51
RoomState.events) confirming that listeners accumulate. When the
callbacks change fast enough — or React defers cleanup in concurrent
mode — multiple handleTimelineEvent instances are live simultaneously.
Each one independently calls setTimeline({ range: start+1, end+1 }) for
the same arriving event, producing the spurious scroll jumps reported.

Fix: use the stable callback ref pattern in all four hooks.
- Add a ref (onArriveRef / onRelationRef / onRefreshRef / onUpdateRef)
- Assign ref.current = callback on every render (always up-to-date)
- Remove the callback from the useEffect dep array (effect runs only
  when `room` changes)
- Inside handlers, delegate via ref.current() instead of the captured
  closure value

The event listener identity is now stable for the entire lifetime of
each room mount. Listener counts stay at 1 regardless of how many
re-renders occur or how many sync error/retry cycles fire.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant