@tanstack/virtual-core 3.19.10 → 3.19.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.cjs +52 -17
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +7 -1
- package/dist/cjs/scroll-classifier.cjs +14 -2
- package/dist/cjs/scroll-classifier.cjs.map +1 -1
- package/dist/cjs/scroll-classifier.d.cts +3 -2
- package/dist/esm/index.d.ts +7 -1
- package/dist/esm/index.js +52 -17
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/scroll-classifier.d.ts +3 -2
- package/dist/esm/scroll-classifier.js +14 -2
- package/dist/esm/scroll-classifier.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +112 -21
- package/src/scroll-classifier.ts +31 -3
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
type ScrollEventKind = 'spurious' | 'readBack' | 'clampedReadBack' | 'clampedEcho' | 'external';
|
|
1
|
+
type ScrollEventKind = 'spurious' | 'readBack' | 'clampedReadBack' | 'clampedEcho' | 'restEcho' | 'external';
|
|
2
2
|
export interface ScrollEventInput {
|
|
3
3
|
offset: number;
|
|
4
4
|
isScrolling: boolean;
|
|
@@ -11,8 +11,9 @@ export interface ScrollEventInput {
|
|
|
11
11
|
lowMaxes?: Array<number>;
|
|
12
12
|
} | null;
|
|
13
13
|
clampedMaxAtWrite: number | null;
|
|
14
|
+
ownWriteAt: number | null;
|
|
14
15
|
}
|
|
15
|
-
export declare function classifyScrollEvent({ offset, isScrolling, elementMax, holdOffset, scrollOffset, pendingWrite, clampedMaxAtWrite, }: ScrollEventInput): ScrollEventKind;
|
|
16
|
+
export declare function classifyScrollEvent({ offset, isScrolling, elementMax, holdOffset, scrollOffset, pendingWrite, clampedMaxAtWrite, ownWriteAt, }: ScrollEventInput): ScrollEventKind;
|
|
16
17
|
export type ScrollPhase = 'queued' | 'smooth' | 'smoothLanded' | 'smoothFinishing' | 'instant';
|
|
17
18
|
export interface RetireInput {
|
|
18
19
|
kind: ScrollEventKind;
|
|
@@ -6,7 +6,8 @@ function classifyScrollEvent({
|
|
|
6
6
|
holdOffset,
|
|
7
7
|
scrollOffset,
|
|
8
8
|
pendingWrite,
|
|
9
|
-
clampedMaxAtWrite
|
|
9
|
+
clampedMaxAtWrite,
|
|
10
|
+
ownWriteAt
|
|
10
11
|
}) {
|
|
11
12
|
var _a;
|
|
12
13
|
if (isScrolling && pendingWrite === null && offset === scrollOffset) {
|
|
@@ -24,6 +25,9 @@ function classifyScrollEvent({
|
|
|
24
25
|
if (clampedMaxAtWrite !== null && Math.abs(offset - clampedMaxAtWrite) < 1.5) {
|
|
25
26
|
return "clampedEcho";
|
|
26
27
|
}
|
|
28
|
+
if (!isScrolling && ownWriteAt !== null && Math.abs(offset - ownWriteAt) < 1.5) {
|
|
29
|
+
return "restEcho";
|
|
30
|
+
}
|
|
27
31
|
return "external";
|
|
28
32
|
}
|
|
29
33
|
const isAnimating = (phase) => phase === "smooth" || phase === "smoothFinishing";
|
|
@@ -33,6 +37,8 @@ function externalRetiresCommand({
|
|
|
33
37
|
command,
|
|
34
38
|
target
|
|
35
39
|
}) {
|
|
40
|
+
let fresh;
|
|
41
|
+
const freshTarget = () => fresh ?? (fresh = target());
|
|
36
42
|
return kind !== "spurious" && kind !== "readBack" && // Our own write, clamped (`clampedReadBack`, #1310): the command is not
|
|
37
43
|
// retired by it, and reconcile retargets below the new max.
|
|
38
44
|
kind !== "clampedReadBack" && (command == null ? void 0 : command.index) != null && // Nothing has been written for a command waiting for the tail; these
|
|
@@ -45,7 +51,13 @@ function externalRetiresCommand({
|
|
|
45
51
|
// keeps it reconciling (#1294). While a smooth scroll's animation may
|
|
46
52
|
// still report, its events look like a user's, so it yields only once it
|
|
47
53
|
// has landed.
|
|
48
|
-
!isAnimating(command.phase) && !approxEqual(offset, command.lastTargetOffset) && !approxEqual(offset,
|
|
54
|
+
!isAnimating(command.phase) && !approxEqual(offset, command.lastTargetOffset) && !approxEqual(offset, freshTarget()) && // Our own write reported again at rest (`restEcho`): nothing else has
|
|
55
|
+
// moved the element, but reconcile writes only a target that moved. Once
|
|
56
|
+
// the target has moved since the command wrote it, reconcile lands the
|
|
57
|
+
// command there (with `scrollend`, a correction's echo arrives before
|
|
58
|
+
// reconcile has run on the measurements behind it); while it has not,
|
|
59
|
+
// nothing will bring the element back, and the command ends here.
|
|
60
|
+
(kind !== "restEcho" || freshTarget() === command.lastTargetOffset) && // A compensation write's clamped echo (#1258) keeps the command. The
|
|
49
61
|
// order is deliberate, not tidiness: this clause stays after `target()`
|
|
50
62
|
// (and `clampedReadBack` before it) because `target()` can rebuild the
|
|
51
63
|
// measurements, so it must run for exactly the events it always did.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scroll-classifier.js","sources":["../../src/scroll-classifier.ts"],"sourcesContent":["import { approxEqual } from './utils'\n\n// Why a scroll event arrived, decided from the virtualizer's state as it was\n// before the event, before the scroll handler mutates anything. The first\n// match in this order wins:\n// spurious Safari/Firefox re-emit at the position we hold, nothing\n// pending\n// readBack our last write reporting back (within 1.5px of its\n// target)\n// clampedReadBack our last non-smooth write, clamped at the element's\n// current max or at a smaller max the layout passed\n// through since the write\n// clampedEcho a compensation write the browser clamped (#1258)\n// reporting at the max seen at write time\n// external anything else: the user, a fling, a smooth animation\n// frame\ntype ScrollEventKind =\n | 'spurious'\n | 'readBack'\n | 'clampedReadBack'\n | 'clampedEcho'\n | 'external'\n\nexport interface ScrollEventInput {\n // The reported position in layout coordinates (the element's offset plus\n // the held correction and a pending prepend anchor's delta).\n offset: number\n isScrolling: boolean\n // The element's own scroll max, read only while scrolling (null otherwise\n // or where it cannot be read).\n elementMax: number | null\n // How far the element sits behind the layout while a correction is held.\n holdOffset: number\n // The layout position the virtualizer holds.\n scrollOffset: number | null\n // Our last write to the element, until a scroll event reports it, with\n // the scroll maxima below its target the element had since (see\n // `lowMaxes` on `pendingWrite` in the virtualizer).\n pendingWrite: {\n target: number\n smooth: boolean\n lowMaxes?: Array<number>\n } | null\n // The element's max when a clamped compensation write was made (#1258),\n // or null when none is pending.\n clampedMaxAtWrite: number | null\n}\n\nexport function classifyScrollEvent({\n offset,\n isScrolling,\n elementMax,\n holdOffset,\n scrollOffset,\n pendingWrite,\n clampedMaxAtWrite,\n}: ScrollEventInput): ScrollEventKind {\n // A scroll event that reports movement but lands on the offset we already\n // hold — and isn't a self-write read-back — is a spurious no-op re-emit\n // that Safari/Firefox fire after a re-render's layout (Chrome doesn't).\n // Treating it as scrolling re-arms `isScrolling`, which forces a render\n // that triggers another such event: an infinite re-render loop.\n if (isScrolling && pendingWrite === null && offset === scrollOffset) {\n return 'spurious'\n }\n if (pendingWrite !== null && Math.abs(offset - pendingWrite.target) < 1.5) {\n return 'readBack'\n }\n // A non-smooth write past the element's max reads back at the max: ours,\n // clamped because the sizer shrank between the write and the report (rows\n // above the target measured smaller in a same-task render, #1310). The\n // element is where it says. A finger that drags to the max while such a\n // write is pending (iOS ignored it) is read the same way; the command then\n // retires at the next frame, its target clamped to the same max.\n if (\n pendingWrite !== null &&\n !pendingWrite.smooth &&\n elementMax !== null &&\n pendingWrite.target > elementMax + holdOffset + 0.5 &&\n Math.abs(offset - (elementMax + holdOffset)) < 1.5\n ) {\n return 'clampedReadBack'\n }\n // The same, clamped to a smaller max than the element's current one: the\n // sizer shrank below the target after the write (rows measured smaller,\n // a count change) and the browser clamped there, then it grew back, all\n // before the report. The clamp lands on one of those maxima; a position\n // between them is not one.\n if (\n pendingWrite !== null &&\n !pendingWrite.smooth &&\n pendingWrite.lowMaxes?.some((max) => Math.abs(offset - max) < 1.5)\n ) {\n return 'clampedReadBack'\n }\n if (\n clampedMaxAtWrite !== null &&\n Math.abs(offset - clampedMaxAtWrite) < 1.5\n ) {\n return 'clampedEcho'\n }\n return 'external'\n}\n\n// Where an in-flight scroll command is in its lifecycle, which decides how\n// its scroll events and measurements are read:\n// queued the first write waits for the momentum tail to close\n// (iOS 27, see `_startScroll`). Nothing has been written:\n// the fling's scroll events do not retire the command, a\n// drag does, and reconcile only keeps the safety valve\n// smooth a smooth animation is travelling to the target\n// smoothLanded a smooth scroll is at its target, waiting to retire\n// smoothFinishing a smooth scroll switched to instant writes for the\n// final approach; its animation can still report positions\n// instant instant writes\nexport type ScrollPhase =\n | 'queued'\n | 'smooth'\n | 'smoothLanded'\n | 'smoothFinishing'\n | 'instant'\n\n// The smooth animation may still be reporting positions, so a scroll event\n// that isn't the read-back of our own write may not be the user.\nconst isAnimating = (phase: ScrollPhase) =>\n phase === 'smooth' || phase === 'smoothFinishing'\n\nexport interface RetireInput {\n kind: ScrollEventKind\n // The reported position in layout coordinates, as classified.\n offset: number\n // The in-flight scroll command, or null.\n command: {\n index: number | null\n phase: ScrollPhase\n lastTargetOffset: number\n } | null\n // The command's target recomputed against the current layout. Called\n // only once every cheaper clause has passed, and at most once.\n target: () => number\n}\n\n// Whether a scroll event that is not the read-back of our own write retires\n// the in-flight index command: an external scroll moved away from its\n// target. Our own measurement updates (including clamped writes) still need\n// to reconcile.\nexport function externalRetiresCommand({\n kind,\n offset,\n command,\n target,\n}: RetireInput): boolean {\n return (\n kind !== 'spurious' &&\n kind !== 'readBack' &&\n // Our own write, clamped (`clampedReadBack`, #1310): the command is not\n // retired by it, and reconcile retargets below the new max.\n kind !== 'clampedReadBack' &&\n command?.index != null &&\n // Nothing has been written for a command waiting for the tail; these\n // are the fling's events.\n command.phase !== 'queued' &&\n // A non-smooth write lands at once, so any other scroll event is the\n // user's, even before reconcile saw the target reached (the target may\n // keep moving, or iOS ignored the write under a finger). A write the\n // browser clamped because the sizer hasn't grown yet reads back at the\n // max, which is also where the fresh target clamps, so the check below\n // keeps it reconciling (#1294). While a smooth scroll's animation may\n // still report, its events look like a user's, so it yields only once it\n // has landed.\n !isAnimating(command.phase) &&\n !approxEqual(offset, command.lastTargetOffset) &&\n !approxEqual(offset, target()) &&\n // A compensation write's clamped echo (#1258) keeps the command. The\n // order is deliberate, not tidiness: this clause stays after `target()`\n // (and `clampedReadBack` before it) because `target()` can rebuild the\n // measurements, so it must run for exactly the events it always did.\n kind !== 'clampedEcho'\n )\n}\n\n// A scroll position reported at a time, in the element's own coordinates.\nexport interface ScrollSample {\n t: number\n offset: number\n}\n\nconst FLING_WINDOW_MS = 40\n\n// Whether the scroll sped up: a finger flicked again or dragged on during a\n// fling. iOS fires no touch events for a touch that lands on a fling\n// (#1307), so this, and a turn against the fling, is all that shows it.\n// Momentum only slows down. Scroll events arrive 7–26ms apart and some\n// carry two frames, so one event can read 2–3× faster than its neighbours;\n// the speed is taken over the last 40ms and compared with the 40ms before.\n// On an iPhone (27.0.1) the flicks read 2.4–6× faster than the window before\n// and at least 1.5px/ms more, momentum alone at most 1.7× (#1307).\nexport function flingSpedUp(samples: ReadonlyArray<ScrollSample>): boolean {\n const last = samples[samples.length - 1]\n if (last === undefined) return false\n let j = samples.length - 2\n while (j >= 0 && last.t - samples[j]!.t < FLING_WINDOW_MS) j--\n if (j < 0) return false\n const mid = samples[j]!\n let k = j - 1\n while (k >= 0 && mid.t - samples[k]!.t < FLING_WINDOW_MS) k--\n if (k < 0) return false\n const first = samples[k]!\n const recent = (last.offset - mid.offset) / (last.t - mid.t)\n const before = (mid.offset - first.offset) / (mid.t - first.t)\n return (\n recent * before >= 0 &&\n Math.abs(recent) >= 2 * Math.abs(before) &&\n Math.abs(recent) - Math.abs(before) >= 0.8\n )\n}\n\n// Whether a fling that sent its last scroll event was still going at\n// 0.05px/ms or more over its last 40ms: a finger caught it. A fling that\n// ends on its own fades to at most 0.015px/ms first; taps stopped one at\n// 0.21–1.43px/ms (iPhones on 27.0.1, #1307). The finger stays down with no\n// touch events, and iOS undoes a write made under it. A tap in the last\n// pixels of a fling reads as its end.\nexport function flingCaught(samples: ReadonlyArray<ScrollSample>): boolean {\n const last = samples[samples.length - 1]\n if (last === undefined) return false\n let j = samples.length - 2\n while (j >= 0 && last.t - samples[j]!.t < FLING_WINDOW_MS) j--\n if (j < 0) return false\n const from = samples[j]!\n return Math.abs(last.offset - from.offset) / (last.t - from.t) >= 0.05\n}\n"],"names":[],"mappings":";AAgDO,SAAS,oBAAoB;AAAA,EAClC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAAsC;;AAMpC,MAAI,eAAe,iBAAiB,QAAQ,WAAW,cAAc;AACnE,WAAO;AAAA,EACT;AACA,MAAI,iBAAiB,QAAQ,KAAK,IAAI,SAAS,aAAa,MAAM,IAAI,KAAK;AACzE,WAAO;AAAA,EACT;AAOA,MACE,iBAAiB,QACjB,CAAC,aAAa,UACd,eAAe,QACf,aAAa,SAAS,aAAa,aAAa,OAChD,KAAK,IAAI,UAAU,aAAa,WAAW,IAAI,KAC/C;AACA,WAAO;AAAA,EACT;AAMA,MACE,iBAAiB,QACjB,CAAC,aAAa,YACd,kBAAa,aAAb,mBAAuB,KAAK,CAAC,QAAQ,KAAK,IAAI,SAAS,GAAG,IAAI,OAC9D;AACA,WAAO;AAAA,EACT;AACA,MACE,sBAAsB,QACtB,KAAK,IAAI,SAAS,iBAAiB,IAAI,KACvC;AACA,WAAO;AAAA,EACT;AACA,SAAO;AACT;AAsBA,MAAM,cAAc,CAAC,UACnB,UAAU,YAAY,UAAU;AAqB3B,SAAS,uBAAuB;AAAA,EACrC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAAyB;AACvB,SACE,SAAS,cACT,SAAS;AAAA;AAAA,EAGT,SAAS,sBACT,mCAAS,UAAS;AAAA;AAAA,EAGlB,QAAQ,UAAU;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASlB,CAAC,YAAY,QAAQ,KAAK,KAC1B,CAAC,YAAY,QAAQ,QAAQ,gBAAgB,KAC7C,CAAC,YAAY,QAAQ,QAAQ;AAAA;AAAA;AAAA;AAAA,EAK7B,SAAS;AAEb;AAQA,MAAM,kBAAkB;AAUjB,SAAS,YAAY,SAA+C;AACzE,QAAM,OAAO,QAAQ,QAAQ,SAAS,CAAC;AACvC,MAAI,SAAS,OAAW,QAAO;AAC/B,MAAI,IAAI,QAAQ,SAAS;AACzB,SAAO,KAAK,KAAK,KAAK,IAAI,QAAQ,CAAC,EAAG,IAAI,gBAAiB;AAC3D,MAAI,IAAI,EAAG,QAAO;AAClB,QAAM,MAAM,QAAQ,CAAC;AACrB,MAAI,IAAI,IAAI;AACZ,SAAO,KAAK,KAAK,IAAI,IAAI,QAAQ,CAAC,EAAG,IAAI,gBAAiB;AAC1D,MAAI,IAAI,EAAG,QAAO;AAClB,QAAM,QAAQ,QAAQ,CAAC;AACvB,QAAM,UAAU,KAAK,SAAS,IAAI,WAAW,KAAK,IAAI,IAAI;AAC1D,QAAM,UAAU,IAAI,SAAS,MAAM,WAAW,IAAI,IAAI,MAAM;AAC5D,SACE,SAAS,UAAU,KACnB,KAAK,IAAI,MAAM,KAAK,IAAI,KAAK,IAAI,MAAM,KACvC,KAAK,IAAI,MAAM,IAAI,KAAK,IAAI,MAAM,KAAK;AAE3C;AAQO,SAAS,YAAY,SAA+C;AACzE,QAAM,OAAO,QAAQ,QAAQ,SAAS,CAAC;AACvC,MAAI,SAAS,OAAW,QAAO;AAC/B,MAAI,IAAI,QAAQ,SAAS;AACzB,SAAO,KAAK,KAAK,KAAK,IAAI,QAAQ,CAAC,EAAG,IAAI,gBAAiB;AAC3D,MAAI,IAAI,EAAG,QAAO;AAClB,QAAM,OAAO,QAAQ,CAAC;AACtB,SAAO,KAAK,IAAI,KAAK,SAAS,KAAK,MAAM,KAAK,KAAK,IAAI,KAAK,MAAM;AACpE;"}
|
|
1
|
+
{"version":3,"file":"scroll-classifier.js","sources":["../../src/scroll-classifier.ts"],"sourcesContent":["import { approxEqual } from './utils'\n\n// Why a scroll event arrived, decided from the virtualizer's state as it was\n// before the event, before the scroll handler mutates anything. The first\n// match in this order wins:\n// spurious Safari/Firefox re-emit at the position we hold, nothing\n// pending\n// readBack our last write reporting back (within 1.5px of its\n// target)\n// clampedReadBack our last non-smooth write, clamped at the element's\n// current max or at a smaller max the layout passed\n// through since the write\n// clampedEcho a compensation write the browser clamped (#1258)\n// reporting at the max seen at write time\n// restEcho our own write reported again at rest, where the last\n// scroll event reported it\n// external anything else: the user, a fling, a smooth animation\n// frame\ntype ScrollEventKind =\n | 'spurious'\n | 'readBack'\n | 'clampedReadBack'\n | 'clampedEcho'\n | 'restEcho'\n | 'external'\n\nexport interface ScrollEventInput {\n // The reported position in layout coordinates (the element's offset plus\n // the held correction and a pending prepend anchor's delta).\n offset: number\n isScrolling: boolean\n // The element's own scroll max, read only while scrolling (null otherwise\n // or where it cannot be read).\n elementMax: number | null\n // How far the element sits behind the layout while a correction is held.\n holdOffset: number\n // The layout position the virtualizer holds.\n scrollOffset: number | null\n // Our last write to the element, until a scroll event reports it, with\n // the scroll maxima below its target the element had since (see\n // `lowMaxes` on `pendingWrite` in the virtualizer).\n pendingWrite: {\n target: number\n smooth: boolean\n lowMaxes?: Array<number>\n } | null\n // The element's max when a clamped compensation write was made (#1258),\n // or null when none is pending.\n clampedMaxAtWrite: number | null\n // Where the last scroll event reported our own write (read back, clamped\n // or echoed), until a report anywhere else.\n ownWriteAt: number | null\n}\n\nexport function classifyScrollEvent({\n offset,\n isScrolling,\n elementMax,\n holdOffset,\n scrollOffset,\n pendingWrite,\n clampedMaxAtWrite,\n ownWriteAt,\n}: ScrollEventInput): ScrollEventKind {\n // A scroll event that reports movement but lands on the offset we already\n // hold — and isn't a self-write read-back — is a spurious no-op re-emit\n // that Safari/Firefox fire after a re-render's layout (Chrome doesn't).\n // Treating it as scrolling re-arms `isScrolling`, which forces a render\n // that triggers another such event: an infinite re-render loop.\n if (isScrolling && pendingWrite === null && offset === scrollOffset) {\n return 'spurious'\n }\n if (pendingWrite !== null && Math.abs(offset - pendingWrite.target) < 1.5) {\n return 'readBack'\n }\n // A non-smooth write past the element's max reads back at the max: ours,\n // clamped because the sizer shrank between the write and the report (rows\n // above the target measured smaller in a same-task render, #1310). The\n // element is where it says. A finger that drags to the max while such a\n // write is pending (iOS ignored it) is read the same way; the command then\n // retires at the next frame, its target clamped to the same max.\n if (\n pendingWrite !== null &&\n !pendingWrite.smooth &&\n elementMax !== null &&\n pendingWrite.target > elementMax + holdOffset + 0.5 &&\n Math.abs(offset - (elementMax + holdOffset)) < 1.5\n ) {\n return 'clampedReadBack'\n }\n // The same, clamped to a smaller max than the element's current one: the\n // sizer shrank below the target after the write (rows measured smaller,\n // a count change) and the browser clamped there, then it grew back, all\n // before the report. The clamp lands on one of those maxima; a position\n // between them is not one.\n if (\n pendingWrite !== null &&\n !pendingWrite.smooth &&\n pendingWrite.lowMaxes?.some((max) => Math.abs(offset - max) < 1.5)\n ) {\n return 'clampedReadBack'\n }\n if (\n clampedMaxAtWrite !== null &&\n Math.abs(offset - clampedMaxAtWrite) < 1.5\n ) {\n return 'clampedEcho'\n }\n // A write is reported again when the element comes to rest: `scrollend`\n // follows its scroll event in the same frame's scroll steps, before\n // reconcile; the fallback follows it after `isScrollingResetDelay`.\n // Nothing has moved the element in between.\n if (\n !isScrolling &&\n ownWriteAt !== null &&\n Math.abs(offset - ownWriteAt) < 1.5\n ) {\n return 'restEcho'\n }\n return 'external'\n}\n\n// Where an in-flight scroll command is in its lifecycle, which decides how\n// its scroll events and measurements are read:\n// queued the first write waits for the momentum tail to close\n// (iOS 27, see `_startScroll`). Nothing has been written:\n// the fling's scroll events do not retire the command, a\n// drag does, and reconcile only keeps the safety valve\n// smooth a smooth animation is travelling to the target\n// smoothLanded a smooth scroll is at its target, waiting to retire\n// smoothFinishing a smooth scroll switched to instant writes for the\n// final approach; its animation can still report positions\n// instant instant writes\nexport type ScrollPhase =\n | 'queued'\n | 'smooth'\n | 'smoothLanded'\n | 'smoothFinishing'\n | 'instant'\n\n// The smooth animation may still be reporting positions, so a scroll event\n// that isn't the read-back of our own write may not be the user.\nconst isAnimating = (phase: ScrollPhase) =>\n phase === 'smooth' || phase === 'smoothFinishing'\n\nexport interface RetireInput {\n kind: ScrollEventKind\n // The reported position in layout coordinates, as classified.\n offset: number\n // The in-flight scroll command, or null.\n command: {\n index: number | null\n phase: ScrollPhase\n lastTargetOffset: number\n } | null\n // The command's target recomputed against the current layout. Called\n // only once every cheaper clause has passed, and at most once.\n target: () => number\n}\n\n// Whether a scroll event that is not the read-back of our own write retires\n// the in-flight index command: an external scroll moved away from its\n// target, or our own write left the element where reconcile will not move\n// it. Our own measurement updates (including clamped writes) still need to\n// reconcile.\nexport function externalRetiresCommand({\n kind,\n offset,\n command,\n target,\n}: RetireInput): boolean {\n let fresh: number | undefined\n const freshTarget = () => (fresh ??= target())\n return (\n kind !== 'spurious' &&\n kind !== 'readBack' &&\n // Our own write, clamped (`clampedReadBack`, #1310): the command is not\n // retired by it, and reconcile retargets below the new max.\n kind !== 'clampedReadBack' &&\n command?.index != null &&\n // Nothing has been written for a command waiting for the tail; these\n // are the fling's events.\n command.phase !== 'queued' &&\n // A non-smooth write lands at once, so any other scroll event is the\n // user's, even before reconcile saw the target reached (the target may\n // keep moving, or iOS ignored the write under a finger). A write the\n // browser clamped because the sizer hasn't grown yet reads back at the\n // max, which is also where the fresh target clamps, so the check below\n // keeps it reconciling (#1294). While a smooth scroll's animation may\n // still report, its events look like a user's, so it yields only once it\n // has landed.\n !isAnimating(command.phase) &&\n !approxEqual(offset, command.lastTargetOffset) &&\n !approxEqual(offset, freshTarget()) &&\n // Our own write reported again at rest (`restEcho`): nothing else has\n // moved the element, but reconcile writes only a target that moved. Once\n // the target has moved since the command wrote it, reconcile lands the\n // command there (with `scrollend`, a correction's echo arrives before\n // reconcile has run on the measurements behind it); while it has not,\n // nothing will bring the element back, and the command ends here.\n (kind !== 'restEcho' || freshTarget() === command.lastTargetOffset) &&\n // A compensation write's clamped echo (#1258) keeps the command. The\n // order is deliberate, not tidiness: this clause stays after `target()`\n // (and `clampedReadBack` before it) because `target()` can rebuild the\n // measurements, so it must run for exactly the events it always did.\n kind !== 'clampedEcho'\n )\n}\n\n// A scroll position reported at a time, in the element's own coordinates.\nexport interface ScrollSample {\n t: number\n offset: number\n}\n\nconst FLING_WINDOW_MS = 40\n\n// Whether the scroll sped up: a finger flicked again or dragged on during a\n// fling. iOS fires no touch events for a touch that lands on a fling\n// (#1307), so this, and a turn against the fling, is all that shows it.\n// Momentum only slows down. Scroll events arrive 7–26ms apart and some\n// carry two frames, so one event can read 2–3× faster than its neighbours;\n// the speed is taken over the last 40ms and compared with the 40ms before.\n// On an iPhone (27.0.1) the flicks read 2.4–6× faster than the window before\n// and at least 1.5px/ms more, momentum alone at most 1.7× (#1307).\nexport function flingSpedUp(samples: ReadonlyArray<ScrollSample>): boolean {\n const last = samples[samples.length - 1]\n if (last === undefined) return false\n let j = samples.length - 2\n while (j >= 0 && last.t - samples[j]!.t < FLING_WINDOW_MS) j--\n if (j < 0) return false\n const mid = samples[j]!\n let k = j - 1\n while (k >= 0 && mid.t - samples[k]!.t < FLING_WINDOW_MS) k--\n if (k < 0) return false\n const first = samples[k]!\n const recent = (last.offset - mid.offset) / (last.t - mid.t)\n const before = (mid.offset - first.offset) / (mid.t - first.t)\n return (\n recent * before >= 0 &&\n Math.abs(recent) >= 2 * Math.abs(before) &&\n Math.abs(recent) - Math.abs(before) >= 0.8\n )\n}\n\n// Whether a fling that sent its last scroll event was still going at\n// 0.05px/ms or more over its last 40ms: a finger caught it. A fling that\n// ends on its own fades to at most 0.015px/ms first; taps stopped one at\n// 0.21–1.43px/ms (iPhones on 27.0.1, #1307). The finger stays down with no\n// touch events, and iOS undoes a write made under it. A tap in the last\n// pixels of a fling reads as its end.\nexport function flingCaught(samples: ReadonlyArray<ScrollSample>): boolean {\n const last = samples[samples.length - 1]\n if (last === undefined) return false\n let j = samples.length - 2\n while (j >= 0 && last.t - samples[j]!.t < FLING_WINDOW_MS) j--\n if (j < 0) return false\n const from = samples[j]!\n return Math.abs(last.offset - from.offset) / (last.t - from.t) >= 0.05\n}\n"],"names":[],"mappings":"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"}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -300,16 +300,29 @@ export const measureElement = <TItemElement extends Element>(
|
|
|
300
300
|
]
|
|
301
301
|
}
|
|
302
302
|
|
|
303
|
-
const scrollWithAdjustments =
|
|
303
|
+
const scrollWithAdjustments = <
|
|
304
|
+
TScrollElement extends Element | Window,
|
|
305
|
+
TItemElement extends Element,
|
|
306
|
+
>(
|
|
304
307
|
offset: number,
|
|
305
308
|
{
|
|
306
309
|
adjustments = 0,
|
|
307
310
|
behavior,
|
|
308
311
|
}: { adjustments?: number; behavior?: ScrollBehavior },
|
|
309
|
-
instance: Virtualizer<
|
|
312
|
+
instance: Virtualizer<TScrollElement, TItemElement>,
|
|
310
313
|
) => {
|
|
311
|
-
instance
|
|
312
|
-
|
|
314
|
+
const { scrollElement, options } = instance
|
|
315
|
+
const adjustedOffset = offset + adjustments
|
|
316
|
+
const isRtlElement =
|
|
317
|
+
options.horizontal &&
|
|
318
|
+
options.isRtl &&
|
|
319
|
+
scrollElement !== null &&
|
|
320
|
+
'ownerDocument' in scrollElement
|
|
321
|
+
|
|
322
|
+
scrollElement?.scrollTo?.({
|
|
323
|
+
[options.horizontal ? 'left' : 'top']: isRtlElement
|
|
324
|
+
? -adjustedOffset
|
|
325
|
+
: adjustedOffset,
|
|
313
326
|
behavior,
|
|
314
327
|
})
|
|
315
328
|
}
|
|
@@ -536,6 +549,13 @@ export class Virtualizer<
|
|
|
536
549
|
// (`followOnAppend does not follow 'trim only'`; reading the element at
|
|
537
550
|
// write time still moves 49 of 1080 snapshot seeds).
|
|
538
551
|
private _reportedOverscroll = false
|
|
552
|
+
// How far the scroll element extends past the end of the list: other
|
|
553
|
+
// content that shares it (#1371). Read with the element's scroll range on
|
|
554
|
+
// scroll reports and on attach, against the sizer the element has (the
|
|
555
|
+
// last `getTotalSize()` an adapter drew), not a layout that measurements
|
|
556
|
+
// moved since.
|
|
557
|
+
private _trailing = 0
|
|
558
|
+
private _drawnListEnd = Infinity
|
|
539
559
|
// The touch gesture in progress (see `IosGesture`). Its tail closing on
|
|
540
560
|
// its own makes a write possible again: the last momentum frame may have
|
|
541
561
|
// come before the timer. The hold is released first, so a command that
|
|
@@ -613,6 +633,10 @@ export class Virtualizer<
|
|
|
613
633
|
smooth: boolean
|
|
614
634
|
lowMaxes?: Array<number>
|
|
615
635
|
} | null = null
|
|
636
|
+
// Where the last scroll event reported our own write, until a report
|
|
637
|
+
// anywhere else. The browser reports the write again when the element
|
|
638
|
+
// comes to rest (`restEcho`).
|
|
639
|
+
private _ownWriteAt: number | null = null
|
|
616
640
|
// A compensation write from `applyScrollAdjustment` whose target exceeded
|
|
617
641
|
// the element's scroll max at the moment of the write. The browser clamps
|
|
618
642
|
// such a write because the consumer's sizer has not grown yet: an
|
|
@@ -936,7 +960,7 @@ export class Virtualizer<
|
|
|
936
960
|
// the rows and the total shifted back by it, so the reader's row
|
|
937
961
|
// stays put, and the flush writes it once a write is possible.
|
|
938
962
|
this._holdOffset += anchorDelta
|
|
939
|
-
this.
|
|
963
|
+
this._followAnchor(anchorDelta)
|
|
940
964
|
}
|
|
941
965
|
}
|
|
942
966
|
}
|
|
@@ -1056,10 +1080,12 @@ export class Virtualizer<
|
|
|
1056
1080
|
// its next touch cycle.
|
|
1057
1081
|
this._holdOffset = 0
|
|
1058
1082
|
this._reportedOverscroll = false
|
|
1083
|
+
this._trailing = 0
|
|
1059
1084
|
// (The gesture was ended with its listeners, in the unsubs above.)
|
|
1060
1085
|
this._clampedAdjustment = null
|
|
1061
1086
|
// A write to the old element will never be read back.
|
|
1062
1087
|
this.pendingWrite = null
|
|
1088
|
+
this._ownWriteAt = null
|
|
1063
1089
|
this.scrollElement = null
|
|
1064
1090
|
this.targetWindow = null
|
|
1065
1091
|
}
|
|
@@ -1108,6 +1134,7 @@ export class Virtualizer<
|
|
|
1108
1134
|
// judged only while scrolling: at rest the element has clamped,
|
|
1109
1135
|
// and a stale sizer makes no bounce.
|
|
1110
1136
|
const elementMax = isScrolling ? this._elementMaxScrollOffset() : null
|
|
1137
|
+
this._readTrailing(elementMax)
|
|
1111
1138
|
const wasOverscrolled = this._reportedOverscroll
|
|
1112
1139
|
this._reportedOverscroll =
|
|
1113
1140
|
offset < 0 || (elementMax !== null && offset > elementMax + 1)
|
|
@@ -1148,6 +1175,17 @@ export class Virtualizer<
|
|
|
1148
1175
|
this.scrollState = null
|
|
1149
1176
|
}
|
|
1150
1177
|
this.pendingWrite = null
|
|
1178
|
+
// Remember where our own write was reported. Its echo at rest
|
|
1179
|
+
// leaves that as it is; any other report clears it.
|
|
1180
|
+
if (
|
|
1181
|
+
kind === 'readBack' ||
|
|
1182
|
+
kind === 'clampedReadBack' ||
|
|
1183
|
+
kind === 'clampedEcho'
|
|
1184
|
+
) {
|
|
1185
|
+
this._ownWriteAt = offset
|
|
1186
|
+
} else if (kind !== 'restEcho') {
|
|
1187
|
+
this._ownWriteAt = null
|
|
1188
|
+
}
|
|
1151
1189
|
|
|
1152
1190
|
// A pending clamped compensation write (#1258) survives only its
|
|
1153
1191
|
// own read-back, which the browser reports at the scroll max we
|
|
@@ -1246,6 +1284,10 @@ export class Virtualizer<
|
|
|
1246
1284
|
}
|
|
1247
1285
|
|
|
1248
1286
|
this._scrollToOffset(this.getScrollOffset())
|
|
1287
|
+
// The rect observer has notified already; reading what follows the
|
|
1288
|
+
// list can change the range again.
|
|
1289
|
+
this._readTrailing(this._elementMaxScrollOffset())
|
|
1290
|
+
this.maybeNotify()
|
|
1249
1291
|
}
|
|
1250
1292
|
|
|
1251
1293
|
// The consumer has committed the sizer.
|
|
@@ -1266,14 +1308,11 @@ export class Virtualizer<
|
|
|
1266
1308
|
// an update scheduled from a layout effect commits before paint.
|
|
1267
1309
|
if (!this._canWriteScroll()) {
|
|
1268
1310
|
this._holdOffset += anchor.delta
|
|
1269
|
-
this.
|
|
1311
|
+
this._followAnchor(anchor.delta)
|
|
1270
1312
|
this.notify(false)
|
|
1271
1313
|
} else if (
|
|
1272
1314
|
isSmoothScroll(this.scrollState) &&
|
|
1273
|
-
!
|
|
1274
|
-
this.getScrollOffset() - anchor.delta,
|
|
1275
|
-
this.scrollState.lastTargetOffset,
|
|
1276
|
-
)
|
|
1315
|
+
!this._landedBeforeAnchor(this.scrollState, anchor.delta)
|
|
1277
1316
|
) {
|
|
1278
1317
|
// A smooth programmatic scroll is still travelling. Writing scrollTop
|
|
1279
1318
|
// here would cancel the browser's animation, and Chromium drops a
|
|
@@ -1281,10 +1320,10 @@ export class Virtualizer<
|
|
|
1281
1320
|
// the journey would be stranded. The target is index-based and
|
|
1282
1321
|
// recomputes against the new layout in reconcileScroll, so let the
|
|
1283
1322
|
// animation run; the next scroll event re-syncs the tracked offset.
|
|
1284
|
-
// A smooth scroll that has already landed (
|
|
1285
|
-
//
|
|
1323
|
+
// A smooth scroll that has already landed (reconcile not yet
|
|
1324
|
+
// retired it) still gets the anchor sync.
|
|
1286
1325
|
} else {
|
|
1287
|
-
this.
|
|
1326
|
+
this._followAnchor(anchor.delta)
|
|
1288
1327
|
this._scrollToOffset(this.getScrollOffset())
|
|
1289
1328
|
}
|
|
1290
1329
|
} else {
|
|
@@ -1319,6 +1358,7 @@ export class Virtualizer<
|
|
|
1319
1358
|
scrollOffset: this.scrollOffset,
|
|
1320
1359
|
pendingWrite: this.pendingWrite,
|
|
1321
1360
|
clampedMaxAtWrite: this._clampedAdjustment?.maxAtWrite ?? null,
|
|
1361
|
+
ownWriteAt: this._ownWriteAt,
|
|
1322
1362
|
})
|
|
1323
1363
|
|
|
1324
1364
|
// Re-issue a compensation write the browser clamped because the sizer had
|
|
@@ -1863,16 +1903,19 @@ export class Virtualizer<
|
|
|
1863
1903
|
// scrollOffset is measured in the scroll element and the layout total
|
|
1864
1904
|
// from the list start, so the list ends scrollMargin further down.
|
|
1865
1905
|
this._getLayoutTotalSize() + this.options.scrollMargin,
|
|
1906
|
+
this._trailing,
|
|
1866
1907
|
],
|
|
1867
|
-
(measurements, outerSize, scrollOffset, lanes, listEnd) => {
|
|
1908
|
+
(measurements, outerSize, scrollOffset, lanes, listEnd, trailing) => {
|
|
1868
1909
|
if (measurements.length === 0 || outerSize === 0) {
|
|
1869
1910
|
this.range = null
|
|
1870
1911
|
return null
|
|
1871
1912
|
}
|
|
1872
1913
|
// Until the element reports a clamped position (the list shrank, a
|
|
1873
1914
|
// rubber-band bounce), the offset can sit past the last row, which
|
|
1874
|
-
// would leave only that row in the range.
|
|
1875
|
-
|
|
1915
|
+
// would leave only that row in the range. The element ends `trailing`
|
|
1916
|
+
// past the list where other content follows it, and an offset up to
|
|
1917
|
+
// there is where the element is.
|
|
1918
|
+
const maxScrollOffset = Math.max(listEnd + trailing - outerSize, 0)
|
|
1876
1919
|
this.range = calculateRangeImpl(
|
|
1877
1920
|
measurements,
|
|
1878
1921
|
outerSize,
|
|
@@ -2136,6 +2179,17 @@ export class Virtualizer<
|
|
|
2136
2179
|
// `_willUpdate` (#1266).
|
|
2137
2180
|
this._flushHoldIfReady(true)
|
|
2138
2181
|
this._retryClampedAdjustment()
|
|
2182
|
+
} else if (
|
|
2183
|
+
this.options.lanes > 1 &&
|
|
2184
|
+
!this.options.useCachedMeasurements &&
|
|
2185
|
+
!this.laneAssignments.has(index)
|
|
2186
|
+
) {
|
|
2187
|
+
// Measured at its estimate, so no rebuild will cache its lane. A single
|
|
2188
|
+
// lane has none to cache, a size under useCachedMeasurements is not a
|
|
2189
|
+
// measurement, and a lane already cached (as always in 'estimate' mode)
|
|
2190
|
+
// must not change.
|
|
2191
|
+
const item = this.getMeasurements()[index]
|
|
2192
|
+
if (item) this.laneAssignments.set(index, item.lane)
|
|
2139
2193
|
}
|
|
2140
2194
|
}
|
|
2141
2195
|
|
|
@@ -2228,6 +2282,12 @@ export class Virtualizer<
|
|
|
2228
2282
|
return offset
|
|
2229
2283
|
}
|
|
2230
2284
|
|
|
2285
|
+
private _readTrailing = (elementMax: number | null) => {
|
|
2286
|
+
if (elementMax === null) return
|
|
2287
|
+
const trailing = elementMax + this.getSize() - this._drawnListEnd
|
|
2288
|
+
this._trailing = trailing > 0 ? trailing : 0
|
|
2289
|
+
}
|
|
2290
|
+
|
|
2231
2291
|
// The element's own scroll range, or null where it cannot be read (no
|
|
2232
2292
|
// element, or a bare test double without `scrollHeight` / `document`).
|
|
2233
2293
|
private _elementMaxScrollOffset = (): number | null => {
|
|
@@ -2376,7 +2436,9 @@ export class Virtualizer<
|
|
|
2376
2436
|
const size = this.getSize()
|
|
2377
2437
|
const scrollOffset = this.getScrollOffset()
|
|
2378
2438
|
|
|
2379
|
-
|
|
2439
|
+
// Not `measurementsCache`: that field is only rebuilt by getMeasurements,
|
|
2440
|
+
// so it lags a size change until the next render reads the items.
|
|
2441
|
+
const item = this.getMeasurements()[index]
|
|
2380
2442
|
if (!item) return
|
|
2381
2443
|
|
|
2382
2444
|
if (align === 'auto') {
|
|
@@ -2583,18 +2645,21 @@ export class Virtualizer<
|
|
|
2583
2645
|
// The size the consumer gives the container: the layout's total, short by
|
|
2584
2646
|
// a held correction so the rows exposed shifted by it still fill it (see
|
|
2585
2647
|
// `_holdOffset`).
|
|
2586
|
-
getTotalSize = () =>
|
|
2587
|
-
Math.max(this._getLayoutTotalSize() - this._holdOffset, 0)
|
|
2648
|
+
getTotalSize = () => {
|
|
2649
|
+
const total = Math.max(this._getLayoutTotalSize() - this._holdOffset, 0)
|
|
2650
|
+
this._drawnListEnd = total + this.options.scrollMargin
|
|
2651
|
+
return total
|
|
2652
|
+
}
|
|
2588
2653
|
|
|
2589
2654
|
private _getLayoutTotalSize = () => {
|
|
2590
2655
|
const measurements = this.getMeasurements()
|
|
2591
2656
|
|
|
2592
2657
|
let end: number
|
|
2593
|
-
// If there are no measurements,
|
|
2658
|
+
// If there are no measurements, include the scroll origin and start padding
|
|
2594
2659
|
// If there is only one lane, use the last measurement's end
|
|
2595
2660
|
// Otherwise find the maximum end value among all measurements
|
|
2596
2661
|
if (measurements.length === 0) {
|
|
2597
|
-
end = this.options.paddingStart
|
|
2662
|
+
end = this.options.scrollMargin + this.options.paddingStart
|
|
2598
2663
|
} else if (this.options.lanes === 1) {
|
|
2599
2664
|
// Fast path: read last item's end directly from the flat typed array
|
|
2600
2665
|
// when available; avoids a Proxy.get + VirtualItem materialization
|
|
@@ -2723,6 +2788,32 @@ export class Virtualizer<
|
|
|
2723
2788
|
}
|
|
2724
2789
|
}
|
|
2725
2790
|
|
|
2791
|
+
// Whether a command had landed where a prepend's anchor (`delta`, already
|
|
2792
|
+
// in `getScrollOffset()`) found the viewport: it has written (a `queued`
|
|
2793
|
+
// command has not, wherever the fling is), and reconcile has seen it at
|
|
2794
|
+
// its target or the viewport is where it last wrote. A correction since
|
|
2795
|
+
// then can have moved the viewport and the target together.
|
|
2796
|
+
private _landedBeforeAnchor = (state: ScrollState, delta: number) =>
|
|
2797
|
+
state.phase !== 'queued' &&
|
|
2798
|
+
(state.stableFrames > 0 ||
|
|
2799
|
+
approxEqual(this.getScrollOffset() - delta, state.lastTargetOffset))
|
|
2800
|
+
|
|
2801
|
+
// A prepend's anchor moves the viewport with the rows it shows. An offset
|
|
2802
|
+
// target moves with it. An index command that had landed is done: the
|
|
2803
|
+
// reader stays on the rows it was aimed at, and its index now names
|
|
2804
|
+
// others. An end target is still the end.
|
|
2805
|
+
private _followAnchor = (delta: number) => {
|
|
2806
|
+
this._followOffsetTarget(delta)
|
|
2807
|
+
const state = this.scrollState
|
|
2808
|
+
if (
|
|
2809
|
+
state?.index != null &&
|
|
2810
|
+
!state.toEnd &&
|
|
2811
|
+
this._landedBeforeAnchor(state, delta)
|
|
2812
|
+
) {
|
|
2813
|
+
this.scrollState = null
|
|
2814
|
+
}
|
|
2815
|
+
}
|
|
2816
|
+
|
|
2726
2817
|
measure = () => {
|
|
2727
2818
|
// Reset pendingMin so the next getMeasurements rebuilds from index 0.
|
|
2728
2819
|
// Without this, a prior resizeItem() that left pendingMin > 0 would
|
package/src/scroll-classifier.ts
CHANGED
|
@@ -12,6 +12,8 @@ import { approxEqual } from './utils'
|
|
|
12
12
|
// through since the write
|
|
13
13
|
// clampedEcho a compensation write the browser clamped (#1258)
|
|
14
14
|
// reporting at the max seen at write time
|
|
15
|
+
// restEcho our own write reported again at rest, where the last
|
|
16
|
+
// scroll event reported it
|
|
15
17
|
// external anything else: the user, a fling, a smooth animation
|
|
16
18
|
// frame
|
|
17
19
|
type ScrollEventKind =
|
|
@@ -19,6 +21,7 @@ type ScrollEventKind =
|
|
|
19
21
|
| 'readBack'
|
|
20
22
|
| 'clampedReadBack'
|
|
21
23
|
| 'clampedEcho'
|
|
24
|
+
| 'restEcho'
|
|
22
25
|
| 'external'
|
|
23
26
|
|
|
24
27
|
export interface ScrollEventInput {
|
|
@@ -44,6 +47,9 @@ export interface ScrollEventInput {
|
|
|
44
47
|
// The element's max when a clamped compensation write was made (#1258),
|
|
45
48
|
// or null when none is pending.
|
|
46
49
|
clampedMaxAtWrite: number | null
|
|
50
|
+
// Where the last scroll event reported our own write (read back, clamped
|
|
51
|
+
// or echoed), until a report anywhere else.
|
|
52
|
+
ownWriteAt: number | null
|
|
47
53
|
}
|
|
48
54
|
|
|
49
55
|
export function classifyScrollEvent({
|
|
@@ -54,6 +60,7 @@ export function classifyScrollEvent({
|
|
|
54
60
|
scrollOffset,
|
|
55
61
|
pendingWrite,
|
|
56
62
|
clampedMaxAtWrite,
|
|
63
|
+
ownWriteAt,
|
|
57
64
|
}: ScrollEventInput): ScrollEventKind {
|
|
58
65
|
// A scroll event that reports movement but lands on the offset we already
|
|
59
66
|
// hold — and isn't a self-write read-back — is a spurious no-op re-emit
|
|
@@ -99,6 +106,17 @@ export function classifyScrollEvent({
|
|
|
99
106
|
) {
|
|
100
107
|
return 'clampedEcho'
|
|
101
108
|
}
|
|
109
|
+
// A write is reported again when the element comes to rest: `scrollend`
|
|
110
|
+
// follows its scroll event in the same frame's scroll steps, before
|
|
111
|
+
// reconcile; the fallback follows it after `isScrollingResetDelay`.
|
|
112
|
+
// Nothing has moved the element in between.
|
|
113
|
+
if (
|
|
114
|
+
!isScrolling &&
|
|
115
|
+
ownWriteAt !== null &&
|
|
116
|
+
Math.abs(offset - ownWriteAt) < 1.5
|
|
117
|
+
) {
|
|
118
|
+
return 'restEcho'
|
|
119
|
+
}
|
|
102
120
|
return 'external'
|
|
103
121
|
}
|
|
104
122
|
|
|
@@ -142,14 +160,17 @@ export interface RetireInput {
|
|
|
142
160
|
|
|
143
161
|
// Whether a scroll event that is not the read-back of our own write retires
|
|
144
162
|
// the in-flight index command: an external scroll moved away from its
|
|
145
|
-
// target
|
|
146
|
-
// to
|
|
163
|
+
// target, or our own write left the element where reconcile will not move
|
|
164
|
+
// it. Our own measurement updates (including clamped writes) still need to
|
|
165
|
+
// reconcile.
|
|
147
166
|
export function externalRetiresCommand({
|
|
148
167
|
kind,
|
|
149
168
|
offset,
|
|
150
169
|
command,
|
|
151
170
|
target,
|
|
152
171
|
}: RetireInput): boolean {
|
|
172
|
+
let fresh: number | undefined
|
|
173
|
+
const freshTarget = () => (fresh ??= target())
|
|
153
174
|
return (
|
|
154
175
|
kind !== 'spurious' &&
|
|
155
176
|
kind !== 'readBack' &&
|
|
@@ -170,7 +191,14 @@ export function externalRetiresCommand({
|
|
|
170
191
|
// has landed.
|
|
171
192
|
!isAnimating(command.phase) &&
|
|
172
193
|
!approxEqual(offset, command.lastTargetOffset) &&
|
|
173
|
-
!approxEqual(offset,
|
|
194
|
+
!approxEqual(offset, freshTarget()) &&
|
|
195
|
+
// Our own write reported again at rest (`restEcho`): nothing else has
|
|
196
|
+
// moved the element, but reconcile writes only a target that moved. Once
|
|
197
|
+
// the target has moved since the command wrote it, reconcile lands the
|
|
198
|
+
// command there (with `scrollend`, a correction's echo arrives before
|
|
199
|
+
// reconcile has run on the measurements behind it); while it has not,
|
|
200
|
+
// nothing will bring the element back, and the command ends here.
|
|
201
|
+
(kind !== 'restEcho' || freshTarget() === command.lastTargetOffset) &&
|
|
174
202
|
// A compensation write's clamped echo (#1258) keeps the command. The
|
|
175
203
|
// order is deliberate, not tidiness: this clause stays after `target()`
|
|
176
204
|
// (and `clampedReadBack` before it) because `target()` can rebuild the
|