@tanstack/virtual-core 3.0.4 → 3.1.0

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.
Files changed (36) hide show
  1. package/dist/cjs/index.cjs +645 -0
  2. package/dist/cjs/index.cjs.map +1 -0
  3. package/dist/cjs/index.d.cts +126 -0
  4. package/dist/cjs/utils.cjs +59 -0
  5. package/dist/cjs/utils.cjs.map +1 -0
  6. package/{build/lib → dist/esm}/index.d.ts +1 -1
  7. package/dist/esm/index.js +645 -0
  8. package/dist/esm/index.js.map +1 -0
  9. package/dist/esm/utils.d.ts +10 -0
  10. package/dist/esm/utils.js +59 -0
  11. package/dist/esm/utils.js.map +1 -0
  12. package/package.json +33 -23
  13. package/src/index.ts +18 -21
  14. package/build/lib/_virtual/_rollupPluginBabelHelpers.esm.js +0 -27
  15. package/build/lib/_virtual/_rollupPluginBabelHelpers.esm.js.map +0 -1
  16. package/build/lib/_virtual/_rollupPluginBabelHelpers.js +0 -31
  17. package/build/lib/_virtual/_rollupPluginBabelHelpers.js.map +0 -1
  18. package/build/lib/_virtual/_rollupPluginBabelHelpers.mjs +0 -27
  19. package/build/lib/_virtual/_rollupPluginBabelHelpers.mjs.map +0 -1
  20. package/build/lib/index.esm.js +0 -653
  21. package/build/lib/index.esm.js.map +0 -1
  22. package/build/lib/index.js +0 -668
  23. package/build/lib/index.js.map +0 -1
  24. package/build/lib/index.mjs +0 -653
  25. package/build/lib/index.mjs.map +0 -1
  26. package/build/lib/utils.esm.js +0 -58
  27. package/build/lib/utils.esm.js.map +0 -1
  28. package/build/lib/utils.js +0 -64
  29. package/build/lib/utils.js.map +0 -1
  30. package/build/lib/utils.mjs +0 -58
  31. package/build/lib/utils.mjs.map +0 -1
  32. package/build/umd/index.development.js +0 -732
  33. package/build/umd/index.development.js.map +0 -1
  34. package/build/umd/index.production.js +0 -12
  35. package/build/umd/index.production.js.map +0 -1
  36. /package/{build/lib/utils.d.ts → dist/cjs/utils.d.cts} +0 -0
@@ -0,0 +1,645 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const utils = require("./utils.cjs");
4
+ const defaultKeyExtractor = (index) => index;
5
+ const defaultRangeExtractor = (range) => {
6
+ const start = Math.max(range.startIndex - range.overscan, 0);
7
+ const end = Math.min(range.endIndex + range.overscan, range.count - 1);
8
+ const arr = [];
9
+ for (let i = start; i <= end; i++) {
10
+ arr.push(i);
11
+ }
12
+ return arr;
13
+ };
14
+ const observeElementRect = (instance, cb) => {
15
+ const element = instance.scrollElement;
16
+ if (!element) {
17
+ return;
18
+ }
19
+ const handler = (rect) => {
20
+ const { width, height } = rect;
21
+ cb({ width: Math.round(width), height: Math.round(height) });
22
+ };
23
+ handler(element.getBoundingClientRect());
24
+ const observer = new ResizeObserver((entries) => {
25
+ const entry = entries[0];
26
+ if (entry == null ? void 0 : entry.borderBoxSize) {
27
+ const box = entry.borderBoxSize[0];
28
+ if (box) {
29
+ handler({ width: box.inlineSize, height: box.blockSize });
30
+ return;
31
+ }
32
+ }
33
+ handler(element.getBoundingClientRect());
34
+ });
35
+ observer.observe(element, { box: "border-box" });
36
+ return () => {
37
+ observer.unobserve(element);
38
+ };
39
+ };
40
+ const observeWindowRect = (instance, cb) => {
41
+ const element = instance.scrollElement;
42
+ if (!element) {
43
+ return;
44
+ }
45
+ const handler = () => {
46
+ cb({ width: element.innerWidth, height: element.innerHeight });
47
+ };
48
+ handler();
49
+ element.addEventListener("resize", handler, {
50
+ passive: true
51
+ });
52
+ return () => {
53
+ element.removeEventListener("resize", handler);
54
+ };
55
+ };
56
+ const observeElementOffset = (instance, cb) => {
57
+ const element = instance.scrollElement;
58
+ if (!element) {
59
+ return;
60
+ }
61
+ const handler = () => {
62
+ cb(element[instance.options.horizontal ? "scrollLeft" : "scrollTop"]);
63
+ };
64
+ handler();
65
+ element.addEventListener("scroll", handler, {
66
+ passive: true
67
+ });
68
+ return () => {
69
+ element.removeEventListener("scroll", handler);
70
+ };
71
+ };
72
+ const observeWindowOffset = (instance, cb) => {
73
+ const element = instance.scrollElement;
74
+ if (!element) {
75
+ return;
76
+ }
77
+ const handler = () => {
78
+ cb(element[instance.options.horizontal ? "scrollX" : "scrollY"]);
79
+ };
80
+ handler();
81
+ element.addEventListener("scroll", handler, {
82
+ passive: true
83
+ });
84
+ return () => {
85
+ element.removeEventListener("scroll", handler);
86
+ };
87
+ };
88
+ const measureElement = (element, entry, instance) => {
89
+ if (entry == null ? void 0 : entry.borderBoxSize) {
90
+ const box = entry.borderBoxSize[0];
91
+ if (box) {
92
+ const size = Math.round(
93
+ box[instance.options.horizontal ? "inlineSize" : "blockSize"]
94
+ );
95
+ return size;
96
+ }
97
+ }
98
+ return Math.round(
99
+ element.getBoundingClientRect()[instance.options.horizontal ? "width" : "height"]
100
+ );
101
+ };
102
+ const windowScroll = (offset, {
103
+ adjustments = 0,
104
+ behavior
105
+ }, instance) => {
106
+ var _a, _b;
107
+ const toOffset = offset + adjustments;
108
+ (_b = (_a = instance.scrollElement) == null ? void 0 : _a.scrollTo) == null ? void 0 : _b.call(_a, {
109
+ [instance.options.horizontal ? "left" : "top"]: toOffset,
110
+ behavior
111
+ });
112
+ };
113
+ const elementScroll = (offset, {
114
+ adjustments = 0,
115
+ behavior
116
+ }, instance) => {
117
+ var _a, _b;
118
+ const toOffset = offset + adjustments;
119
+ (_b = (_a = instance.scrollElement) == null ? void 0 : _a.scrollTo) == null ? void 0 : _b.call(_a, {
120
+ [instance.options.horizontal ? "left" : "top"]: toOffset,
121
+ behavior
122
+ });
123
+ };
124
+ class Virtualizer {
125
+ constructor(opts) {
126
+ this.unsubs = [];
127
+ this.scrollElement = null;
128
+ this.isScrolling = false;
129
+ this.isScrollingTimeoutId = null;
130
+ this.scrollToIndexTimeoutId = null;
131
+ this.measurementsCache = [];
132
+ this.itemSizeCache = /* @__PURE__ */ new Map();
133
+ this.pendingMeasuredCacheIndexes = [];
134
+ this.scrollDirection = null;
135
+ this.scrollAdjustments = 0;
136
+ this.measureElementCache = /* @__PURE__ */ new Map();
137
+ this.observer = (() => {
138
+ let _ro = null;
139
+ const get = () => {
140
+ if (_ro) {
141
+ return _ro;
142
+ } else if (typeof ResizeObserver !== "undefined") {
143
+ return _ro = new ResizeObserver((entries) => {
144
+ entries.forEach((entry) => {
145
+ this._measureElement(entry.target, entry);
146
+ });
147
+ });
148
+ } else {
149
+ return null;
150
+ }
151
+ };
152
+ return {
153
+ disconnect: () => {
154
+ var _a;
155
+ return (_a = get()) == null ? void 0 : _a.disconnect();
156
+ },
157
+ observe: (target) => {
158
+ var _a;
159
+ return (_a = get()) == null ? void 0 : _a.observe(target, { box: "border-box" });
160
+ },
161
+ unobserve: (target) => {
162
+ var _a;
163
+ return (_a = get()) == null ? void 0 : _a.unobserve(target);
164
+ }
165
+ };
166
+ })();
167
+ this.range = null;
168
+ this.setOptions = (opts2) => {
169
+ Object.entries(opts2).forEach(([key, value]) => {
170
+ if (typeof value === "undefined")
171
+ delete opts2[key];
172
+ });
173
+ this.options = {
174
+ debug: false,
175
+ initialOffset: 0,
176
+ overscan: 1,
177
+ paddingStart: 0,
178
+ paddingEnd: 0,
179
+ scrollPaddingStart: 0,
180
+ scrollPaddingEnd: 0,
181
+ horizontal: false,
182
+ getItemKey: defaultKeyExtractor,
183
+ rangeExtractor: defaultRangeExtractor,
184
+ onChange: () => {
185
+ },
186
+ measureElement,
187
+ initialRect: { width: 0, height: 0 },
188
+ scrollMargin: 0,
189
+ scrollingDelay: 150,
190
+ indexAttribute: "data-index",
191
+ initialMeasurementsCache: [],
192
+ lanes: 1,
193
+ ...opts2
194
+ };
195
+ };
196
+ this.notify = (sync) => {
197
+ var _a, _b;
198
+ (_b = (_a = this.options).onChange) == null ? void 0 : _b.call(_a, this, sync);
199
+ };
200
+ this.maybeNotify = utils.memo(
201
+ () => {
202
+ this.calculateRange();
203
+ return [
204
+ this.isScrolling,
205
+ this.range ? this.range.startIndex : null,
206
+ this.range ? this.range.endIndex : null
207
+ ];
208
+ },
209
+ (isScrolling) => {
210
+ this.notify(isScrolling);
211
+ },
212
+ {
213
+ key: process.env.NODE_ENV !== "production" && "maybeNotify",
214
+ debug: () => this.options.debug,
215
+ initialDeps: [
216
+ this.isScrolling,
217
+ this.range ? this.range.startIndex : null,
218
+ this.range ? this.range.endIndex : null
219
+ ]
220
+ }
221
+ );
222
+ this.cleanup = () => {
223
+ this.unsubs.filter(Boolean).forEach((d) => d());
224
+ this.unsubs = [];
225
+ this.scrollElement = null;
226
+ };
227
+ this._didMount = () => {
228
+ this.measureElementCache.forEach(this.observer.observe);
229
+ return () => {
230
+ this.observer.disconnect();
231
+ this.cleanup();
232
+ };
233
+ };
234
+ this._willUpdate = () => {
235
+ const scrollElement = this.options.getScrollElement();
236
+ if (this.scrollElement !== scrollElement) {
237
+ this.cleanup();
238
+ this.scrollElement = scrollElement;
239
+ this._scrollToOffset(this.scrollOffset, {
240
+ adjustments: void 0,
241
+ behavior: void 0
242
+ });
243
+ this.unsubs.push(
244
+ this.options.observeElementRect(this, (rect) => {
245
+ this.scrollRect = rect;
246
+ this.maybeNotify();
247
+ })
248
+ );
249
+ this.unsubs.push(
250
+ this.options.observeElementOffset(this, (offset) => {
251
+ this.scrollAdjustments = 0;
252
+ if (this.scrollOffset === offset) {
253
+ return;
254
+ }
255
+ if (this.isScrollingTimeoutId !== null) {
256
+ clearTimeout(this.isScrollingTimeoutId);
257
+ this.isScrollingTimeoutId = null;
258
+ }
259
+ this.isScrolling = true;
260
+ this.scrollDirection = this.scrollOffset < offset ? "forward" : "backward";
261
+ this.scrollOffset = offset;
262
+ this.maybeNotify();
263
+ this.isScrollingTimeoutId = setTimeout(() => {
264
+ this.isScrollingTimeoutId = null;
265
+ this.isScrolling = false;
266
+ this.scrollDirection = null;
267
+ this.maybeNotify();
268
+ }, this.options.scrollingDelay);
269
+ })
270
+ );
271
+ }
272
+ };
273
+ this.getSize = () => {
274
+ return this.scrollRect[this.options.horizontal ? "width" : "height"];
275
+ };
276
+ this.memoOptions = utils.memo(
277
+ () => [
278
+ this.options.count,
279
+ this.options.paddingStart,
280
+ this.options.scrollMargin,
281
+ this.options.getItemKey
282
+ ],
283
+ (count, paddingStart, scrollMargin, getItemKey) => {
284
+ this.pendingMeasuredCacheIndexes = [];
285
+ return {
286
+ count,
287
+ paddingStart,
288
+ scrollMargin,
289
+ getItemKey
290
+ };
291
+ },
292
+ {
293
+ key: false
294
+ }
295
+ );
296
+ this.getFurthestMeasurement = (measurements, index) => {
297
+ const furthestMeasurementsFound = /* @__PURE__ */ new Map();
298
+ const furthestMeasurements = /* @__PURE__ */ new Map();
299
+ for (let m = index - 1; m >= 0; m--) {
300
+ const measurement = measurements[m];
301
+ if (furthestMeasurementsFound.has(measurement.lane)) {
302
+ continue;
303
+ }
304
+ const previousFurthestMeasurement = furthestMeasurements.get(
305
+ measurement.lane
306
+ );
307
+ if (previousFurthestMeasurement == null || measurement.end > previousFurthestMeasurement.end) {
308
+ furthestMeasurements.set(measurement.lane, measurement);
309
+ } else if (measurement.end < previousFurthestMeasurement.end) {
310
+ furthestMeasurementsFound.set(measurement.lane, true);
311
+ }
312
+ if (furthestMeasurementsFound.size === this.options.lanes) {
313
+ break;
314
+ }
315
+ }
316
+ return furthestMeasurements.size === this.options.lanes ? Array.from(furthestMeasurements.values()).sort((a, b) => {
317
+ if (a.end === b.end) {
318
+ return a.index - b.index;
319
+ }
320
+ return a.end - b.end;
321
+ })[0] : void 0;
322
+ };
323
+ this.getMeasurements = utils.memo(
324
+ () => [this.memoOptions(), this.itemSizeCache],
325
+ ({ count, paddingStart, scrollMargin, getItemKey }, itemSizeCache) => {
326
+ const min = this.pendingMeasuredCacheIndexes.length > 0 ? Math.min(...this.pendingMeasuredCacheIndexes) : 0;
327
+ this.pendingMeasuredCacheIndexes = [];
328
+ const measurements = this.measurementsCache.slice(0, min);
329
+ for (let i = min; i < count; i++) {
330
+ const key = getItemKey(i);
331
+ const furthestMeasurement = this.options.lanes === 1 ? measurements[i - 1] : this.getFurthestMeasurement(measurements, i);
332
+ const start = furthestMeasurement ? furthestMeasurement.end : paddingStart + scrollMargin;
333
+ const measuredSize = itemSizeCache.get(key);
334
+ const size = typeof measuredSize === "number" ? measuredSize : this.options.estimateSize(i);
335
+ const end = start + size;
336
+ const lane = furthestMeasurement ? furthestMeasurement.lane : i % this.options.lanes;
337
+ measurements[i] = {
338
+ index: i,
339
+ start,
340
+ size,
341
+ end,
342
+ key,
343
+ lane
344
+ };
345
+ }
346
+ this.measurementsCache = measurements;
347
+ return measurements;
348
+ },
349
+ {
350
+ key: process.env.NODE_ENV !== "production" && "getMeasurements",
351
+ debug: () => this.options.debug
352
+ }
353
+ );
354
+ this.calculateRange = utils.memo(
355
+ () => [this.getMeasurements(), this.getSize(), this.scrollOffset],
356
+ (measurements, outerSize, scrollOffset) => {
357
+ return this.range = measurements.length > 0 && outerSize > 0 ? calculateRange({
358
+ measurements,
359
+ outerSize,
360
+ scrollOffset
361
+ }) : null;
362
+ },
363
+ {
364
+ key: process.env.NODE_ENV !== "production" && "calculateRange",
365
+ debug: () => this.options.debug
366
+ }
367
+ );
368
+ this.getIndexes = utils.memo(
369
+ () => [
370
+ this.options.rangeExtractor,
371
+ this.calculateRange(),
372
+ this.options.overscan,
373
+ this.options.count
374
+ ],
375
+ (rangeExtractor, range, overscan, count) => {
376
+ return range === null ? [] : rangeExtractor({
377
+ ...range,
378
+ overscan,
379
+ count
380
+ });
381
+ },
382
+ {
383
+ key: process.env.NODE_ENV !== "production" && "getIndexes",
384
+ debug: () => this.options.debug
385
+ }
386
+ );
387
+ this.indexFromElement = (node) => {
388
+ const attributeName = this.options.indexAttribute;
389
+ const indexStr = node.getAttribute(attributeName);
390
+ if (!indexStr) {
391
+ console.warn(
392
+ `Missing attribute name '${attributeName}={index}' on measured element.`
393
+ );
394
+ return -1;
395
+ }
396
+ return parseInt(indexStr, 10);
397
+ };
398
+ this._measureElement = (node, entry) => {
399
+ const item = this.measurementsCache[this.indexFromElement(node)];
400
+ if (!item || !node.isConnected) {
401
+ this.measureElementCache.forEach((cached, key) => {
402
+ if (cached === node) {
403
+ this.observer.unobserve(node);
404
+ this.measureElementCache.delete(key);
405
+ }
406
+ });
407
+ return;
408
+ }
409
+ const prevNode = this.measureElementCache.get(item.key);
410
+ if (prevNode !== node) {
411
+ if (prevNode) {
412
+ this.observer.unobserve(prevNode);
413
+ }
414
+ this.observer.observe(node);
415
+ this.measureElementCache.set(item.key, node);
416
+ }
417
+ const measuredItemSize = this.options.measureElement(node, entry, this);
418
+ this.resizeItem(item, measuredItemSize);
419
+ };
420
+ this.resizeItem = (item, size) => {
421
+ const itemSize = this.itemSizeCache.get(item.key) ?? item.size;
422
+ const delta = size - itemSize;
423
+ if (delta !== 0) {
424
+ if (item.start < this.scrollOffset + this.scrollAdjustments) {
425
+ if (process.env.NODE_ENV !== "production" && this.options.debug) {
426
+ console.info("correction", delta);
427
+ }
428
+ this._scrollToOffset(this.scrollOffset, {
429
+ adjustments: this.scrollAdjustments += delta,
430
+ behavior: void 0
431
+ });
432
+ }
433
+ this.pendingMeasuredCacheIndexes.push(item.index);
434
+ this.itemSizeCache = new Map(this.itemSizeCache.set(item.key, size));
435
+ this.notify(false);
436
+ }
437
+ };
438
+ this.measureElement = (node) => {
439
+ if (!node) {
440
+ return;
441
+ }
442
+ this._measureElement(node, void 0);
443
+ };
444
+ this.getVirtualItems = utils.memo(
445
+ () => [this.getIndexes(), this.getMeasurements()],
446
+ (indexes, measurements) => {
447
+ const virtualItems = [];
448
+ for (let k = 0, len = indexes.length; k < len; k++) {
449
+ const i = indexes[k];
450
+ const measurement = measurements[i];
451
+ virtualItems.push(measurement);
452
+ }
453
+ return virtualItems;
454
+ },
455
+ {
456
+ key: process.env.NODE_ENV !== "production" && "getIndexes",
457
+ debug: () => this.options.debug
458
+ }
459
+ );
460
+ this.getVirtualItemForOffset = (offset) => {
461
+ const measurements = this.getMeasurements();
462
+ return utils.notUndefined(
463
+ measurements[findNearestBinarySearch(
464
+ 0,
465
+ measurements.length - 1,
466
+ (index) => utils.notUndefined(measurements[index]).start,
467
+ offset
468
+ )]
469
+ );
470
+ };
471
+ this.getOffsetForAlignment = (toOffset, align) => {
472
+ const size = this.getSize();
473
+ if (align === "auto") {
474
+ if (toOffset <= this.scrollOffset) {
475
+ align = "start";
476
+ } else if (toOffset >= this.scrollOffset + size) {
477
+ align = "end";
478
+ } else {
479
+ align = "start";
480
+ }
481
+ }
482
+ if (align === "start") {
483
+ toOffset = toOffset;
484
+ } else if (align === "end") {
485
+ toOffset = toOffset - size;
486
+ } else if (align === "center") {
487
+ toOffset = toOffset - size / 2;
488
+ }
489
+ const scrollSizeProp = this.options.horizontal ? "scrollWidth" : "scrollHeight";
490
+ const scrollSize = this.scrollElement ? "document" in this.scrollElement ? this.scrollElement.document.documentElement[scrollSizeProp] : this.scrollElement[scrollSizeProp] : 0;
491
+ const maxOffset = scrollSize - this.getSize();
492
+ return Math.max(Math.min(maxOffset, toOffset), 0);
493
+ };
494
+ this.getOffsetForIndex = (index, align = "auto") => {
495
+ index = Math.max(0, Math.min(index, this.options.count - 1));
496
+ const measurement = utils.notUndefined(this.getMeasurements()[index]);
497
+ if (align === "auto") {
498
+ if (measurement.end >= this.scrollOffset + this.getSize() - this.options.scrollPaddingEnd) {
499
+ align = "end";
500
+ } else if (measurement.start <= this.scrollOffset + this.options.scrollPaddingStart) {
501
+ align = "start";
502
+ } else {
503
+ return [this.scrollOffset, align];
504
+ }
505
+ }
506
+ const toOffset = align === "end" ? measurement.end + this.options.scrollPaddingEnd : measurement.start - this.options.scrollPaddingStart;
507
+ return [this.getOffsetForAlignment(toOffset, align), align];
508
+ };
509
+ this.isDynamicMode = () => this.measureElementCache.size > 0;
510
+ this.cancelScrollToIndex = () => {
511
+ if (this.scrollToIndexTimeoutId !== null) {
512
+ clearTimeout(this.scrollToIndexTimeoutId);
513
+ this.scrollToIndexTimeoutId = null;
514
+ }
515
+ };
516
+ this.scrollToOffset = (toOffset, { align = "start", behavior } = {}) => {
517
+ this.cancelScrollToIndex();
518
+ if (behavior === "smooth" && this.isDynamicMode()) {
519
+ console.warn(
520
+ "The `smooth` scroll behavior is not fully supported with dynamic size."
521
+ );
522
+ }
523
+ this._scrollToOffset(this.getOffsetForAlignment(toOffset, align), {
524
+ adjustments: void 0,
525
+ behavior
526
+ });
527
+ };
528
+ this.scrollToIndex = (index, { align: initialAlign = "auto", behavior } = {}) => {
529
+ index = Math.max(0, Math.min(index, this.options.count - 1));
530
+ this.cancelScrollToIndex();
531
+ if (behavior === "smooth" && this.isDynamicMode()) {
532
+ console.warn(
533
+ "The `smooth` scroll behavior is not fully supported with dynamic size."
534
+ );
535
+ }
536
+ const [toOffset, align] = this.getOffsetForIndex(index, initialAlign);
537
+ this._scrollToOffset(toOffset, { adjustments: void 0, behavior });
538
+ if (behavior !== "smooth" && this.isDynamicMode()) {
539
+ this.scrollToIndexTimeoutId = setTimeout(() => {
540
+ this.scrollToIndexTimeoutId = null;
541
+ const elementInDOM = this.measureElementCache.has(
542
+ this.options.getItemKey(index)
543
+ );
544
+ if (elementInDOM) {
545
+ const [toOffset2] = this.getOffsetForIndex(index, align);
546
+ if (!utils.approxEqual(toOffset2, this.scrollOffset)) {
547
+ this.scrollToIndex(index, { align, behavior });
548
+ }
549
+ } else {
550
+ this.scrollToIndex(index, { align, behavior });
551
+ }
552
+ });
553
+ }
554
+ };
555
+ this.scrollBy = (delta, { behavior } = {}) => {
556
+ this.cancelScrollToIndex();
557
+ if (behavior === "smooth" && this.isDynamicMode()) {
558
+ console.warn(
559
+ "The `smooth` scroll behavior is not fully supported with dynamic size."
560
+ );
561
+ }
562
+ this._scrollToOffset(this.scrollOffset + delta, {
563
+ adjustments: void 0,
564
+ behavior
565
+ });
566
+ };
567
+ this.getTotalSize = () => {
568
+ var _a;
569
+ const measurements = this.getMeasurements();
570
+ let end;
571
+ if (measurements.length === 0) {
572
+ end = this.options.paddingStart;
573
+ } else {
574
+ end = this.options.lanes === 1 ? ((_a = measurements[measurements.length - 1]) == null ? void 0 : _a.end) ?? 0 : Math.max(
575
+ ...measurements.slice(-this.options.lanes).map((m) => m.end)
576
+ );
577
+ }
578
+ return end - this.options.scrollMargin + this.options.paddingEnd;
579
+ };
580
+ this._scrollToOffset = (offset, {
581
+ adjustments,
582
+ behavior
583
+ }) => {
584
+ this.options.scrollToFn(offset, { behavior, adjustments }, this);
585
+ };
586
+ this.measure = () => {
587
+ this.itemSizeCache = /* @__PURE__ */ new Map();
588
+ this.notify(false);
589
+ };
590
+ this.setOptions(opts);
591
+ this.scrollRect = this.options.initialRect;
592
+ this.scrollOffset = this.options.initialOffset;
593
+ this.measurementsCache = this.options.initialMeasurementsCache;
594
+ this.measurementsCache.forEach((item) => {
595
+ this.itemSizeCache.set(item.key, item.size);
596
+ });
597
+ this.maybeNotify();
598
+ }
599
+ }
600
+ const findNearestBinarySearch = (low, high, getCurrentValue, value) => {
601
+ while (low <= high) {
602
+ const middle = (low + high) / 2 | 0;
603
+ const currentValue = getCurrentValue(middle);
604
+ if (currentValue < value) {
605
+ low = middle + 1;
606
+ } else if (currentValue > value) {
607
+ high = middle - 1;
608
+ } else {
609
+ return middle;
610
+ }
611
+ }
612
+ if (low > 0) {
613
+ return low - 1;
614
+ } else {
615
+ return 0;
616
+ }
617
+ };
618
+ function calculateRange({
619
+ measurements,
620
+ outerSize,
621
+ scrollOffset
622
+ }) {
623
+ const count = measurements.length - 1;
624
+ const getOffset = (index) => measurements[index].start;
625
+ const startIndex = findNearestBinarySearch(0, count, getOffset, scrollOffset);
626
+ let endIndex = startIndex;
627
+ while (endIndex < count && measurements[endIndex].end < scrollOffset + outerSize) {
628
+ endIndex++;
629
+ }
630
+ return { startIndex, endIndex };
631
+ }
632
+ exports.approxEqual = utils.approxEqual;
633
+ exports.memo = utils.memo;
634
+ exports.notUndefined = utils.notUndefined;
635
+ exports.Virtualizer = Virtualizer;
636
+ exports.defaultKeyExtractor = defaultKeyExtractor;
637
+ exports.defaultRangeExtractor = defaultRangeExtractor;
638
+ exports.elementScroll = elementScroll;
639
+ exports.measureElement = measureElement;
640
+ exports.observeElementOffset = observeElementOffset;
641
+ exports.observeElementRect = observeElementRect;
642
+ exports.observeWindowOffset = observeWindowOffset;
643
+ exports.observeWindowRect = observeWindowRect;
644
+ exports.windowScroll = windowScroll;
645
+ //# sourceMappingURL=index.cjs.map