@tanstack/virtual-core 3.19.2 → 3.19.4

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.
@@ -17,6 +17,22 @@ const getRect = (element) => {
17
17
  const { offsetWidth, offsetHeight } = element;
18
18
  return { width: offsetWidth, height: offsetHeight };
19
19
  };
20
+ const createFrameQueue = (targetWindow) => {
21
+ const ids = /* @__PURE__ */ new Set();
22
+ return {
23
+ schedule: (fn) => {
24
+ const id = targetWindow.requestAnimationFrame(() => {
25
+ ids.delete(id);
26
+ fn();
27
+ });
28
+ ids.add(id);
29
+ },
30
+ cancel: () => {
31
+ ids.forEach((id) => targetWindow.cancelAnimationFrame(id));
32
+ ids.clear();
33
+ }
34
+ };
35
+ };
20
36
  const defaultKeyExtractor = (index) => index;
21
37
  const defaultRangeExtractor = (range) => {
22
38
  const overscan = range.overscan * Math.max(range.lanes ?? 1, 1);
@@ -47,6 +63,7 @@ const observeElementRect = (instance, cb) => {
47
63
  return () => {
48
64
  };
49
65
  }
66
+ const frames = createFrameQueue(targetWindow);
50
67
  const observer = new targetWindow.ResizeObserver((entries) => {
51
68
  const run = () => {
52
69
  const entry = entries[0];
@@ -59,11 +76,12 @@ const observeElementRect = (instance, cb) => {
59
76
  }
60
77
  handler(getRect(element));
61
78
  };
62
- instance.options.useAnimationFrameWithResizeObserver ? requestAnimationFrame(run) : run();
79
+ instance.options.useAnimationFrameWithResizeObserver ? frames.schedule(run) : run();
63
80
  });
64
81
  observer.observe(element, { box: "border-box" });
65
82
  return () => {
66
83
  observer.unobserve(element);
84
+ frames.cancel();
67
85
  };
68
86
  };
69
87
  const addEventListenerOptions = {
@@ -209,12 +227,9 @@ class Virtualizer {
209
227
  this.scrollRect = null;
210
228
  this.scrollOffset = null;
211
229
  this.scrollDirection = null;
212
- this.scrollAdjustments = 0;
213
230
  this._iosDeferredAdjustment = 0;
214
- this._iosTouching = false;
215
- this._iosJustTouchEnded = false;
216
- this._iosTouchEndTimerId = null;
217
- this._intendedScrollOffset = null;
231
+ this._iosTouch = null;
232
+ this.pendingWrite = null;
218
233
  this._clampedAdjustment = null;
219
234
  this.elementsCache = /* @__PURE__ */ new Map();
220
235
  this._resizeBatch = null;
@@ -224,14 +239,17 @@ class Virtualizer {
224
239
  };
225
240
  this.observer = /* @__PURE__ */ (() => {
226
241
  let _ro = null;
242
+ let _frames = null;
227
243
  const get = () => {
228
244
  if (_ro) {
229
245
  return _ro;
230
246
  }
231
- if (!this.targetWindow || !this.targetWindow.ResizeObserver) {
247
+ const targetWindow = this.targetWindow;
248
+ if (!targetWindow || !targetWindow.ResizeObserver) {
232
249
  return null;
233
250
  }
234
- return _ro = new this.targetWindow.ResizeObserver((entries) => {
251
+ const frames = _frames = createFrameQueue(targetWindow);
252
+ return _ro = new targetWindow.ResizeObserver((entries) => {
235
253
  const run = () => {
236
254
  const batch = { sync: false };
237
255
  this._resizeBatch = batch;
@@ -245,7 +263,7 @@ class Virtualizer {
245
263
  }
246
264
  }
247
265
  };
248
- this.options.useAnimationFrameWithResizeObserver ? requestAnimationFrame(run) : run();
266
+ this.options.useAnimationFrameWithResizeObserver ? frames.schedule(run) : run();
249
267
  });
250
268
  };
251
269
  const measureEntry = (entry) => {
@@ -270,7 +288,9 @@ class Virtualizer {
270
288
  disconnect: () => {
271
289
  var _a;
272
290
  (_a = get()) == null ? void 0 : _a.disconnect();
291
+ _frames == null ? void 0 : _frames.cancel();
273
292
  _ro = null;
293
+ _frames = null;
274
294
  },
275
295
  observe: (target) => {
276
296
  var _a;
@@ -358,8 +378,7 @@ class Virtualizer {
358
378
  this.pendingMin = 0;
359
379
  this.itemSizeCacheVersion++;
360
380
  }
361
- let anchorResolved = false;
362
- let anchorDelta = 0;
381
+ let anchorDelta = null;
363
382
  if (anchor && this.scrollOffset !== null) {
364
383
  const [anchorKey, anchorOffset] = anchor;
365
384
  const newMeasurements = this.getMeasurements();
@@ -375,18 +394,14 @@ class Virtualizer {
375
394
  if (!followOnAppend && newOffset !== this.scrollOffset) {
376
395
  anchorDelta = newOffset - this.scrollOffset;
377
396
  this.scrollOffset = newOffset;
378
- anchorResolved = true;
379
397
  }
380
398
  }
381
399
  }
382
400
  }
383
- if (anchorResolved || followOnAppend) {
384
- this.pendingScrollAnchor = [
385
- anchorResolved ? anchor[0] : null,
386
- anchorResolved ? anchor[1] : 0,
387
- followOnAppend,
388
- anchorDelta
389
- ];
401
+ if (followOnAppend) {
402
+ this.pendingScrollAnchor = { kind: "follow", behavior: followOnAppend };
403
+ } else if (anchorDelta !== null) {
404
+ this.pendingScrollAnchor = { kind: "anchor", delta: anchorDelta };
390
405
  }
391
406
  };
392
407
  this.notify = (sync) => {
@@ -427,9 +442,9 @@ class Virtualizer {
427
442
  this.isScrolling = false;
428
443
  this.scrollDirection = null;
429
444
  this._iosDeferredAdjustment = 0;
430
- this._iosTouching = false;
431
- this._iosJustTouchEnded = false;
445
+ this._endIosTouch();
432
446
  this._clampedAdjustment = null;
447
+ this.pendingWrite = null;
433
448
  this.scrollElement = null;
434
449
  this.targetWindow = null;
435
450
  };
@@ -465,11 +480,12 @@ class Virtualizer {
465
480
  this.unsubs.push(
466
481
  this.options.observeElementOffset(this, (offset, isScrolling) => {
467
482
  var _a2;
468
- if (isScrolling && this._intendedScrollOffset === null && offset === this.scrollOffset) {
483
+ if (isScrolling && this.pendingWrite === null && offset === this.scrollOffset) {
469
484
  return;
470
485
  }
471
- if (this._intendedScrollOffset !== null && Math.abs(offset - this._intendedScrollOffset) < 1.5) {
472
- offset = this._intendedScrollOffset;
486
+ const pending = this.pendingWrite;
487
+ if (pending !== null && Math.abs(offset - pending.target) < 1.5) {
488
+ offset = pending.target;
473
489
  } else if (((_a2 = this.scrollState) == null ? void 0 : _a2.index) != null && // A non-smooth write lands at once, so any other scroll event is
474
490
  // the user's, even before reconcile saw the target reached (the
475
491
  // target may keep moving, or iOS ignored the write under a
@@ -486,12 +502,10 @@ class Virtualizer {
486
502
  !isAnimating(this.scrollState) && !utils.approxEqual(offset, this.scrollState.lastTargetOffset) && !utils.approxEqual(offset, this.getScrollStateTarget(this.scrollState)) && (this._clampedAdjustment === null || Math.abs(offset - this._clampedAdjustment.maxAtWrite) >= 1.5)) {
487
503
  this.scrollState = null;
488
504
  }
489
- this._intendedScrollOffset = null;
490
- if (this.scrollState) this.scrollState.writtenOffset = null;
505
+ this.pendingWrite = null;
491
506
  if (this._clampedAdjustment !== null && Math.abs(offset - this._clampedAdjustment.maxAtWrite) >= 1.5) {
492
507
  this._clampedAdjustment = null;
493
508
  }
494
- this.scrollAdjustments = 0;
495
509
  const prevOffset = this.getScrollOffset();
496
510
  this.scrollDirection = isScrolling ? prevOffset === offset ? this.scrollDirection : prevOffset < offset ? "forward" : "backward" : null;
497
511
  this.scrollOffset = offset;
@@ -506,24 +520,22 @@ class Virtualizer {
506
520
  if ("addEventListener" in this.scrollElement) {
507
521
  const scrollEl = this.scrollElement;
508
522
  const onTouchStart = () => {
509
- this._iosTouching = true;
510
- this._iosJustTouchEnded = false;
511
- if (this._iosTouchEndTimerId !== null && this.targetWindow != null) {
512
- this.targetWindow.clearTimeout(this._iosTouchEndTimerId);
513
- this._iosTouchEndTimerId = null;
514
- }
523
+ this._endIosTouch();
524
+ this._iosTouch = { phase: "down" };
515
525
  };
516
526
  const onTouchEnd = () => {
517
- this._iosTouching = false;
527
+ this._endIosTouch();
518
528
  if (!isIOSWebKit() || this.targetWindow == null) {
519
529
  return;
520
530
  }
521
- this._iosJustTouchEnded = true;
522
- this._iosTouchEndTimerId = this.targetWindow.setTimeout(() => {
523
- this._iosJustTouchEnded = false;
524
- this._iosTouchEndTimerId = null;
525
- this._flushIosDeferredIfReady();
526
- }, 150);
531
+ const tail = {
532
+ phase: "tail",
533
+ timerId: this.targetWindow.setTimeout(() => {
534
+ if (this._iosTouch === tail) this._iosTouch = null;
535
+ this._flushIosDeferredIfReady();
536
+ }, 150)
537
+ };
538
+ this._iosTouch = tail;
527
539
  };
528
540
  scrollEl.addEventListener(
529
541
  "touchstart",
@@ -544,10 +556,7 @@ class Virtualizer {
544
556
  scrollEl.removeEventListener("touchstart", onTouchStart);
545
557
  scrollEl.removeEventListener("touchend", onTouchEnd);
546
558
  scrollEl.removeEventListener("touchcancel", onTouchEnd);
547
- if (this._iosTouchEndTimerId !== null && this.targetWindow != null) {
548
- this.targetWindow.clearTimeout(this._iosTouchEndTimerId);
549
- this._iosTouchEndTimerId = null;
550
- }
559
+ this._endIosTouch();
551
560
  });
552
561
  }
553
562
  this._scrollToOffset(this.getScrollOffset(), {
@@ -558,14 +567,11 @@ class Virtualizer {
558
567
  const anchor = this.pendingScrollAnchor;
559
568
  this.pendingScrollAnchor = null;
560
569
  if (anchor && this.scrollElement && this.options.enabled) {
561
- const [key, _offset, followOnAppend, anchorDelta] = anchor;
562
- if (key !== null && !followOnAppend) {
563
- if (isIOSWebKit() && (this.isScrolling || this._iosTouching || this._iosJustTouchEnded)) {
564
- if (anchorDelta !== 0) {
565
- this._iosDeferredAdjustment += anchorDelta;
566
- }
570
+ if (anchor.kind === "anchor") {
571
+ if (isIOSWebKit() && this._iosInMotion()) {
572
+ this._iosDeferredAdjustment += anchor.delta;
567
573
  } else if (isSmoothScroll(this.scrollState) && !utils.approxEqual(
568
- this.getScrollOffset() - anchorDelta,
574
+ this.getScrollOffset() - anchor.delta,
569
575
  this.scrollState.lastTargetOffset
570
576
  )) ;
571
577
  else {
@@ -574,9 +580,8 @@ class Virtualizer {
574
580
  behavior: void 0
575
581
  });
576
582
  }
577
- }
578
- if (followOnAppend) {
579
- this.scrollToEnd({ behavior: followOnAppend });
583
+ } else {
584
+ this.scrollToEnd({ behavior: anchor.behavior });
580
585
  }
581
586
  }
582
587
  this._retryClampedAdjustment();
@@ -595,11 +600,17 @@ class Virtualizer {
595
600
  });
596
601
  }
597
602
  };
603
+ this._iosInMotion = () => this.isScrolling || this._iosTouch !== null;
604
+ this._endIosTouch = () => {
605
+ var _a;
606
+ if (((_a = this._iosTouch) == null ? void 0 : _a.phase) === "tail" && this.targetWindow != null) {
607
+ this.targetWindow.clearTimeout(this._iosTouch.timerId);
608
+ }
609
+ this._iosTouch = null;
610
+ };
598
611
  this._flushIosDeferredIfReady = () => {
599
612
  if (this._iosDeferredAdjustment === 0) return;
600
- if (this.isScrolling) return;
601
- if (this._iosTouching) return;
602
- if (this._iosJustTouchEnded) return;
613
+ if (this._iosInMotion()) return;
603
614
  const cur = this.getScrollOffset();
604
615
  const max = this.getMaxScrollOffset();
605
616
  if (cur < 0 || cur > max) return;
@@ -610,7 +621,7 @@ class Virtualizer {
610
621
  const delta = this._iosDeferredAdjustment;
611
622
  this._iosDeferredAdjustment = 0;
612
623
  this._scrollToOffset(cur, {
613
- adjustments: this.scrollAdjustments += delta,
624
+ adjustments: this.scrollAdjustments + delta,
614
625
  behavior: void 0
615
626
  });
616
627
  };
@@ -933,7 +944,7 @@ class Virtualizer {
933
944
  }
934
945
  };
935
946
  this.resizeItem = (index, size) => {
936
- var _a, _b, _c, _d;
947
+ var _a, _b, _c;
937
948
  if (!this.isIndexInRange(index)) return;
938
949
  if ((_a = this._resizeBatch) == null ? void 0 : _a.sync) this.getMeasurements();
939
950
  let cachedSize;
@@ -992,8 +1003,9 @@ class Virtualizer {
992
1003
  }
993
1004
  this.itemSizeCache.set(key, size);
994
1005
  this.itemSizeCacheVersion++;
995
- const awaitingCommandReadBack = ((_c = this.scrollState) == null ? void 0 : _c.writtenOffset) != null && !utils.approxEqual(
996
- this.scrollState.writtenOffset,
1006
+ const writtenOffset = this.getCommandWrittenOffset();
1007
+ const awaitingCommandReadBack = writtenOffset !== null && !utils.approxEqual(
1008
+ writtenOffset,
997
1009
  this.getScrollOffset() + this.scrollAdjustments
998
1010
  );
999
1011
  let adjustedSync = false;
@@ -1008,7 +1020,7 @@ class Virtualizer {
1008
1020
  }
1009
1021
  if (this._resizeBatch) {
1010
1022
  this._resizeBatch.notify = true;
1011
- (_d = this._resizeBatch).sync || (_d.sync = adjustedSync);
1023
+ (_c = this._resizeBatch).sync || (_c.sync = adjustedSync);
1012
1024
  return;
1013
1025
  }
1014
1026
  this.notify(adjustedSync);
@@ -1110,82 +1122,46 @@ class Virtualizer {
1110
1122
  ];
1111
1123
  };
1112
1124
  this.scrollToOffset = (toOffset, { align = "start", behavior = "auto" } = {}) => {
1113
- this._iosDeferredAdjustment = 0;
1114
- const offset = this.getOffsetForAlignment(toOffset, align);
1115
- const now = this.now();
1116
- this.scrollState = {
1117
- index: null,
1118
- align,
1119
- startedAt: now,
1120
- lastTargetOffset: offset,
1121
- stableFrames: 0,
1122
- phase: initialScrollPhase(behavior, false)
1123
- };
1124
- this._scrollToOffset(offset, { adjustments: void 0, behavior });
1125
- this.scheduleScrollReconcile();
1125
+ this._startScroll(
1126
+ { index: null, align },
1127
+ this.getOffsetForAlignment(toOffset, align),
1128
+ behavior,
1129
+ "absolute"
1130
+ );
1126
1131
  };
1127
1132
  this.scrollToIndex = (index, {
1128
1133
  align: initialAlign = "auto",
1129
1134
  behavior = "auto"
1130
1135
  } = {}) => {
1131
- this._iosDeferredAdjustment = 0;
1132
1136
  index = Math.max(0, Math.min(index, this.options.count - 1));
1133
1137
  const offsetInfo = this.getOffsetForIndex(index, initialAlign);
1134
1138
  if (!offsetInfo) {
1139
+ this._iosDeferredAdjustment = 0;
1135
1140
  return;
1136
1141
  }
1137
1142
  const [offset, align] = offsetInfo;
1138
- const now = this.now();
1139
- this.scrollState = {
1140
- index,
1141
- align,
1142
- startedAt: now,
1143
- lastTargetOffset: offset,
1144
- stableFrames: 0,
1145
- phase: initialScrollPhase(
1146
- behavior,
1147
- utils.approxEqual(offset, this.getScrollOffset())
1148
- )
1149
- };
1150
- this._scrollToOffset(offset, { adjustments: void 0, behavior });
1151
- this.scheduleScrollReconcile();
1143
+ this._startScroll({ index, align }, offset, behavior, "absolute");
1152
1144
  };
1153
1145
  this.scrollBy = (delta, { behavior = "auto" } = {}) => {
1154
- var _a;
1155
1146
  const offset = this.getOffsetForAlignment(
1156
- (((_a = this.scrollState) == null ? void 0 : _a.writtenOffset) ?? this.getScrollOffset()) + delta,
1147
+ (this.getCommandWrittenOffset() ?? this.getScrollOffset()) + delta,
1157
1148
  "start"
1158
1149
  );
1159
- const now = this.now();
1160
- this.scrollState = {
1161
- index: null,
1162
- align: "start",
1163
- startedAt: now,
1164
- lastTargetOffset: offset,
1165
- stableFrames: 0,
1166
- phase: initialScrollPhase(behavior, false)
1167
- };
1168
- this._scrollToOffset(offset, { adjustments: void 0, behavior });
1169
- this.scheduleScrollReconcile();
1150
+ this._startScroll(
1151
+ { index: null, align: "start" },
1152
+ offset,
1153
+ behavior,
1154
+ "relative"
1155
+ );
1170
1156
  };
1171
1157
  this.scrollToEnd = ({ behavior = "auto" } = {}) => {
1172
1158
  if (this.options.count > 0) {
1173
- this._iosDeferredAdjustment = 0;
1174
- const offset = Math.max(this.getMaxScrollOffset(), 0);
1175
- this.scrollState = {
1176
- index: this.options.count - 1,
1177
- align: "end",
1178
- toEnd: true,
1179
- startedAt: this.now(),
1180
- lastTargetOffset: offset,
1181
- stableFrames: 0,
1182
- phase: initialScrollPhase(
1183
- behavior,
1184
- utils.approxEqual(offset, this.getScrollOffset())
1185
- )
1186
- };
1187
- this._scrollToOffset(offset, { adjustments: void 0, behavior });
1188
- this.scheduleScrollReconcile();
1159
+ this._startScroll(
1160
+ { index: this.options.count - 1, align: "end", toEnd: true },
1161
+ Math.max(this.getMaxScrollOffset(), 0),
1162
+ behavior,
1163
+ "absolute"
1164
+ );
1189
1165
  return;
1190
1166
  }
1191
1167
  this.scrollToOffset(Math.max(this.getTotalSize() - this.getSize(), 0), {
@@ -1252,12 +1228,14 @@ class Virtualizer {
1252
1228
  adjustments,
1253
1229
  behavior
1254
1230
  }) => {
1255
- this._intendedScrollOffset = offset + (adjustments ?? 0);
1256
- if (this.scrollState) {
1257
- this.scrollState.writtenOffset = behavior === "smooth" ? null : this._intendedScrollOffset;
1258
- }
1231
+ this.pendingWrite = {
1232
+ target: offset + (adjustments ?? 0),
1233
+ smooth: behavior === "smooth",
1234
+ adjustments: adjustments ?? this.scrollAdjustments
1235
+ };
1259
1236
  this.options.scrollToFn(offset, { behavior, adjustments }, this);
1260
1237
  };
1238
+ this.getCommandWrittenOffset = () => this.scrollState !== null && this.pendingWrite !== null && !this.pendingWrite.smooth ? this.pendingWrite.target : null;
1261
1239
  this.measure = () => {
1262
1240
  this.pendingMin = null;
1263
1241
  this.itemSizeCache.clear();
@@ -1267,6 +1245,13 @@ class Virtualizer {
1267
1245
  };
1268
1246
  this.setOptions(opts);
1269
1247
  }
1248
+ // Size compensation written to the element but not yet reflected in
1249
+ // `scrollOffset`: the element is expected at `scrollOffset +
1250
+ // scrollAdjustments` until the next scroll event reports it.
1251
+ get scrollAdjustments() {
1252
+ var _a;
1253
+ return ((_a = this.pendingWrite) == null ? void 0 : _a.adjustments) ?? 0;
1254
+ }
1270
1255
  // Returns `true` when it performed a synchronous `scrollTop` write this
1271
1256
  // tick, `false` when the delta was zero or the write was deferred (iOS).
1272
1257
  // `resizeItem` uses that to decide whether the follow-up `notify` must be
@@ -1277,7 +1262,7 @@ class Virtualizer {
1277
1262
  if (process.env.NODE_ENV !== "production" && this.options.debug) {
1278
1263
  console.info("correction", delta);
1279
1264
  }
1280
- if (isIOSWebKit() && (this.isScrolling || this._iosTouching || this._iosJustTouchEnded)) {
1265
+ if (isIOSWebKit() && this._iosInMotion()) {
1281
1266
  if (((_a = this.scrollState) == null ? void 0 : _a.index) == null) {
1282
1267
  this._iosDeferredAdjustment += delta;
1283
1268
  }
@@ -1288,13 +1273,13 @@ class Virtualizer {
1288
1273
  const maxAtWrite = el !== null && ("scrollHeight" in el || "document" in el) ? this.getMaxScrollOffset() : null;
1289
1274
  this._clampedAdjustment = maxAtWrite !== null && target > maxAtWrite + 0.5 ? { target, maxAtWrite } : null;
1290
1275
  this._scrollToOffset(this.getScrollOffset(), {
1291
- adjustments: this.scrollAdjustments += delta,
1276
+ adjustments: this.scrollAdjustments + delta,
1292
1277
  behavior
1293
1278
  });
1294
1279
  if (this.scrollOffset !== null) {
1295
1280
  this.scrollOffset += this.scrollAdjustments;
1296
1281
  if (this.scrollOffset < 0) this.scrollOffset = 0;
1297
- this.scrollAdjustments = 0;
1282
+ if (this.pendingWrite) this.pendingWrite.adjustments = 0;
1298
1283
  }
1299
1284
  return true;
1300
1285
  }
@@ -1366,6 +1351,36 @@ class Virtualizer {
1366
1351
  }
1367
1352
  this.scheduleScrollReconcile();
1368
1353
  }
1354
+ // Every scroll command starts here: it becomes the in-flight command,
1355
+ // writes its first target and leaves reconcile to retarget and retire it.
1356
+ //
1357
+ // An absolute command derives its target from current measurements, so
1358
+ // any iOS-deferred compensation still pending is stale by definition —
1359
+ // the command already accounts for the measurements the delta was
1360
+ // compensating for. Drop it so _flushIosDeferredIfReady doesn't replay it
1361
+ // onto the just-established position. A relative command (scrollBy)
1362
+ // builds on the current offset, so it keeps the deferral.
1363
+ //
1364
+ // An index or end target that is already at its offset starts a smooth
1365
+ // scroll as landed; an offset target never does, so its first scroll
1366
+ // event is read as the user's (see the scroll handler in `_willUpdate`).
1367
+ _startScroll(target, offset, behavior, kind) {
1368
+ if (kind === "absolute") {
1369
+ this._iosDeferredAdjustment = 0;
1370
+ }
1371
+ this.scrollState = {
1372
+ ...target,
1373
+ startedAt: this.now(),
1374
+ lastTargetOffset: offset,
1375
+ stableFrames: 0,
1376
+ phase: initialScrollPhase(
1377
+ behavior,
1378
+ target.index != null && utils.approxEqual(offset, this.getScrollOffset())
1379
+ )
1380
+ };
1381
+ this._scrollToOffset(offset, { adjustments: void 0, behavior });
1382
+ this.scheduleScrollReconcile();
1383
+ }
1369
1384
  }
1370
1385
  const findNearestBinarySearch = (low, high, getCurrentValue, value) => {
1371
1386
  while (low <= high) {