@tanstack/svelte-virtual 3.0.0-beta.30 → 3.0.0-beta.33

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.
@@ -14,6 +14,46 @@
14
14
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.SvelteVirtual = {}, global.SvelteStore));
15
15
  })(this, (function (exports, store) { 'use strict';
16
16
 
17
+ function _extends$1() {
18
+ _extends$1 = Object.assign ? Object.assign.bind() : function (target) {
19
+ for (var i = 1; i < arguments.length; i++) {
20
+ var source = arguments[i];
21
+ for (var key in source) {
22
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
23
+ target[key] = source[key];
24
+ }
25
+ }
26
+ }
27
+ return target;
28
+ };
29
+ return _extends$1.apply(this, arguments);
30
+ }
31
+
32
+ /**
33
+ * virtual-core
34
+ *
35
+ * Copyright (c) TanStack
36
+ *
37
+ * This source code is licensed under the MIT license found in the
38
+ * LICENSE.md file in the root directory of this source tree.
39
+ *
40
+ * @license MIT
41
+ */
42
+ function _extends() {
43
+ _extends = Object.assign ? Object.assign.bind() : function (target) {
44
+ for (var i = 1; i < arguments.length; i++) {
45
+ var source = arguments[i];
46
+ for (var key in source) {
47
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
48
+ target[key] = source[key];
49
+ }
50
+ }
51
+ }
52
+ return target;
53
+ };
54
+ return _extends.apply(this, arguments);
55
+ }
56
+
17
57
  /**
18
58
  * virtual-core
19
59
  *
@@ -25,36 +65,35 @@
25
65
  * @license MIT
26
66
  */
27
67
  function memo(getDeps, fn, opts) {
28
- let deps = [];
29
- let result;
30
- return () => {
31
- let depTime;
68
+ var deps = [];
69
+ var result;
70
+ return function () {
71
+ var depTime;
32
72
  if (opts.key && opts.debug != null && opts.debug()) depTime = Date.now();
33
- const newDeps = getDeps();
34
- const depsChanged = newDeps.length !== deps.length || newDeps.some((dep, index) => deps[index] !== dep);
73
+ var newDeps = getDeps();
74
+ var depsChanged = newDeps.length !== deps.length || newDeps.some(function (dep, index) {
75
+ return deps[index] !== dep;
76
+ });
35
77
  if (!depsChanged) {
36
78
  return result;
37
79
  }
38
80
  deps = newDeps;
39
- let resultTime;
81
+ var resultTime;
40
82
  if (opts.key && opts.debug != null && opts.debug()) resultTime = Date.now();
41
- result = fn(...newDeps);
83
+ result = fn.apply(void 0, newDeps);
42
84
  opts == null ? void 0 : opts.onChange == null ? void 0 : opts.onChange(result);
43
85
  if (opts.key && opts.debug != null && opts.debug()) {
44
- const depEndTime = Math.round((Date.now() - depTime) * 100) / 100;
45
- const resultEndTime = Math.round((Date.now() - resultTime) * 100) / 100;
46
- const resultFpsPercentage = resultEndTime / 16;
47
- const pad = (str, num) => {
86
+ var depEndTime = Math.round((Date.now() - depTime) * 100) / 100;
87
+ var resultEndTime = Math.round((Date.now() - resultTime) * 100) / 100;
88
+ var resultFpsPercentage = resultEndTime / 16;
89
+ var pad = function pad(str, num) {
48
90
  str = String(str);
49
91
  while (str.length < num) {
50
92
  str = ' ' + str;
51
93
  }
52
94
  return str;
53
95
  };
54
- console.info(`%c⏱ ${pad(resultEndTime, 5)} /${pad(depEndTime, 5)} ms`, `
55
- font-size: .6rem;
56
- font-weight: bold;
57
- color: hsl(${Math.max(0, Math.min(120 - 120 * resultFpsPercentage, 120))}deg 100% 31%);`, opts == null ? void 0 : opts.key);
96
+ 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);
58
97
  }
59
98
  return result;
60
99
  };
@@ -75,30 +114,32 @@
75
114
 
76
115
  //
77
116
 
78
- const defaultKeyExtractor = index => index;
79
- const defaultRangeExtractor = range => {
80
- const start = Math.max(range.startIndex - range.overscan, 0);
81
- const end = Math.min(range.endIndex + range.overscan, range.count - 1);
82
- const arr = [];
83
- for (let i = start; i <= end; i++) {
84
- arr.push(i);
117
+ var defaultKeyExtractor = function defaultKeyExtractor(index) {
118
+ return index;
119
+ };
120
+ var defaultRangeExtractor = function defaultRangeExtractor(range) {
121
+ var start = Math.max(range.startIndex - range.overscan, 0);
122
+ var end = Math.min(range.endIndex + range.overscan, range.count - 1);
123
+ var arr = [];
124
+ for (var _i = start; _i <= end; _i++) {
125
+ arr.push(_i);
85
126
  }
86
127
  return arr;
87
128
  };
88
- const memoRectCallback = (instance, cb) => {
89
- let prev = {
129
+ var memoRectCallback = function memoRectCallback(instance, cb) {
130
+ var prev = {
90
131
  height: -1,
91
132
  width: -1
92
133
  };
93
- return rect => {
134
+ return function (rect) {
94
135
  if (instance.options.horizontal ? rect.width !== prev.width : rect.height !== prev.height) {
95
136
  cb(rect);
96
137
  }
97
138
  prev = rect;
98
139
  };
99
140
  };
100
- const observeElementRect = (instance, cb) => {
101
- const observer = new ResizeObserver(entries => {
141
+ var observeElementRect = function observeElementRect(instance, cb) {
142
+ var observer = new ResizeObserver(function (entries) {
102
143
  var _entries$, _entries$2;
103
144
  cb({
104
145
  width: (_entries$ = entries[0]) == null ? void 0 : _entries$.contentRect.width,
@@ -110,16 +151,18 @@
110
151
  }
111
152
  cb(instance.scrollElement.getBoundingClientRect());
112
153
  observer.observe(instance.scrollElement);
113
- return () => {
154
+ return function () {
114
155
  observer.unobserve(instance.scrollElement);
115
156
  };
116
157
  };
117
- const observeWindowRect = (instance, cb) => {
118
- const memoizedCallback = memoRectCallback(instance, cb);
119
- const onResize = () => memoizedCallback({
120
- width: instance.scrollElement.innerWidth,
121
- height: instance.scrollElement.innerHeight
122
- });
158
+ var observeWindowRect = function observeWindowRect(instance, cb) {
159
+ var memoizedCallback = memoRectCallback(instance, cb);
160
+ var onResize = function onResize() {
161
+ return memoizedCallback({
162
+ width: instance.scrollElement.innerWidth,
163
+ height: instance.scrollElement.innerHeight
164
+ });
165
+ };
123
166
  if (!instance.scrollElement) {
124
167
  return;
125
168
  }
@@ -128,32 +171,32 @@
128
171
  capture: false,
129
172
  passive: true
130
173
  });
131
- return () => {
174
+ return function () {
132
175
  instance.scrollElement.removeEventListener('resize', onResize);
133
176
  };
134
177
  };
135
- const scrollProps = {
178
+ var scrollProps = {
136
179
  element: ['scrollLeft', 'scrollTop'],
137
180
  window: ['scrollX', 'scrollY']
138
181
  };
139
- const createOffsetObserver = mode => {
140
- return (instance, cb) => {
182
+ var createOffsetObserver = function createOffsetObserver(mode) {
183
+ return function (instance, cb) {
141
184
  if (!instance.scrollElement) {
142
185
  return;
143
186
  }
144
- const propX = scrollProps[mode][0];
145
- const propY = scrollProps[mode][1];
146
- let prevX = instance.scrollElement[propX];
147
- let prevY = instance.scrollElement[propY];
148
- const scroll = () => {
149
- const offset = instance.scrollElement[instance.options.horizontal ? propX : propY];
150
- cb(Math.max(0, offset - instance.options.scrollMargin));
187
+ var propX = scrollProps[mode][0];
188
+ var propY = scrollProps[mode][1];
189
+ var prevX = instance.scrollElement[propX];
190
+ var prevY = instance.scrollElement[propY];
191
+ var scroll = function scroll() {
192
+ var offset = instance.scrollElement[instance.options.horizontal ? propX : propY];
193
+ cb(offset);
151
194
  };
152
195
  scroll();
153
- const onScroll = e => {
154
- const target = e.currentTarget;
155
- const scrollX = target[propX];
156
- const scrollY = target[propY];
196
+ var onScroll = function onScroll(e) {
197
+ var target = e.currentTarget;
198
+ var scrollX = target[propX];
199
+ var scrollY = target[propY];
157
200
  if (instance.options.horizontal ? prevX - scrollX : prevY - scrollY) {
158
201
  scroll();
159
202
  }
@@ -164,390 +207,407 @@
164
207
  capture: false,
165
208
  passive: true
166
209
  });
167
- return () => {
210
+ return function () {
168
211
  instance.scrollElement.removeEventListener('scroll', onScroll);
169
212
  };
170
213
  };
171
214
  };
172
- const observeElementOffset = createOffsetObserver('element');
173
- const observeWindowOffset = createOffsetObserver('window');
174
- const measureElement = (element, instance) => {
215
+ var observeElementOffset = createOffsetObserver('element');
216
+ var observeWindowOffset = createOffsetObserver('window');
217
+ var measureElement = function measureElement(element, instance) {
175
218
  return Math.round(element.getBoundingClientRect()[instance.options.horizontal ? 'width' : 'height']);
176
219
  };
177
- const windowScroll = (offset, _ref, instance) => {
178
- var _instance$scrollEleme;
179
- let {
180
- adjustments,
181
- behavior,
182
- sync
183
- } = _ref;
184
- const toOffset = (sync ? offset : offset + instance.options.scrollMargin) + (adjustments ?? 0);
185
- (_instance$scrollEleme = instance.scrollElement) == null ? void 0 : _instance$scrollEleme.scrollTo == null ? void 0 : _instance$scrollEleme.scrollTo({
186
- [instance.options.horizontal ? 'left' : 'top']: toOffset,
187
- behavior
188
- });
220
+ var windowScroll = function windowScroll(offset, _ref, instance) {
221
+ var _instance$scrollEleme, _instance$scrollEleme2;
222
+ var _ref$adjustments = _ref.adjustments,
223
+ adjustments = _ref$adjustments === void 0 ? 0 : _ref$adjustments,
224
+ behavior = _ref.behavior;
225
+ var toOffset = offset + adjustments;
226
+ (_instance$scrollEleme = instance.scrollElement) == null ? void 0 : _instance$scrollEleme.scrollTo == null ? void 0 : _instance$scrollEleme.scrollTo((_instance$scrollEleme2 = {}, _instance$scrollEleme2[instance.options.horizontal ? 'left' : 'top'] = toOffset, _instance$scrollEleme2.behavior = behavior, _instance$scrollEleme2));
189
227
  };
190
- const elementScroll = (offset, _ref2, instance) => {
191
- var _instance$scrollEleme2;
192
- let {
193
- adjustments,
194
- behavior,
195
- sync
196
- } = _ref2;
197
- const toOffset = (sync ? offset : offset + instance.options.scrollMargin) + (adjustments ?? 0);
198
- (_instance$scrollEleme2 = instance.scrollElement) == null ? void 0 : _instance$scrollEleme2.scrollTo == null ? void 0 : _instance$scrollEleme2.scrollTo({
199
- [instance.options.horizontal ? 'left' : 'top']: toOffset,
200
- behavior
201
- });
228
+ var elementScroll = function elementScroll(offset, _ref2, instance) {
229
+ var _instance$scrollEleme3, _instance$scrollEleme4;
230
+ var _ref2$adjustments = _ref2.adjustments,
231
+ adjustments = _ref2$adjustments === void 0 ? 0 : _ref2$adjustments,
232
+ behavior = _ref2.behavior;
233
+ var toOffset = offset + adjustments;
234
+ (_instance$scrollEleme3 = instance.scrollElement) == null ? void 0 : _instance$scrollEleme3.scrollTo == null ? void 0 : _instance$scrollEleme3.scrollTo((_instance$scrollEleme4 = {}, _instance$scrollEleme4[instance.options.horizontal ? 'left' : 'top'] = toOffset, _instance$scrollEleme4.behavior = behavior, _instance$scrollEleme4));
202
235
  };
203
- class Virtualizer {
204
- constructor(_opts) {
205
- var _this = this;
206
- this.unsubs = [];
207
- this.scrollElement = null;
208
- this.isScrolling = false;
209
- this.isScrollingTimeoutId = null;
210
- this.measurementsCache = [];
211
- this.itemMeasurementsCache = {};
212
- this.pendingMeasuredCacheIndexes = [];
213
- this.scrollAdjustments = 0;
214
- this.measureElementCache = {};
215
- this.pendingScrollToIndexCallback = null;
216
- this.getResizeObserver = (() => {
217
- let _ro = null;
218
- return () => {
219
- if (_ro) {
220
- return _ro;
221
- } else if (typeof ResizeObserver !== 'undefined') {
222
- return _ro = new ResizeObserver(entries => {
223
- entries.forEach(entry => {
224
- this._measureElement(entry.target, false);
225
- });
236
+ var Virtualizer = function Virtualizer(_opts) {
237
+ var _this = this;
238
+ this.unsubs = [];
239
+ this.scrollElement = null;
240
+ this.isScrolling = false;
241
+ this.isScrollingTimeoutId = null;
242
+ this.measurementsCache = [];
243
+ this.itemSizeCache = {};
244
+ this.pendingMeasuredCacheIndexes = [];
245
+ this.scrollDirection = null;
246
+ this.scrollAdjustments = 0;
247
+ this.measureElementCache = {};
248
+ this.pendingScrollToIndexCallback = null;
249
+ this.getResizeObserver = function () {
250
+ var _ro = null;
251
+ return function () {
252
+ if (_ro) {
253
+ return _ro;
254
+ } else if (typeof ResizeObserver !== 'undefined') {
255
+ return _ro = new ResizeObserver(function (entries) {
256
+ entries.forEach(function (entry) {
257
+ _this._measureElement(entry.target, false);
226
258
  });
227
- } else {
228
- return null;
229
- }
230
- };
231
- })();
232
- this.range = {
233
- startIndex: 0,
234
- endIndex: 0
235
- };
236
- this.setOptions = opts => {
237
- Object.entries(opts).forEach(_ref3 => {
238
- let [key, value] = _ref3;
239
- if (typeof value === 'undefined') delete opts[key];
240
- });
241
- this.options = {
242
- debug: false,
243
- initialOffset: 0,
244
- overscan: 1,
245
- paddingStart: 0,
246
- paddingEnd: 0,
247
- scrollPaddingStart: 0,
248
- scrollPaddingEnd: 0,
249
- horizontal: false,
250
- getItemKey: defaultKeyExtractor,
251
- rangeExtractor: defaultRangeExtractor,
252
- onChange: () => {},
253
- measureElement,
254
- initialRect: {
255
- width: 0,
256
- height: 0
257
- },
258
- scrollMargin: 0,
259
- scrollingDelay: 150,
260
- indexAttribute: 'data-index',
261
- ...opts
262
- };
263
- };
264
- this.notify = () => {
265
- var _this$options$onChang, _this$options;
266
- (_this$options$onChang = (_this$options = this.options).onChange) == null ? void 0 : _this$options$onChang.call(_this$options, this);
267
- };
268
- this.cleanup = () => {
269
- this.unsubs.filter(Boolean).forEach(d => d());
270
- this.unsubs = [];
271
- this.scrollElement = null;
272
- };
273
- this._didMount = () => {
274
- const ro = this.getResizeObserver();
275
- Object.values(this.measureElementCache).forEach(node => ro == null ? void 0 : ro.observe(node));
276
- return () => {
277
- ro == null ? void 0 : ro.disconnect();
278
- this.cleanup();
279
- };
280
- };
281
- this._willUpdate = () => {
282
- var _this$pendingScrollTo;
283
- (_this$pendingScrollTo = this.pendingScrollToIndexCallback) == null ? void 0 : _this$pendingScrollTo.call(this);
284
- const scrollElement = this.options.getScrollElement();
285
- if (this.scrollElement !== scrollElement) {
286
- this.cleanup();
287
- this.scrollElement = scrollElement;
288
- this._scrollToOffset(this.scrollOffset, {
289
- adjustments: undefined,
290
- behavior: undefined,
291
- sync: true
292
259
  });
293
- this.unsubs.push(this.options.observeElementRect(this, rect => {
294
- this.scrollRect = rect;
295
- this.calculateRange();
296
- }));
297
- this.unsubs.push(this.options.observeElementOffset(this, offset => {
298
- if (this.isScrollingTimeoutId !== null) {
299
- clearTimeout(this.isScrollingTimeoutId);
300
- this.isScrollingTimeoutId = null;
301
- }
302
- const onIsScrollingChange = isScrolling => {
303
- if (this.isScrolling !== isScrolling) {
304
- this.isScrolling = isScrolling;
305
- this.notify();
306
- }
307
- };
308
- this.scrollAdjustments = 0;
309
- if (this.scrollOffset !== offset) {
310
- this.scrollOffset = offset;
311
- onIsScrollingChange(true);
312
- }
313
- this.calculateRange();
314
- this.isScrollingTimeoutId = setTimeout(() => {
315
- this.isScrollingTimeoutId = null;
316
- onIsScrollingChange(false);
317
- }, this.options.scrollingDelay);
318
- }));
319
- } else if (!this.isScrolling) {
320
- this.calculateRange();
260
+ } else {
261
+ return null;
321
262
  }
322
263
  };
323
- this.getSize = () => {
324
- return this.scrollRect[this.options.horizontal ? 'width' : 'height'];
325
- };
326
- this.getMeasurements = memo(() => [this.options.count, this.options.paddingStart, this.options.getItemKey, this.itemMeasurementsCache], (count, paddingStart, getItemKey, measurementsCache) => {
327
- const min = this.pendingMeasuredCacheIndexes.length > 0 ? Math.min(...this.pendingMeasuredCacheIndexes) : 0;
328
- this.pendingMeasuredCacheIndexes = [];
329
- const measurements = this.measurementsCache.slice(0, min);
330
- for (let i = min; i < count; i++) {
331
- const key = getItemKey(i);
332
- const measuredSize = measurementsCache[key];
333
- const start = measurements[i - 1] ? measurements[i - 1].end : paddingStart;
334
- const size = typeof measuredSize === 'number' ? measuredSize : this.options.estimateSize(i);
335
- const end = start + size;
336
- measurements[i] = {
337
- index: i,
338
- start,
339
- size,
340
- end,
341
- key
342
- };
343
- }
344
- this.measurementsCache = measurements;
345
- return measurements;
346
- }, {
347
- key: 'getMeasurements',
348
- debug: () => this.options.debug
264
+ }();
265
+ this.range = {
266
+ startIndex: 0,
267
+ endIndex: 0
268
+ };
269
+ this.setOptions = function (opts) {
270
+ Object.entries(opts).forEach(function (_ref3) {
271
+ var key = _ref3[0],
272
+ value = _ref3[1];
273
+ if (typeof value === 'undefined') delete opts[key];
349
274
  });
350
- this.calculateRange = memo(() => [this.getMeasurements(), this.getSize(), this.scrollOffset], (measurements, outerSize, scrollOffset) => {
351
- const range = calculateRange({
352
- measurements,
353
- outerSize,
354
- scrollOffset
355
- });
356
- if (range.startIndex !== this.range.startIndex || range.endIndex !== this.range.endIndex) {
357
- this.range = range;
358
- this.notify();
359
- }
360
- return this.range;
361
- }, {
362
- key: 'calculateRange',
363
- debug: () => this.options.debug
275
+ _this.options = _extends({
276
+ debug: false,
277
+ initialOffset: 0,
278
+ overscan: 1,
279
+ paddingStart: 0,
280
+ paddingEnd: 0,
281
+ scrollPaddingStart: 0,
282
+ scrollPaddingEnd: 0,
283
+ horizontal: false,
284
+ getItemKey: defaultKeyExtractor,
285
+ rangeExtractor: defaultRangeExtractor,
286
+ onChange: function onChange() {},
287
+ measureElement: measureElement,
288
+ initialRect: {
289
+ width: 0,
290
+ height: 0
291
+ },
292
+ scrollMargin: 0,
293
+ scrollingDelay: 150,
294
+ indexAttribute: 'data-index',
295
+ initialMeasurementsCache: []
296
+ }, opts);
297
+ };
298
+ this.notify = function () {
299
+ _this.options.onChange == null ? void 0 : _this.options.onChange(_this);
300
+ };
301
+ this.cleanup = function () {
302
+ _this.unsubs.filter(Boolean).forEach(function (d) {
303
+ return d();
364
304
  });
365
- this.getIndexes = memo(() => [this.options.rangeExtractor, this.range, this.options.overscan, this.options.count], (rangeExtractor, range, overscan, count) => {
366
- return rangeExtractor({
367
- ...range,
368
- overscan,
369
- count: count
370
- });
371
- }, {
372
- key: 'getIndexes',
373
- debug: () => this.options.debug
305
+ _this.unsubs = [];
306
+ _this.scrollElement = null;
307
+ };
308
+ this._didMount = function () {
309
+ var ro = _this.getResizeObserver();
310
+ Object.values(_this.measureElementCache).forEach(function (node) {
311
+ return ro == null ? void 0 : ro.observe(node);
374
312
  });
375
- this.indexFromElement = node => {
376
- const attributeName = this.options.indexAttribute;
377
- const indexStr = node.getAttribute(attributeName);
378
- if (!indexStr) {
379
- console.warn(`Missing attribute name '${attributeName}={index}' on measured element.`);
380
- return -1;
381
- }
382
- return parseInt(indexStr, 10);
313
+ return function () {
314
+ ro == null ? void 0 : ro.disconnect();
315
+ _this.cleanup();
383
316
  };
384
- this._measureElement = (node, _sync) => {
385
- const index = this.indexFromElement(node);
386
- const item = this.measurementsCache[index];
387
- if (!item) {
388
- return;
389
- }
390
- const prevNode = this.measureElementCache[item.key];
391
- const ro = this.getResizeObserver();
392
- if (!node.isConnected) {
393
- if (prevNode) {
394
- ro == null ? void 0 : ro.unobserve(prevNode);
395
- delete this.measureElementCache[item.key];
317
+ };
318
+ this._willUpdate = function () {
319
+ _this.pendingScrollToIndexCallback == null ? void 0 : _this.pendingScrollToIndexCallback();
320
+ var scrollElement = _this.options.getScrollElement();
321
+ if (_this.scrollElement !== scrollElement) {
322
+ _this.cleanup();
323
+ _this.scrollElement = scrollElement;
324
+ _this._scrollToOffset(_this.scrollOffset, {
325
+ adjustments: undefined,
326
+ behavior: undefined
327
+ });
328
+ _this.unsubs.push(_this.options.observeElementRect(_this, function (rect) {
329
+ _this.scrollRect = rect;
330
+ _this.calculateRange();
331
+ }));
332
+ _this.unsubs.push(_this.options.observeElementOffset(_this, function (offset) {
333
+ _this.scrollAdjustments = 0;
334
+ if (_this.scrollOffset === offset) {
335
+ return;
396
336
  }
397
- return;
398
- }
399
- if (!prevNode || prevNode !== node) {
400
- if (prevNode) {
401
- ro == null ? void 0 : ro.unobserve(prevNode);
337
+ if (_this.isScrollingTimeoutId !== null) {
338
+ clearTimeout(_this.isScrollingTimeoutId);
339
+ _this.isScrollingTimeoutId = null;
402
340
  }
403
- this.measureElementCache[item.key] = node;
404
- ro == null ? void 0 : ro.observe(node);
405
- }
406
- const measuredItemSize = this.options.measureElement(node, this);
407
- const itemSize = this.itemMeasurementsCache[item.key] ?? item.size;
408
- const delta = measuredItemSize - itemSize;
409
- if (delta !== 0) {
410
- if (item.start < this.scrollOffset && this.isScrolling) {
411
- if (this.options.debug) {
412
- console.info('correction', delta);
341
+ var onIsScrollingChange = function onIsScrollingChange(isScrolling) {
342
+ if (_this.isScrolling !== isScrolling) {
343
+ _this.isScrolling = isScrolling;
344
+ _this.notify();
413
345
  }
414
- this._scrollToOffset(this.scrollOffset, {
415
- adjustments: this.scrollAdjustments += delta,
416
- behavior: undefined,
417
- sync: false
418
- });
419
- }
420
- this.pendingMeasuredCacheIndexes.push(index);
421
- this.itemMeasurementsCache = {
422
- ...this.itemMeasurementsCache,
423
- [item.key]: measuredItemSize
424
346
  };
425
- this.notify();
426
- }
427
- };
428
- this.measureElement = node => {
429
- if (!node) {
430
- return;
347
+ _this.scrollDirection = _this.scrollOffset < offset ? 'forward' : 'backward';
348
+ _this.scrollOffset = offset;
349
+ _this.calculateRange();
350
+ onIsScrollingChange(true);
351
+ _this.isScrollingTimeoutId = setTimeout(function () {
352
+ _this.isScrollingTimeoutId = null;
353
+ _this.scrollDirection = null;
354
+ onIsScrollingChange(false);
355
+ }, _this.options.scrollingDelay);
356
+ }));
357
+ } else if (!_this.isScrolling) {
358
+ _this.calculateRange();
359
+ }
360
+ };
361
+ this.getSize = function () {
362
+ return _this.scrollRect[_this.options.horizontal ? 'width' : 'height'];
363
+ };
364
+ this.getMeasurements = memo(function () {
365
+ return [_this.options.count, _this.options.paddingStart, _this.options.scrollMargin, _this.options.getItemKey, _this.itemSizeCache];
366
+ }, function (count, paddingStart, scrollMargin, getItemKey, itemSizeCache) {
367
+ var min = _this.pendingMeasuredCacheIndexes.length > 0 ? Math.min.apply(Math, _this.pendingMeasuredCacheIndexes) : 0;
368
+ _this.pendingMeasuredCacheIndexes = [];
369
+ var measurements = _this.measurementsCache.slice(0, min);
370
+ for (var _i2 = min; _i2 < count; _i2++) {
371
+ var key = getItemKey(_i2);
372
+ var measuredSize = itemSizeCache[key];
373
+ var start = measurements[_i2 - 1] ? measurements[_i2 - 1].end : paddingStart + scrollMargin;
374
+ var size = typeof measuredSize === 'number' ? measuredSize : _this.options.estimateSize(_i2);
375
+ var end = start + size;
376
+ measurements[_i2] = {
377
+ index: _i2,
378
+ start: start,
379
+ size: size,
380
+ end: end,
381
+ key: key
382
+ };
383
+ }
384
+ _this.measurementsCache = measurements;
385
+ return measurements;
386
+ }, {
387
+ key: 'getMeasurements',
388
+ debug: function debug() {
389
+ return _this.options.debug;
390
+ }
391
+ });
392
+ this.calculateRange = memo(function () {
393
+ return [_this.getMeasurements(), _this.getSize(), _this.scrollOffset];
394
+ }, function (measurements, outerSize, scrollOffset) {
395
+ var range = calculateRange({
396
+ measurements: measurements,
397
+ outerSize: outerSize,
398
+ scrollOffset: scrollOffset
399
+ });
400
+ if (range.startIndex !== _this.range.startIndex || range.endIndex !== _this.range.endIndex) {
401
+ _this.range = range;
402
+ _this.notify();
403
+ }
404
+ return _this.range;
405
+ }, {
406
+ key: 'calculateRange',
407
+ debug: function debug() {
408
+ return _this.options.debug;
409
+ }
410
+ });
411
+ this.getIndexes = memo(function () {
412
+ return [_this.options.rangeExtractor, _this.range, _this.options.overscan, _this.options.count];
413
+ }, function (rangeExtractor, range, overscan, count) {
414
+ return rangeExtractor(_extends({}, range, {
415
+ overscan: overscan,
416
+ count: count
417
+ }));
418
+ }, {
419
+ key: 'getIndexes',
420
+ debug: function debug() {
421
+ return _this.options.debug;
422
+ }
423
+ });
424
+ this.indexFromElement = function (node) {
425
+ var attributeName = _this.options.indexAttribute;
426
+ var indexStr = node.getAttribute(attributeName);
427
+ if (!indexStr) {
428
+ console.warn("Missing attribute name '" + attributeName + "={index}' on measured element.");
429
+ return -1;
430
+ }
431
+ return parseInt(indexStr, 10);
432
+ };
433
+ this._measureElement = function (node, _sync) {
434
+ var _this$itemSizeCache$i;
435
+ var index = _this.indexFromElement(node);
436
+ var item = _this.measurementsCache[index];
437
+ if (!item) {
438
+ return;
439
+ }
440
+ var prevNode = _this.measureElementCache[item.key];
441
+ var ro = _this.getResizeObserver();
442
+ if (!node.isConnected) {
443
+ if (prevNode) {
444
+ ro == null ? void 0 : ro.unobserve(prevNode);
445
+ delete _this.measureElementCache[item.key];
431
446
  }
432
- this._measureElement(node, true);
433
- };
434
- this.getVirtualItems = memo(() => [this.getIndexes(), this.getMeasurements()], (indexes, measurements) => {
435
- const virtualItems = [];
436
- for (let k = 0, len = indexes.length; k < len; k++) {
437
- const i = indexes[k];
438
- const measurement = measurements[i];
439
- virtualItems.push(measurement);
447
+ return;
448
+ }
449
+ if (!prevNode || prevNode !== node) {
450
+ if (prevNode) {
451
+ ro == null ? void 0 : ro.unobserve(prevNode);
440
452
  }
441
- return virtualItems;
442
- }, {
443
- key: 'getIndexes',
444
- debug: () => this.options.debug
445
- });
446
- this.scrollToOffset = function (toOffset, _temp) {
447
- let {
448
- align = 'start',
449
- behavior
450
- } = _temp === void 0 ? {} : _temp;
451
- const offset = _this.scrollOffset;
452
- const size = _this.getSize();
453
- if (align === 'auto') {
454
- if (toOffset <= offset) {
455
- align = 'start';
456
- } else if (toOffset >= offset + size) {
457
- align = 'end';
458
- } else {
459
- align = 'start';
453
+ _this.measureElementCache[item.key] = node;
454
+ ro == null ? void 0 : ro.observe(node);
455
+ }
456
+ var measuredItemSize = _this.options.measureElement(node, _this);
457
+ var itemSize = (_this$itemSizeCache$i = _this.itemSizeCache[item.key]) != null ? _this$itemSizeCache$i : item.size;
458
+ var delta = measuredItemSize - itemSize;
459
+ if (delta !== 0) {
460
+ var _extends2;
461
+ if (item.start < _this.scrollOffset && _this.isScrolling && _this.scrollDirection === 'backward') {
462
+ if (_this.options.debug) {
463
+ console.info('correction', delta);
460
464
  }
465
+ _this._scrollToOffset(_this.scrollOffset, {
466
+ adjustments: _this.scrollAdjustments += delta,
467
+ behavior: undefined
468
+ });
461
469
  }
462
- const options = {
463
- adjustments: undefined,
464
- behavior,
465
- sync: false
466
- };
467
- if (align === 'start') {
468
- _this._scrollToOffset(toOffset, options);
469
- } else if (align === 'end') {
470
- _this._scrollToOffset(toOffset - size, options);
471
- } else if (align === 'center') {
472
- _this._scrollToOffset(toOffset - size / 2, options);
470
+ _this.pendingMeasuredCacheIndexes.push(index);
471
+ _this.itemSizeCache = _extends({}, _this.itemSizeCache, (_extends2 = {}, _extends2[item.key] = measuredItemSize, _extends2));
472
+ _this.notify();
473
+ }
474
+ };
475
+ this.measureElement = function (node) {
476
+ if (!node) {
477
+ return;
478
+ }
479
+ _this._measureElement(node, true);
480
+ };
481
+ this.getVirtualItems = memo(function () {
482
+ return [_this.getIndexes(), _this.getMeasurements()];
483
+ }, function (indexes, measurements) {
484
+ var virtualItems = [];
485
+ for (var k = 0, len = indexes.length; k < len; k++) {
486
+ var _i3 = indexes[k];
487
+ var measurement = measurements[_i3];
488
+ virtualItems.push(measurement);
489
+ }
490
+ return virtualItems;
491
+ }, {
492
+ key: 'getIndexes',
493
+ debug: function debug() {
494
+ return _this.options.debug;
495
+ }
496
+ });
497
+ this.getOffsetForAlignment = function (toOffset, align) {
498
+ var offset = _this.scrollOffset;
499
+ var size = _this.getSize();
500
+ if (align === 'auto') {
501
+ if (toOffset <= offset) {
502
+ align = 'start';
503
+ } else if (toOffset >= offset + size) {
504
+ align = 'end';
505
+ } else {
506
+ align = 'start';
473
507
  }
508
+ }
509
+ if (align === 'start') {
510
+ return toOffset;
511
+ } else if (align === 'end') {
512
+ return toOffset - size;
513
+ } else if (align === 'center') {
514
+ return toOffset - size / 2;
515
+ }
516
+ return toOffset;
517
+ };
518
+ this.scrollToOffset = function (toOffset, _temp) {
519
+ var _ref4 = _temp === void 0 ? {} : _temp,
520
+ _ref4$align = _ref4.align,
521
+ align = _ref4$align === void 0 ? 'start' : _ref4$align,
522
+ behavior = _ref4.behavior;
523
+ var options = {
524
+ adjustments: undefined,
525
+ behavior: behavior,
526
+ sync: false
474
527
  };
475
- this.scrollToIndex = function (index, _temp2) {
476
- let {
477
- align = 'auto',
478
- ...rest
479
- } = _temp2 === void 0 ? {} : _temp2;
480
- _this.pendingScrollToIndexCallback = null;
481
- const measurements = _this.getMeasurements();
482
- const offset = _this.scrollOffset;
483
- const size = _this.getSize();
484
- const {
485
- count
486
- } = _this.options;
487
- const measurement = measurements[Math.max(0, Math.min(index, count - 1))];
488
- if (!measurement) {
528
+ _this._scrollToOffset(_this.getOffsetForAlignment(toOffset, align), options);
529
+ };
530
+ this.scrollToIndex = function (index, _temp2) {
531
+ var _ref5 = _temp2 === void 0 ? {} : _temp2,
532
+ _ref5$align = _ref5.align,
533
+ align = _ref5$align === void 0 ? 'auto' : _ref5$align,
534
+ behavior = _ref5.behavior;
535
+ _this.pendingScrollToIndexCallback = null;
536
+ var offset = _this.scrollOffset;
537
+ var size = _this.getSize();
538
+ var count = _this.options.count;
539
+ var measurements = _this.getMeasurements();
540
+ var measurement = measurements[Math.max(0, Math.min(index, count - 1))];
541
+ if (!measurement) {
542
+ throw new Error("VirtualItem not found for index = " + index);
543
+ }
544
+ if (align === 'auto') {
545
+ if (measurement.end >= offset + size - _this.options.scrollPaddingEnd) {
546
+ align = 'end';
547
+ } else if (measurement.start <= offset + _this.options.scrollPaddingStart) {
548
+ align = 'start';
549
+ } else {
489
550
  return;
490
551
  }
491
- if (align === 'auto') {
492
- if (measurement.end >= offset + size - _this.options.scrollPaddingEnd) {
493
- align = 'end';
494
- } else if (measurement.start <= offset + _this.options.scrollPaddingStart) {
495
- align = 'start';
496
- } else {
497
- return;
498
- }
499
- }
500
- const toOffset = align === 'end' ? measurement.end + _this.options.scrollPaddingEnd : measurement.start - _this.options.scrollPaddingStart;
501
- _this.scrollToOffset(toOffset, {
502
- align,
503
- ...rest
504
- });
505
- const isDynamic = Object.keys(_this.measureElementCache).length > 0;
506
- if (isDynamic) {
507
- const didSeen = () => typeof _this.itemMeasurementsCache[_this.options.getItemKey(index)] === 'number';
508
- if (!didSeen()) {
509
- _this.pendingScrollToIndexCallback = () => {
510
- if (didSeen()) {
511
- _this.pendingScrollToIndexCallback = null;
512
- _this.scrollToIndex(index, {
513
- align,
514
- ...rest
515
- });
516
- }
517
- };
518
- }
519
- }
520
- };
521
- this.getTotalSize = () => {
522
- var _this$getMeasurements;
523
- return (((_this$getMeasurements = this.getMeasurements()[this.options.count - 1]) == null ? void 0 : _this$getMeasurements.end) || this.options.paddingStart) + this.options.paddingEnd;
524
- };
525
- this._scrollToOffset = (offset, _ref4) => {
526
- let {
527
- adjustments,
528
- behavior,
529
- sync
530
- } = _ref4;
531
- this.options.scrollToFn(offset, {
532
- behavior,
533
- sync,
534
- adjustments
535
- }, this);
552
+ }
553
+ var getOffsetForIndexAndAlignment = function getOffsetForIndexAndAlignment(measurement) {
554
+ var toOffset = align === 'end' ? measurement.end + _this.options.scrollPaddingEnd : measurement.start - _this.options.scrollPaddingStart;
555
+ return _this.getOffsetForAlignment(toOffset, align);
536
556
  };
537
- this.measure = () => {
538
- this.itemMeasurementsCache = {};
539
- this.notify();
557
+ var toOffset = getOffsetForIndexAndAlignment(measurement);
558
+ if (toOffset === offset) {
559
+ return;
560
+ }
561
+ var options = {
562
+ adjustments: undefined,
563
+ behavior: behavior
540
564
  };
541
- this.setOptions(_opts);
542
- this.scrollRect = this.options.initialRect;
543
- this.scrollOffset = this.options.initialOffset;
544
- this.calculateRange();
545
- }
546
- }
547
- const findNearestBinarySearch = (low, high, getCurrentValue, value) => {
565
+ _this._scrollToOffset(toOffset, options);
566
+ var isDynamic = Object.keys(_this.measureElementCache).length > 0;
567
+ if (isDynamic) {
568
+ _this.pendingScrollToIndexCallback = function () {
569
+ _this.scrollToIndex(index, {
570
+ align: align,
571
+ behavior: behavior
572
+ });
573
+ };
574
+ }
575
+ };
576
+ this.scrollBy = function (adjustments, options) {
577
+ _this._scrollToOffset(_this.scrollOffset, {
578
+ adjustments: adjustments,
579
+ behavior: options == null ? void 0 : options.behavior
580
+ });
581
+ };
582
+ this.getTotalSize = function () {
583
+ var _this$getMeasurements;
584
+ return (((_this$getMeasurements = _this.getMeasurements()[_this.options.count - 1]) == null ? void 0 : _this$getMeasurements.end) || _this.options.paddingStart) - _this.options.scrollMargin + _this.options.paddingEnd;
585
+ };
586
+ this._scrollToOffset = function (offset, _ref6) {
587
+ var adjustments = _ref6.adjustments,
588
+ behavior = _ref6.behavior;
589
+ _this.options.scrollToFn(offset, {
590
+ behavior: behavior,
591
+ adjustments: adjustments
592
+ }, _this);
593
+ };
594
+ this.measure = function () {
595
+ _this.itemSizeCache = {};
596
+ _this.notify();
597
+ };
598
+ this.setOptions(_opts);
599
+ this.scrollRect = this.options.initialRect;
600
+ this.scrollOffset = this.options.initialOffset;
601
+ this.measurementsCache = this.options.initialMeasurementsCache;
602
+ this.measurementsCache.forEach(function (item) {
603
+ _this.itemSizeCache[item.key] = item.size;
604
+ });
605
+ this.calculateRange();
606
+ };
607
+ var findNearestBinarySearch = function findNearestBinarySearch(low, high, getCurrentValue, value) {
548
608
  while (low <= high) {
549
- const middle = (low + high) / 2 | 0;
550
- const currentValue = getCurrentValue(middle);
609
+ var middle = (low + high) / 2 | 0;
610
+ var currentValue = getCurrentValue(middle);
551
611
  if (currentValue < value) {
552
612
  low = middle + 1;
553
613
  } else if (currentValue > value) {
@@ -562,56 +622,57 @@
562
622
  return 0;
563
623
  }
564
624
  };
565
- function calculateRange(_ref5) {
566
- let {
567
- measurements,
568
- outerSize,
569
- scrollOffset
570
- } = _ref5;
571
- const count = measurements.length - 1;
572
- const getOffset = index => measurements[index].start;
573
- const startIndex = findNearestBinarySearch(0, count, getOffset, scrollOffset);
574
- let endIndex = startIndex;
625
+ function calculateRange(_ref7) {
626
+ var measurements = _ref7.measurements,
627
+ outerSize = _ref7.outerSize,
628
+ scrollOffset = _ref7.scrollOffset;
629
+ var count = measurements.length - 1;
630
+ var getOffset = function getOffset(index) {
631
+ return measurements[index].start;
632
+ };
633
+ var startIndex = findNearestBinarySearch(0, count, getOffset, scrollOffset);
634
+ var endIndex = startIndex;
575
635
  while (endIndex < count && measurements[endIndex].end < scrollOffset + outerSize) {
576
636
  endIndex++;
577
637
  }
578
638
  return {
579
- startIndex,
580
- endIndex
639
+ startIndex: startIndex,
640
+ endIndex: endIndex
581
641
  };
582
642
  }
583
643
 
584
644
  function createVirtualizerBase(options) {
585
- const virtualizer = new Virtualizer(options);
586
- return store.readable(virtualizer, set => {
587
- const cleanup = virtualizer._didMount();
645
+ var virtualizer = new Virtualizer(options);
646
+ return store.readable(virtualizer, function (set) {
647
+ var cleanup = virtualizer._didMount();
588
648
  virtualizer._willUpdate();
589
- virtualizer.setOptions({
590
- ...options,
591
- onChange: instance => {
649
+ virtualizer.setOptions(_extends$1({}, options, {
650
+ onChange: function onChange(instance) {
592
651
  set(instance);
593
652
  options.onChange == null ? void 0 : options.onChange(instance);
594
653
  }
595
- });
596
- return () => cleanup();
654
+ }));
655
+ return function () {
656
+ return cleanup();
657
+ };
597
658
  });
598
659
  }
599
660
  function createVirtualizer(options) {
600
- return createVirtualizerBase({
661
+ return createVirtualizerBase(_extends$1({
601
662
  observeElementRect: observeElementRect,
602
663
  observeElementOffset: observeElementOffset,
603
- scrollToFn: elementScroll,
604
- ...options
605
- });
664
+ scrollToFn: elementScroll
665
+ }, options));
606
666
  }
607
667
  function createWindowVirtualizer(options) {
608
- return createVirtualizerBase({
609
- getScrollElement: () => typeof window !== 'undefined' ? window : null,
668
+ return createVirtualizerBase(_extends$1({
669
+ getScrollElement: function getScrollElement() {
670
+ return typeof window !== 'undefined' ? window : null;
671
+ },
610
672
  observeElementRect: observeWindowRect,
611
673
  observeElementOffset: observeWindowOffset,
612
- scrollToFn: windowScroll,
613
- ...options
614
- });
674
+ scrollToFn: windowScroll
675
+ }, options));
615
676
  }
616
677
 
617
678
  exports.Virtualizer = Virtualizer;