@@ -145,25 +145,6 @@ const REPLACE_FILE_COUNTS_CSS = `
145145
146146const NO_SLICES : ReadonlyArray < DiffSlice > = [ ] ;
147147
148- function sameSlice ( slice : DiffSlice , page : DiffSlice ) {
149- return (
150- slice . cursor === page . cursor &&
151- slice . patch === page . patch &&
152- slice . truncated === page . truncated &&
153- slice . nextCursor === page . nextCursor &&
154- slice . omittedFileStats . length === page . omittedFileStats . length &&
155- slice . omittedFileStats . every ( ( file , index ) => {
156- const refreshed = page . omittedFileStats [ index ] ;
157- return (
158- refreshed !== undefined &&
159- refreshed . path === file . path &&
160- refreshed . additions === file . additions &&
161- refreshed . deletions === file . deletions
162- ) ;
163- } )
164- ) ;
165- }
166-
167148// Each slice is parsed once per cache key and the parse lives as long as the slice does, so a
168149// slice arriving costs one parse rather than one per slice already on screen.
169150const sliceParses = new WeakMap < DiffSlice , Map < string , RenderablePatch | null > > ( ) ;
@@ -357,31 +338,55 @@ function PullRequestCodeTab({
357338 } ) ,
358339 ) ;
359340 // Pages accumulate into slices as each answer arrives, so the slice list is adjusted while
360- // rendering the answer rather than one commit later. Whether an answer is in is read from the
361- // slices: an equal refetch keeps the answer's identity, so it cannot signal a reset's refill.
341+ // rendering the answer rather than one commit later.
342+ const [ appliedPage , setAppliedPage ] = useState < {
343+ readonly data : typeof diffQuery . data ;
344+ readonly cursor : string | null ;
345+ readonly scopeKey : string ;
346+ } | null > ( null ) ;
362347 const data = diffQuery . data ;
363- const page : DiffSlice | null =
364- data === null
365- ? null
366- : {
367- cursor,
368- patch : data . patch ,
369- truncated : data . truncated ,
370- nextCursor : data . nextCursor ,
371- omittedFileStats : data . omittedFileStats ?? [ ] ,
372- } ;
373- if ( page !== null && ! loadedSlices . some ( ( slice ) => sameSlice ( slice , page ) ) ) {
348+ if (
349+ data !== null &&
350+ ( appliedPage ?. data !== data ||
351+ appliedPage . cursor !== cursor ||
352+ appliedPage . scopeKey !== scopeKey )
353+ ) {
354+ setAppliedPage ( { data, cursor, scopeKey } ) ;
374355 setSliceState ( ( previous ) => {
375356 const slices = previous . key === scopeKey ? previous . slices : NO_SLICES ;
357+ const next = {
358+ cursor,
359+ patch : data . patch ,
360+ truncated : data . truncated ,
361+ nextCursor : data . nextCursor ,
362+ omittedFileStats : data . omittedFileStats ?? [ ] ,
363+ } ;
376364 const index = slices . findIndex ( ( slice ) => slice . cursor === cursor ) ;
377365 if ( index === - 1 ) {
378- return { key : scopeKey , cursor, slices : [ ...slices , page ] } ;
366+ return { key : scopeKey , cursor, slices : [ ...slices , next ] } ;
379367 }
380368 const existing = slices [ index ] ;
381- if ( existing !== undefined && sameSlice ( existing , page ) ) return previous ;
369+ if (
370+ existing !== undefined &&
371+ existing . patch === next . patch &&
372+ existing . truncated === next . truncated &&
373+ existing . nextCursor === next . nextCursor &&
374+ existing . omittedFileStats . length === next . omittedFileStats . length &&
375+ existing . omittedFileStats . every ( ( file , index ) => {
376+ const refreshed = next . omittedFileStats [ index ] ;
377+ return (
378+ refreshed !== undefined &&
379+ refreshed . path === file . path &&
380+ refreshed . additions === file . additions &&
381+ refreshed . deletions === file . deletions
382+ ) ;
383+ } )
384+ ) {
385+ return previous ;
386+ }
382387 // A page that came back different means the diff moved under the review. The slices
383388 // after it go with the replacement: their cursors were positions in the old diff.
384- return { key : scopeKey , cursor, slices : [ ...slices . slice ( 0 , index ) , page ] } ;
389+ return { key : scopeKey , cursor, slices : [ ...slices . slice ( 0 , index ) , next ] } ;
385390 } ) ;
386391 }
387392 const refreshFirstDiffPage = useAtomRefresh (
@@ -426,6 +431,9 @@ function PullRequestCodeTab({
426431 if ( appliedRefreshToken . current === refreshToken ) return ;
427432 appliedRefreshToken . current = refreshToken ;
428433 setSliceState ( { key : scopeKey , cursor : null , slices : NO_SLICES } ) ;
434+ // An equal refetch keeps the answer's identity, so the emptied slices refill only if the
435+ // applied page is forgotten too.
436+ setAppliedPage ( null ) ;
429437 refreshFirstDiffPage ( ) ;
430438 refreshFilesViewed ( ) ;
431439 } , [ refreshToken , scopeKey , refreshFirstDiffPage , refreshFilesViewed ] ) ;
0 commit comments