Skip to content

Commit 8c8812b

Browse files
committed
fix(mobile): continue source recovery through tall rows
1 parent de9d73e commit 8c8812b

3 files changed

Lines changed: 65 additions & 21 deletions

File tree

‎apps/mobile/src/features/files/SourceFileSurface.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ function JavaScriptSourceFileSurface(props: SourceFileSurfaceProps) {
356356
lineScroll.onScroll(
357357
event.nativeEvent.contentOffset.y,
358358
event.nativeEvent.layoutMeasurement.height,
359+
listRef.current,
359360
)
360361
}
361362
onScrollBeginDrag={lineScroll.cancel}

‎apps/mobile/src/features/files/source-file-scroll.test.ts‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,14 @@ function setup() {
5353
replaceDocument(count: number) {
5454
lineCount = count;
5555
},
56+
reportScroll(offset: number, height: number) {
57+
scroll.onScroll(offset, height, mounted ? list : null);
58+
},
5659
reportViewable(offset: number, height: number, indices: readonly number[]) {
5760
visibleIndex = indices[0] ?? visibleIndex;
5861
highestMeasuredIndex = Math.max(highestMeasuredIndex, ...indices);
5962
if (unmeasuredIndex !== null && indices.includes(unmeasuredIndex)) unmeasuredIndex = null;
60-
scroll.onScroll(offset, height);
63+
scroll.onScroll(offset, height, mounted ? list : null);
6164
scroll.onViewableItemsChanged(
6265
mounted ? list : null,
6366
indices.map((index) => ({ index })),
@@ -172,3 +175,35 @@ it("finishes an already visible target without waiting for another viewability e
172175
state.measureThrough(100);
173176
expect(state.visibleIndex).toBe(20);
174177
});
178+
179+
it("keeps moving through a tall wrapped row without changed viewable indices", async () => {
180+
const state = setup();
181+
state.acceptApproximationFor(899);
182+
state.scroll.scrollToLine(899);
183+
state.reportViewable(21_000, 240, [898]);
184+
const firstOffset = state.requestedOffset!;
185+
186+
state.reportScroll(firstOffset, 240);
187+
await Promise.resolve();
188+
expect(state.requestedOffset).toBeGreaterThan(firstOffset);
189+
const nextOffset = state.requestedOffset!;
190+
state.reportScroll(nextOffset, 240);
191+
await Promise.resolve();
192+
expect(state.requestedOffset).toBeGreaterThan(nextOffset);
193+
194+
state.reportViewable(22_000, 240, [899]);
195+
expect(state.visibleIndex).toBe(899);
196+
});
197+
198+
it("cancels queued scroll recovery when the source list unmounts", async () => {
199+
const state = setup();
200+
state.acceptApproximationFor(899);
201+
state.scroll.scrollToLine(899);
202+
state.reportViewable(21_000, 240, [898]);
203+
const offset = state.requestedOffset!;
204+
205+
state.reportScroll(offset, 240);
206+
state.unmount();
207+
await Promise.resolve();
208+
expect(state.requestedOffset).toBe(offset);
209+
});

‎apps/mobile/src/features/files/source-file-scroll.ts‎

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,26 @@ export function createSourceLineScroll() {
2525
list?.scrollToIndex({ index, animated: false, viewPosition: 0.3 });
2626
if (visibleItems.some((item) => item.index === index)) pendingIndex = null;
2727
};
28+
const recoverFromViewport = (list: SourceLineList | null) => {
29+
const index = target(list);
30+
if (index === null) return;
31+
if (visibleItems.some((item) => item.index === index)) {
32+
pendingIndex = null;
33+
list?.scrollToIndex({ index, animated: false, viewPosition: 0.3 });
34+
return;
35+
}
36+
const first = visibleItems[0]?.index;
37+
const last = visibleItems.at(-1)?.index;
38+
if (first == null || last == null || last < first) return;
39+
// An accepted scroll can still use an estimate for an unmeasured row.
40+
// Correct from the actual viewport rather than repeating that same estimate.
41+
const rowHeight = viewportHeight > 0 ? viewportHeight / (last - first + 1) : averageItemLength;
42+
if (rowHeight <= 0) return;
43+
list?.scrollToOffset({
44+
offset: Math.max(0, scrollOffset + (index - first) * rowHeight),
45+
animated: false,
46+
});
47+
};
2848

2949
return {
3050
scrollToLine,
@@ -45,34 +65,22 @@ export function createSourceLineScroll() {
4565
const index = target(list);
4666
if (index !== null) scrollToLine(list, index);
4767
},
48-
onScroll(offset: number, height: number) {
68+
onScroll(offset: number, height: number, list: SourceLineList | null) {
69+
const moved = offset !== scrollOffset;
4970
scrollOffset = offset;
5071
viewportHeight = height;
72+
if (moved && pendingIndex !== null) {
73+
// RN updates viewability after our scroll handler, and skips its callback
74+
// while the viewport stays inside one tall row. Use the updated rows.
75+
queueMicrotask(() => recoverFromViewport(list));
76+
}
5177
},
5278
onViewableItemsChanged(
5379
list: SourceLineList | null,
5480
items: ReadonlyArray<{ readonly index?: number | null }>,
5581
) {
5682
visibleItems = items;
57-
const index = target(list);
58-
if (index === null) return;
59-
if (items.some((item) => item.index === index)) {
60-
pendingIndex = null;
61-
list?.scrollToIndex({ index, animated: false, viewPosition: 0.3 });
62-
return;
63-
}
64-
const first = items[0]?.index;
65-
const last = items.at(-1)?.index;
66-
if (first == null || last == null || last < first) return;
67-
// An accepted scroll can still use an estimate for an unmeasured row.
68-
// Correct from the actual viewport rather than repeating that same estimate.
69-
const rowHeight =
70-
viewportHeight > 0 ? viewportHeight / (last - first + 1) : averageItemLength;
71-
if (rowHeight <= 0) return;
72-
list?.scrollToOffset({
73-
offset: Math.max(0, scrollOffset + (index - first) * rowHeight),
74-
animated: false,
75-
});
83+
recoverFromViewport(list);
7684
},
7785
cancel() {
7886
pendingIndex = null;

0 commit comments

Comments
 (0)