@tanstack/svelte-virtual 3.0.0-beta.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.
@@ -0,0 +1,66 @@
1
+ /**
2
+ * svelte-virtual
3
+ *
4
+ * Copyright (c) TanStack
5
+ *
6
+ * This source code is licensed under the MIT license found in the
7
+ * LICENSE.md file in the root directory of this source tree.
8
+ *
9
+ * @license MIT
10
+ */
11
+ 'use strict';
12
+
13
+ Object.defineProperty(exports, '__esModule', { value: true });
14
+
15
+ var index = require('../../virtual-core/build/esm/index.js');
16
+ var store = require('svelte/store');
17
+
18
+ function createVirtualizerBase(options) {
19
+ const virtualizer = new index.Virtualizer(options);
20
+ return store.readable(virtualizer, set => {
21
+ const cleanup = virtualizer._didMount();
22
+
23
+ virtualizer._willUpdate();
24
+
25
+ virtualizer.setOptions({ ...options,
26
+ onChange: instance => {
27
+ set(instance);
28
+ options.onChange == null ? void 0 : options.onChange(instance);
29
+ }
30
+ });
31
+ return () => cleanup();
32
+ });
33
+ }
34
+
35
+ function createVirtualizer(options) {
36
+ return createVirtualizerBase({
37
+ observeElementRect: index.observeElementRect,
38
+ observeElementOffset: index.observeElementOffset,
39
+ scrollToFn: index.elementScroll,
40
+ ...options
41
+ });
42
+ }
43
+ function createWindowVirtualizer(options) {
44
+ return createVirtualizerBase({
45
+ getScrollElement: () => typeof window !== 'undefined' ? window : null,
46
+ observeElementRect: index.observeWindowRect,
47
+ observeElementOffset: index.observeWindowOffset,
48
+ scrollToFn: index.windowScroll,
49
+ ...options
50
+ });
51
+ }
52
+
53
+ exports.Virtualizer = index.Virtualizer;
54
+ exports.defaultKeyExtractor = index.defaultKeyExtractor;
55
+ exports.defaultRangeExtractor = index.defaultRangeExtractor;
56
+ exports.elementScroll = index.elementScroll;
57
+ exports.measureElement = index.measureElement;
58
+ exports.memo = index.memo;
59
+ exports.observeElementOffset = index.observeElementOffset;
60
+ exports.observeElementRect = index.observeElementRect;
61
+ exports.observeWindowOffset = index.observeWindowOffset;
62
+ exports.observeWindowRect = index.observeWindowRect;
63
+ exports.windowScroll = index.windowScroll;
64
+ exports.createVirtualizer = createVirtualizer;
65
+ exports.createWindowVirtualizer = createWindowVirtualizer;
66
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../../../../src/index.ts"],"sourcesContent":["import {\n elementScroll,\n observeElementOffset,\n observeElementRect,\n observeWindowOffset,\n observeWindowRect,\n PartialKeys,\n Virtualizer,\n VirtualizerOptions,\n windowScroll,\n} from '@tanstack/virtual-core'\nexport * from '@tanstack/virtual-core'\n\nimport { readable, Readable } from 'svelte/store'\n\nfunction createVirtualizerBase<TScrollElement, TItemElement = unknown>(\n options: VirtualizerOptions<TScrollElement, TItemElement>,\n): Readable<Virtualizer<TScrollElement, TItemElement>> {\n const virtualizer = new Virtualizer(options)\n\n return readable(virtualizer, (set) => {\n const cleanup = virtualizer._didMount()\n virtualizer._willUpdate()\n virtualizer.setOptions({\n ...options,\n onChange: (instance: Virtualizer<TScrollElement, TItemElement>) => {\n set(instance)\n options.onChange?.(instance)\n },\n })\n\n return () => cleanup()\n })\n}\n\nexport function createVirtualizer<TScrollElement, TItemElement = unknown>(\n options: PartialKeys<\n VirtualizerOptions<TScrollElement, TItemElement>,\n 'observeElementRect' | 'observeElementOffset' | 'scrollToFn'\n >,\n): Readable<Virtualizer<TScrollElement, TItemElement>> {\n return createVirtualizerBase<TScrollElement, TItemElement>({\n observeElementRect: observeElementRect,\n observeElementOffset: observeElementOffset,\n scrollToFn: elementScroll,\n ...options,\n })\n}\n\nexport function createWindowVirtualizer<TItemElement = unknown>(\n options: PartialKeys<\n VirtualizerOptions<Window, TItemElement>,\n | 'getScrollElement'\n | 'observeElementRect'\n | 'observeElementOffset'\n | 'scrollToFn'\n >,\n): Readable<Virtualizer<Window, TItemElement>> {\n return createVirtualizerBase<Window, TItemElement>({\n getScrollElement: () => (typeof window !== 'undefined' ? window : null!),\n observeElementRect: observeWindowRect,\n observeElementOffset: observeWindowOffset,\n scrollToFn: windowScroll,\n ...options,\n })\n}\n"],"names":["createVirtualizerBase","options","virtualizer","Virtualizer","readable","set","cleanup","_didMount","_willUpdate","setOptions","onChange","instance","createVirtualizer","observeElementRect","observeElementOffset","scrollToFn","elementScroll","createWindowVirtualizer","getScrollElement","window","observeWindowRect","observeWindowOffset","windowScroll"],"mappings":";;;;;;;;;;;;;;;;;AAeA,SAASA,qBAAT,CACEC,OADF,EAEuD;AACrD,EAAA,MAAMC,WAAW,GAAG,IAAIC,iBAAJ,CAAgBF,OAAhB,CAApB,CAAA;AAEA,EAAA,OAAOG,cAAQ,CAACF,WAAD,EAAeG,GAAD,IAAS;AACpC,IAAA,MAAMC,OAAO,GAAGJ,WAAW,CAACK,SAAZ,EAAhB,CAAA;;AACAL,IAAAA,WAAW,CAACM,WAAZ,EAAA,CAAA;;AACAN,IAAAA,WAAW,CAACO,UAAZ,CAAuB,EACrB,GAAGR,OADkB;MAErBS,QAAQ,EAAGC,QAAD,IAAyD;QACjEN,GAAG,CAACM,QAAD,CAAH,CAAA;AACAV,QAAAA,OAAO,CAACS,QAAR,IAAA,IAAA,GAAA,KAAA,CAAA,GAAAT,OAAO,CAACS,QAAR,CAAmBC,QAAnB,CAAA,CAAA;AACD,OAAA;KALH,CAAA,CAAA;IAQA,OAAO,MAAML,OAAO,EAApB,CAAA;AACD,GAZc,CAAf,CAAA;AAaD,CAAA;;AAEM,SAASM,iBAAT,CACLX,OADK,EAKgD;AACrD,EAAA,OAAOD,qBAAqB,CAA+B;AACzDa,IAAAA,kBAAkB,EAAEA,wBADqC;AAEzDC,IAAAA,oBAAoB,EAAEA,0BAFmC;AAGzDC,IAAAA,UAAU,EAAEC,mBAH6C;IAIzD,GAAGf,OAAAA;AAJsD,GAA/B,CAA5B,CAAA;AAMD,CAAA;AAEM,SAASgB,uBAAT,CACLhB,OADK,EAQwC;AAC7C,EAAA,OAAOD,qBAAqB,CAAuB;IACjDkB,gBAAgB,EAAE,MAAO,OAAOC,MAAP,KAAkB,WAAlB,GAAgCA,MAAhC,GAAyC,IADjB;AAEjDN,IAAAA,kBAAkB,EAAEO,uBAF6B;AAGjDN,IAAAA,oBAAoB,EAAEO,yBAH2B;AAIjDN,IAAAA,UAAU,EAAEO,kBAJqC;IAKjD,GAAGrB,OAAAA;AAL8C,GAAvB,CAA5B,CAAA;AAOD;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,638 @@
1
+ /**
2
+ * svelte-virtual
3
+ *
4
+ * Copyright (c) TanStack
5
+ *
6
+ * This source code is licensed under the MIT license found in the
7
+ * LICENSE.md file in the root directory of this source tree.
8
+ *
9
+ * @license MIT
10
+ */
11
+ import { readable } from 'svelte/store';
12
+
13
+ /**
14
+ * virtual-core
15
+ *
16
+ * Copyright (c) TanStack
17
+ *
18
+ * This source code is licensed under the MIT license found in the
19
+ * LICENSE.md file in the root directory of this source tree.
20
+ *
21
+ * @license MIT
22
+ */
23
+ var props = ["bottom", "height", "left", "right", "top", "width"];
24
+
25
+ var rectChanged = function rectChanged(a, b) {
26
+ if (a === void 0) {
27
+ a = {};
28
+ }
29
+
30
+ if (b === void 0) {
31
+ b = {};
32
+ }
33
+
34
+ return props.some(function (prop) {
35
+ return a[prop] !== b[prop];
36
+ });
37
+ };
38
+
39
+ var observedNodes = /*#__PURE__*/new Map();
40
+ var rafId;
41
+
42
+ var run = function run() {
43
+ var changedStates = [];
44
+ observedNodes.forEach(function (state, node) {
45
+ var newRect = node.getBoundingClientRect();
46
+
47
+ if (rectChanged(newRect, state.rect)) {
48
+ state.rect = newRect;
49
+ changedStates.push(state);
50
+ }
51
+ });
52
+ changedStates.forEach(function (state) {
53
+ state.callbacks.forEach(function (cb) {
54
+ return cb(state.rect);
55
+ });
56
+ });
57
+ rafId = window.requestAnimationFrame(run);
58
+ };
59
+
60
+ function observeRect(node, cb) {
61
+ return {
62
+ observe: function observe() {
63
+ var wasEmpty = observedNodes.size === 0;
64
+
65
+ if (observedNodes.has(node)) {
66
+ observedNodes.get(node).callbacks.push(cb);
67
+ } else {
68
+ observedNodes.set(node, {
69
+ rect: undefined,
70
+ hasRectChanged: false,
71
+ callbacks: [cb]
72
+ });
73
+ }
74
+
75
+ if (wasEmpty) run();
76
+ },
77
+ unobserve: function unobserve() {
78
+ var state = observedNodes.get(node);
79
+
80
+ if (state) {
81
+ // Remove the callback
82
+ var index = state.callbacks.indexOf(cb);
83
+ if (index >= 0) state.callbacks.splice(index, 1); // Remove the node reference
84
+
85
+ if (!state.callbacks.length) observedNodes["delete"](node); // Stop the loop
86
+
87
+ if (!observedNodes.size) cancelAnimationFrame(rafId);
88
+ }
89
+ }
90
+ };
91
+ }
92
+
93
+ function memo(getDeps, fn, opts) {
94
+ let deps = [];
95
+ let result;
96
+ return () => {
97
+ let depTime;
98
+ if (opts.key && opts.debug != null && opts.debug()) depTime = Date.now();
99
+ const newDeps = getDeps();
100
+ const depsChanged = newDeps.length !== deps.length || newDeps.some((dep, index) => deps[index] !== dep);
101
+
102
+ if (!depsChanged) {
103
+ return result;
104
+ }
105
+
106
+ deps = newDeps;
107
+ let resultTime;
108
+ if (opts.key && opts.debug != null && opts.debug()) resultTime = Date.now();
109
+ result = fn(...newDeps);
110
+ opts == null ? void 0 : opts.onChange == null ? void 0 : opts.onChange(result);
111
+
112
+ if (opts.key && opts.debug != null && opts.debug()) {
113
+ const depEndTime = Math.round((Date.now() - depTime) * 100) / 100;
114
+ const resultEndTime = Math.round((Date.now() - resultTime) * 100) / 100;
115
+ const resultFpsPercentage = resultEndTime / 16;
116
+
117
+ const pad = (str, num) => {
118
+ str = String(str);
119
+
120
+ while (str.length < num) {
121
+ str = ' ' + str;
122
+ }
123
+
124
+ return str;
125
+ };
126
+
127
+ console.info("%c\u23F1 " + pad(resultEndTime, 5) + " /" + pad(depEndTime, 5) + " ms", "\n font-size: .6rem;\n font-weight: bold;\n color: hsl(" + Math.max(0, Math.min(120 - 120 * resultFpsPercentage, 120)) + "deg 100% 31%);", opts == null ? void 0 : opts.key);
128
+ }
129
+
130
+ return result;
131
+ };
132
+ }
133
+
134
+ //
135
+ const defaultKeyExtractor = index => index;
136
+ const defaultRangeExtractor = range => {
137
+ const start = Math.max(range.startIndex - range.overscan, 0);
138
+ const end = Math.min(range.endIndex + range.overscan, range.count - 1);
139
+ const arr = [];
140
+
141
+ for (let i = start; i <= end; i++) {
142
+ arr.push(i);
143
+ }
144
+
145
+ return arr;
146
+ };
147
+
148
+ const memoRectCallback = (instance, cb) => {
149
+ let prev = {
150
+ height: -1,
151
+ width: -1
152
+ };
153
+ return rect => {
154
+ if (instance.options.horizontal ? rect.width !== prev.width : rect.height !== prev.height) {
155
+ cb(rect);
156
+ }
157
+
158
+ prev = rect;
159
+ };
160
+ };
161
+
162
+ const observeElementRect = (instance, cb) => {
163
+ const onResize = memoRectCallback(instance, cb);
164
+ const observer = observeRect(instance.scrollElement, rect => {
165
+ onResize(rect);
166
+ });
167
+
168
+ if (!instance.scrollElement) {
169
+ return;
170
+ }
171
+
172
+ onResize(instance.scrollElement.getBoundingClientRect());
173
+ observer.observe();
174
+ return () => {
175
+ observer.unobserve();
176
+ };
177
+ };
178
+ const observeWindowRect = (instance, cb) => {
179
+ const memoizedCallback = memoRectCallback(instance, cb);
180
+
181
+ const onResize = () => memoizedCallback({
182
+ width: instance.scrollElement.innerWidth,
183
+ height: instance.scrollElement.innerHeight
184
+ });
185
+
186
+ if (!instance.scrollElement) {
187
+ return;
188
+ }
189
+
190
+ onResize();
191
+ instance.scrollElement.addEventListener('resize', onResize, {
192
+ capture: false,
193
+ passive: true
194
+ });
195
+ return () => {
196
+ instance.scrollElement.removeEventListener('resize', onResize);
197
+ };
198
+ };
199
+ const scrollProps = {
200
+ element: ['scrollLeft', 'scrollTop'],
201
+ window: ['scrollX', 'scrollY']
202
+ };
203
+
204
+ const createOffsetObserver = mode => {
205
+ return (instance, cb) => {
206
+ if (!instance.scrollElement) {
207
+ return;
208
+ }
209
+
210
+ const propX = scrollProps[mode][0];
211
+ const propY = scrollProps[mode][1];
212
+ let prevX = instance.scrollElement[propX];
213
+ let prevY = instance.scrollElement[propY];
214
+
215
+ const scroll = () => {
216
+ cb(instance.scrollElement[instance.options.horizontal ? propX : propY]);
217
+ };
218
+
219
+ scroll();
220
+
221
+ const onScroll = e => {
222
+ const target = e.currentTarget;
223
+ const scrollX = target[propX];
224
+ const scrollY = target[propY];
225
+
226
+ if (instance.options.horizontal ? prevX - scrollX : prevY - scrollY) {
227
+ scroll();
228
+ }
229
+
230
+ prevX = scrollX;
231
+ prevY = scrollY;
232
+ };
233
+
234
+ instance.scrollElement.addEventListener('scroll', onScroll, {
235
+ capture: false,
236
+ passive: true
237
+ });
238
+ return () => {
239
+ instance.scrollElement.removeEventListener('scroll', onScroll);
240
+ };
241
+ };
242
+ };
243
+
244
+ const observeElementOffset = createOffsetObserver('element');
245
+ const observeWindowOffset = createOffsetObserver('window');
246
+ const measureElement = (element, instance) => {
247
+ return element.getBoundingClientRect()[instance.options.horizontal ? 'width' : 'height'];
248
+ };
249
+ const windowScroll = (offset, canSmooth, instance) => {
250
+ var _instance$scrollEleme;
251
+ (_instance$scrollEleme = instance.scrollElement) == null ? void 0 : _instance$scrollEleme.scrollTo({
252
+ [instance.options.horizontal ? 'left' : 'top']: offset,
253
+ behavior: canSmooth ? 'smooth' : undefined
254
+ });
255
+ };
256
+ const elementScroll = (offset, canSmooth, instance) => {
257
+ var _instance$scrollEleme2;
258
+ (_instance$scrollEleme2 = instance.scrollElement) == null ? void 0 : _instance$scrollEleme2.scrollTo({
259
+ [instance.options.horizontal ? 'left' : 'top']: offset,
260
+ behavior: canSmooth ? 'smooth' : undefined
261
+ });
262
+ };
263
+ class Virtualizer {
264
+ constructor(_opts) {
265
+ var _this = this;
266
+
267
+ this.unsubs = [];
268
+ this.scrollElement = null;
269
+ this.measurementsCache = [];
270
+ this.itemMeasurementsCache = {};
271
+ this.pendingMeasuredCacheIndexes = [];
272
+ this.measureElementCache = {};
273
+
274
+ this.setOptions = opts => {
275
+ Object.entries(opts).forEach(_ref => {
276
+ let [key, value] = _ref;
277
+ if (typeof value === 'undefined') delete opts[key];
278
+ });
279
+ this.options = {
280
+ debug: false,
281
+ initialOffset: 0,
282
+ overscan: 1,
283
+ paddingStart: 0,
284
+ paddingEnd: 0,
285
+ scrollPaddingStart: 0,
286
+ scrollPaddingEnd: 0,
287
+ horizontal: false,
288
+ getItemKey: defaultKeyExtractor,
289
+ rangeExtractor: defaultRangeExtractor,
290
+ enableSmoothScroll: true,
291
+ onChange: () => {},
292
+ measureElement,
293
+ initialRect: {
294
+ width: 0,
295
+ height: 0
296
+ },
297
+ ...opts
298
+ };
299
+ };
300
+
301
+ this.notify = () => {
302
+ var _this$options$onChang, _this$options;
303
+
304
+ (_this$options$onChang = (_this$options = this.options).onChange) == null ? void 0 : _this$options$onChang.call(_this$options, this);
305
+ };
306
+
307
+ this.cleanup = () => {
308
+ this.unsubs.filter(Boolean).forEach(d => d());
309
+ this.unsubs = [];
310
+ this.scrollElement = null;
311
+ };
312
+
313
+ this._didMount = () => {
314
+ return () => {
315
+ this.cleanup();
316
+ };
317
+ };
318
+
319
+ this._willUpdate = () => {
320
+ const scrollElement = this.options.getScrollElement();
321
+
322
+ if (this.scrollElement !== scrollElement) {
323
+ this.cleanup();
324
+ this.scrollElement = scrollElement;
325
+ this.unsubs.push(this.options.observeElementRect(this, rect => {
326
+ this.scrollRect = rect;
327
+ this.notify();
328
+ }));
329
+ this.unsubs.push(this.options.observeElementOffset(this, offset => {
330
+ this.scrollOffset = offset;
331
+ this.notify();
332
+ }));
333
+ }
334
+ };
335
+
336
+ this.getSize = () => {
337
+ return this.scrollRect[this.options.horizontal ? 'width' : 'height'];
338
+ };
339
+
340
+ this.getMeasurements = memo(() => [this.options.count, this.options.paddingStart, this.options.getItemKey, this.itemMeasurementsCache], (count, paddingStart, getItemKey, measurementsCache) => {
341
+ const min = this.pendingMeasuredCacheIndexes.length > 0 ? Math.min(...this.pendingMeasuredCacheIndexes) : 0;
342
+ this.pendingMeasuredCacheIndexes = [];
343
+ const measurements = this.measurementsCache.slice(0, min);
344
+
345
+ for (let i = min; i < count; i++) {
346
+ const key = getItemKey(i);
347
+ const measuredSize = measurementsCache[key];
348
+ const start = measurements[i - 1] ? measurements[i - 1].end : paddingStart;
349
+ const size = typeof measuredSize === 'number' ? measuredSize : this.options.estimateSize(i);
350
+ const end = start + size;
351
+ measurements[i] = {
352
+ index: i,
353
+ start,
354
+ size,
355
+ end,
356
+ key
357
+ };
358
+ }
359
+
360
+ this.measurementsCache = measurements;
361
+ return measurements;
362
+ }, {
363
+ key: process.env.NODE_ENV === 'development' && 'getMeasurements',
364
+ debug: () => this.options.debug
365
+ });
366
+ this.calculateRange = memo(() => [this.getMeasurements(), this.getSize(), this.scrollOffset], (measurements, outerSize, scrollOffset) => {
367
+ return calculateRange({
368
+ measurements,
369
+ outerSize,
370
+ scrollOffset
371
+ });
372
+ }, {
373
+ key: process.env.NODE_ENV === 'development' && 'calculateRange',
374
+ debug: () => this.options.debug
375
+ });
376
+ this.getIndexes = memo(() => [this.options.rangeExtractor, this.calculateRange(), this.options.overscan, this.options.count], (rangeExtractor, range, overscan, count) => {
377
+ return rangeExtractor({ ...range,
378
+ overscan,
379
+ count: count
380
+ });
381
+ }, {
382
+ key: process.env.NODE_ENV === 'development' && 'getIndexes'
383
+ });
384
+ this.getVirtualItems = memo(() => [this.getIndexes(), this.getMeasurements(), this.options.measureElement], (indexes, measurements, measureElement) => {
385
+ const makeMeasureElement = index => measurableItem => {
386
+ var _this$itemMeasurement;
387
+
388
+ const item = this.measurementsCache[index];
389
+
390
+ if (!measurableItem) {
391
+ return;
392
+ }
393
+
394
+ const measuredItemSize = measureElement(measurableItem, this);
395
+ const itemSize = (_this$itemMeasurement = this.itemMeasurementsCache[item.key]) != null ? _this$itemMeasurement : item.size;
396
+
397
+ if (measuredItemSize !== itemSize) {
398
+ if (item.start < this.scrollOffset) {
399
+ if (process.env.NODE_ENV === 'development' && this.options.debug) {
400
+ console.info('correction', measuredItemSize - itemSize);
401
+ }
402
+
403
+ if (!this.destinationOffset) {
404
+ this._scrollToOffset(this.scrollOffset + (measuredItemSize - itemSize), false);
405
+ }
406
+ }
407
+
408
+ this.pendingMeasuredCacheIndexes.push(index);
409
+ this.itemMeasurementsCache = { ...this.itemMeasurementsCache,
410
+ [item.key]: measuredItemSize
411
+ };
412
+ this.notify();
413
+ }
414
+ };
415
+
416
+ const virtualItems = [];
417
+ const currentMeasureElements = {};
418
+
419
+ for (let k = 0, len = indexes.length; k < len; k++) {
420
+ var _this$measureElementC;
421
+
422
+ const i = indexes[k];
423
+ const measurement = measurements[i];
424
+ const item = { ...measurement,
425
+ measureElement: currentMeasureElements[i] = (_this$measureElementC = this.measureElementCache[i]) != null ? _this$measureElementC : makeMeasureElement(i)
426
+ };
427
+ virtualItems.push(item);
428
+ }
429
+
430
+ this.measureElementCache = currentMeasureElements;
431
+ return virtualItems;
432
+ }, {
433
+ key: process.env.NODE_ENV === 'development' && 'getIndexes'
434
+ });
435
+
436
+ this.scrollToOffset = function (toOffset, _temp) {
437
+ let {
438
+ align
439
+ } = _temp === void 0 ? {
440
+ align: 'start'
441
+ } : _temp;
442
+
443
+ const attempt = () => {
444
+ const offset = _this.scrollOffset;
445
+
446
+ const size = _this.getSize();
447
+
448
+ if (align === 'auto') {
449
+ if (toOffset <= offset) {
450
+ align = 'start';
451
+ } else if (toOffset >= offset + size) {
452
+ align = 'end';
453
+ } else {
454
+ align = 'start';
455
+ }
456
+ }
457
+
458
+ if (align === 'start') {
459
+ _this._scrollToOffset(toOffset, true);
460
+ } else if (align === 'end') {
461
+ _this._scrollToOffset(toOffset - size, true);
462
+ } else if (align === 'center') {
463
+ _this._scrollToOffset(toOffset - size / 2, true);
464
+ }
465
+ };
466
+
467
+ attempt();
468
+ requestAnimationFrame(() => {
469
+ attempt();
470
+ });
471
+ };
472
+
473
+ this.scrollToIndex = function (index, _temp2) {
474
+ let {
475
+ align,
476
+ ...rest
477
+ } = _temp2 === void 0 ? {
478
+ align: 'auto'
479
+ } : _temp2;
480
+
481
+ const measurements = _this.getMeasurements();
482
+
483
+ const offset = _this.scrollOffset;
484
+
485
+ const size = _this.getSize();
486
+
487
+ const {
488
+ count
489
+ } = _this.options;
490
+ const measurement = measurements[Math.max(0, Math.min(index, count - 1))];
491
+
492
+ if (!measurement) {
493
+ return;
494
+ }
495
+
496
+ if (align === 'auto') {
497
+ if (measurement.end >= offset + size - _this.options.scrollPaddingEnd) {
498
+ align = 'end';
499
+ } else if (measurement.start <= offset + _this.options.scrollPaddingStart) {
500
+ align = 'start';
501
+ } else {
502
+ return;
503
+ }
504
+ }
505
+
506
+ const toOffset = align === 'end' ? measurement.end + _this.options.scrollPaddingEnd : measurement.start - _this.options.scrollPaddingStart;
507
+
508
+ _this.scrollToOffset(toOffset, {
509
+ align,
510
+ ...rest
511
+ });
512
+ };
513
+
514
+ this.getTotalSize = () => {
515
+ var _this$getMeasurements;
516
+
517
+ return (((_this$getMeasurements = this.getMeasurements()[this.options.count - 1]) == null ? void 0 : _this$getMeasurements.end) || this.options.paddingStart) + this.options.paddingEnd;
518
+ };
519
+
520
+ this._scrollToOffset = (offset, canSmooth) => {
521
+ clearTimeout(this.scrollCheckFrame);
522
+ this.destinationOffset = offset;
523
+ this.options.scrollToFn(offset, this.options.enableSmoothScroll && canSmooth, this);
524
+ let scrollCheckFrame;
525
+
526
+ const check = () => {
527
+ let lastOffset = this.scrollOffset;
528
+ this.scrollCheckFrame = scrollCheckFrame = setTimeout(() => {
529
+ if (this.scrollCheckFrame !== scrollCheckFrame) {
530
+ return;
531
+ }
532
+
533
+ if (this.scrollOffset === lastOffset) {
534
+ this.destinationOffset = undefined;
535
+ return;
536
+ }
537
+
538
+ lastOffset = this.scrollOffset;
539
+ check();
540
+ }, 100);
541
+ };
542
+
543
+ check();
544
+ };
545
+
546
+ this.measure = () => {
547
+ this.itemMeasurementsCache = {};
548
+ this.notify();
549
+ };
550
+
551
+ this.setOptions(_opts);
552
+ this.scrollRect = this.options.initialRect;
553
+ this.scrollOffset = this.options.initialOffset;
554
+ }
555
+
556
+ }
557
+
558
+ const findNearestBinarySearch = (low, high, getCurrentValue, value) => {
559
+ while (low <= high) {
560
+ const middle = (low + high) / 2 | 0;
561
+ const currentValue = getCurrentValue(middle);
562
+
563
+ if (currentValue < value) {
564
+ low = middle + 1;
565
+ } else if (currentValue > value) {
566
+ high = middle - 1;
567
+ } else {
568
+ return middle;
569
+ }
570
+ }
571
+
572
+ if (low > 0) {
573
+ return low - 1;
574
+ } else {
575
+ return 0;
576
+ }
577
+ };
578
+
579
+ function calculateRange(_ref2) {
580
+ let {
581
+ measurements,
582
+ outerSize,
583
+ scrollOffset
584
+ } = _ref2;
585
+ const count = measurements.length - 1;
586
+
587
+ const getOffset = index => measurements[index].start;
588
+
589
+ const startIndex = findNearestBinarySearch(0, count, getOffset, scrollOffset);
590
+ let endIndex = startIndex;
591
+
592
+ while (endIndex < count && measurements[endIndex].end < scrollOffset + outerSize) {
593
+ endIndex++;
594
+ }
595
+
596
+ return {
597
+ startIndex,
598
+ endIndex
599
+ };
600
+ }
601
+
602
+ function createVirtualizerBase(options) {
603
+ const virtualizer = new Virtualizer(options);
604
+ return readable(virtualizer, set => {
605
+ const cleanup = virtualizer._didMount();
606
+
607
+ virtualizer._willUpdate();
608
+
609
+ virtualizer.setOptions({ ...options,
610
+ onChange: instance => {
611
+ set(instance);
612
+ options.onChange == null ? void 0 : options.onChange(instance);
613
+ }
614
+ });
615
+ return () => cleanup();
616
+ });
617
+ }
618
+
619
+ function createVirtualizer(options) {
620
+ return createVirtualizerBase({
621
+ observeElementRect: observeElementRect,
622
+ observeElementOffset: observeElementOffset,
623
+ scrollToFn: elementScroll,
624
+ ...options
625
+ });
626
+ }
627
+ function createWindowVirtualizer(options) {
628
+ return createVirtualizerBase({
629
+ getScrollElement: () => typeof window !== 'undefined' ? window : null,
630
+ observeElementRect: observeWindowRect,
631
+ observeElementOffset: observeWindowOffset,
632
+ scrollToFn: windowScroll,
633
+ ...options
634
+ });
635
+ }
636
+
637
+ export { Virtualizer, createVirtualizer, createWindowVirtualizer, defaultKeyExtractor, defaultRangeExtractor, elementScroll, measureElement, memo, observeElementOffset, observeElementRect, observeWindowOffset, observeWindowRect, windowScroll };
638
+ //# sourceMappingURL=index.js.map