@@ -22,23 +22,10 @@ export const MIN_TOP_LEVEL_ROWS_PER_FETCH = 30;
2222
2323// Hard ceiling on relay pages fetched in one pass. On reply-heavy channels a
2424// batch yields only a few visible rows, so the row floor alone could dig through
25- // hundreds of messages behind one spinner and commit them at once (a sudden
26- // "flood" with a tiny scrollbar). Capping per-pass keeps each fetch a bounded
27- // page; the scroll observer re-arms to page further while in view.
25+ // hundreds of messages behind one spinner. Capping per-pass keeps each fetch a
26+ // bounded page; the scroll observer re-arms to page further while in view.
2827const MAX_BATCHES_PER_FETCH = 3 ;
2928
30- // Yield a frame so the rows just merged paint before the next round-trip;
31- // otherwise a multi-batch pass renders as one bulk commit at the loop's end.
32- function yieldToPaint ( ) : Promise < void > {
33- return new Promise ( ( resolve ) => {
34- if ( typeof requestAnimationFrame === "function" ) {
35- requestAnimationFrame ( ( ) => resolve ( ) ) ;
36- } else {
37- setTimeout ( resolve , 0 ) ;
38- }
39- } ) ;
40- }
41-
4229export type PageOlderResult = {
4330 /** False once a short relay page proves history is exhausted. */
4431 hasOlderMessages : boolean ;
@@ -68,16 +55,15 @@ export async function pageOlderMessagesUntilRowFloor(
6855 let hasOlderMessages = true ;
6956 let batchesFetched = 0 ;
7057
71- while ( hasOlderMessages && shouldContinue ( ) ) {
72- const before = queryClient . getQueryData < RelayEvent [ ] > ( queryKey ) ?? [ ] ;
73- if ( before . length === 0 ) {
74- break ;
75- }
58+ // Accumulate every batch of this pass and commit to the cache once at the
59+ // end. Committing per batch paints the timeline in several small steps under
60+ // one spinner — on reply-heavy windows each 200-event batch adds only a few
61+ // visible rows, so the user sees the loader dribble messages in 1-5 at a
62+ // time. One commit = one bounded growth step.
63+ const fetched : RelayEvent [ ] = [ ] ;
64+ let oldestTimestamp = baseline [ 0 ] . created_at ;
7665
77- // `until` is inclusive — the relay returns the boundary message again, but
78- // sortMessages dedupes by id. Subtracting 1 risks skipping same-second
79- // messages.
80- const oldestTimestamp = before [ 0 ] . created_at ;
66+ while ( hasOlderMessages && shouldContinue ( ) ) {
8167 const olderMessages = await relayClient . fetchChannelHistoryBefore (
8268 channelId ,
8369 oldestTimestamp ,
@@ -97,25 +83,24 @@ export async function pageOlderMessagesUntilRowFloor(
9783 }
9884
9985 if ( olderMessages . length > 0 ) {
100- queryClient . setQueryData < RelayEvent [ ] > ( queryKey , ( current = [ ] ) =>
101- mergeTimelineHistoryMessages ( current , olderMessages ) ,
102- ) ;
103- void backfillAuxForMessages ( queryClient , channelId , olderMessages ) ;
86+ fetched . push ( ...olderMessages ) ;
10487 }
10588
10689 // Progress guard, not exhaustion: if the oldest timestamp didn't move back
10790 // (empty page, or all-duplicate), stop this pass to avoid re-fetching the
10891 // same `until`.
109- const oldestAfterMerge = ( queryClient . getQueryData < RelayEvent [ ] > (
110- queryKey ,
111- ) ?? [ ] ) [ 0 ] ?. created_at ;
112- if ( oldestAfterMerge === undefined || oldestAfterMerge >= oldestTimestamp ) {
92+ const oldestFetched = fetched . reduce (
93+ ( min , event ) => ( event . created_at < min ? event . created_at : min ) ,
94+ oldestTimestamp ,
95+ ) ;
96+ if ( oldestFetched >= oldestTimestamp ) {
11397 break ;
11498 }
99+ oldestTimestamp = oldestFetched ;
115100
116101 const rowsGained =
117102 countTopLevelTimelineRows (
118- queryClient . getQueryData < RelayEvent [ ] > ( queryKey ) ?? [ ] ,
103+ mergeTimelineHistoryMessages ( baseline , fetched ) ,
119104 ) - baselineRowCount ;
120105 if ( rowsGained >= MIN_TOP_LEVEL_ROWS_PER_FETCH ) {
121106 break ;
@@ -124,8 +109,13 @@ export async function pageOlderMessagesUntilRowFloor(
124109 if ( batchesFetched >= MAX_BATCHES_PER_FETCH ) {
125110 break ;
126111 }
112+ }
127113
128- await yieldToPaint ( ) ;
114+ if ( fetched . length > 0 && shouldContinue ( ) ) {
115+ queryClient . setQueryData < RelayEvent [ ] > ( queryKey , ( current = [ ] ) =>
116+ mergeTimelineHistoryMessages ( current , fetched ) ,
117+ ) ;
118+ void backfillAuxForMessages ( queryClient , channelId , fetched ) ;
129119 }
130120
131121 return { hasOlderMessages } ;
0 commit comments