virtua 0.46.3 → 0.46.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.
@@ -76,7 +76,7 @@ const setItemSize = (cache, index, size) => {
76
76
  /**
77
77
  * @internal
78
78
  */
79
- const computeOffset = (cache, index) => {
79
+ const getItemOffset = (cache, index) => {
80
80
  if (!cache._length)
81
81
  return 0;
82
82
  if (cache._computedOffsetIndex >= index) {
@@ -98,15 +98,6 @@ const computeOffset = (cache, index) => {
98
98
  cache._computedOffsetIndex = index;
99
99
  return top;
100
100
  };
101
- /**
102
- * @internal
103
- */
104
- const computeTotalSize = (cache) => {
105
- if (!cache._length)
106
- return 0;
107
- return (computeOffset(cache, cache._length - 1) +
108
- getItemSize(cache, cache._length - 1));
109
- };
110
101
  /**
111
102
  * Finds the index of an item in the cache whose computed offset is closest to the specified offset.
112
103
  *
@@ -116,9 +107,8 @@ const findIndex = (cache, offset, low = 0, high = cache._length - 1) => {
116
107
  // Find with binary search
117
108
  while (low <= high) {
118
109
  const mid = floor((low + high) / 2);
119
- const itemOffset = computeOffset(cache, mid);
120
- if (itemOffset <= offset) {
121
- if (itemOffset + getItemSize(cache, mid) > offset) {
110
+ if (getItemOffset(cache, mid) <= offset) {
111
+ if (getItemOffset(cache, mid + 1) > offset) {
122
112
  return mid;
123
113
  }
124
114
  low = mid + 1;
@@ -135,7 +125,7 @@ const findIndex = (cache, offset, low = 0, high = cache._length - 1) => {
135
125
  const computeRange = (cache, startOffset, endOffset, prevStartIndex) => {
136
126
  // Clamp because prevStartIndex may exceed the limit when children decreased a lot after scrolling
137
127
  prevStartIndex = min(prevStartIndex, cache._length - 1);
138
- if (computeOffset(cache, prevStartIndex) <= startOffset) {
128
+ if (getItemOffset(cache, prevStartIndex) <= startOffset) {
139
129
  // search forward
140
130
  // start <= end, prevStartIndex <= start
141
131
  const end = findIndex(cache, endOffset, prevStartIndex);
@@ -187,7 +177,7 @@ const initCache = (length, itemSize, snapshot) => {
187
177
  : fill([], length),
188
178
  _length: length,
189
179
  _computedOffsetIndex: -1,
190
- _offsets: fill([], length),
180
+ _offsets: fill([], length + 1),
191
181
  };
192
182
  };
193
183
  /**
@@ -323,9 +313,9 @@ const createVirtualStore = (elementsCount, itemSize = 40, ssrCount = 0, cacheSna
323
313
  const getRange = (startOffset, endOffset) => {
324
314
  return computeRange(cache, startOffset, endOffset, _prevRange[0]);
325
315
  };
326
- const getTotalSize = () => computeTotalSize(cache);
327
- const getItemOffset = (index) => {
328
- return computeOffset(cache, index) - pendingJump;
316
+ const getTotalSize = () => getItemOffset(cache, cache._length);
317
+ const getItemOffset$1 = (index) => {
318
+ return getItemOffset(cache, index) - pendingJump;
329
319
  };
330
320
  const getItemSize$1 = (index) => {
331
321
  return getItemSize(cache, index);
@@ -349,6 +339,9 @@ const createVirtualStore = (elementsCount, itemSize = 40, ssrCount = 0, cacheSna
349
339
  }
350
340
  };
351
341
  return {
342
+ $dispose: () => {
343
+ subscribers.clear();
344
+ },
352
345
  $getStateVersion: () => stateVersion,
353
346
  $getCacheSnapshot: () => {
354
347
  return takeCacheSnapshot(cache);
@@ -368,11 +361,11 @@ const createVirtualStore = (elementsCount, itemSize = 40, ssrCount = 0, cacheSna
368
361
  [startIndex, endIndex] = _prevRange;
369
362
  }
370
363
  else {
371
- bufferSize = max(0, bufferSize);
372
364
  let startOffset = max(0, getVisibleOffset());
373
365
  let endOffset = startOffset + viewportSize;
374
366
  // For faster initial render pass, returns without buffer if measurement seems to be in progress.
375
367
  if (!shouldAutoEstimateItemSize) {
368
+ bufferSize = max(0, bufferSize);
376
369
  if (_scrollDirection !== SCROLL_DOWN) {
377
370
  startOffset -= bufferSize;
378
371
  }
@@ -391,7 +384,7 @@ const createVirtualStore = (elementsCount, itemSize = 40, ssrCount = 0, cacheSna
391
384
  $findStartIndex: () => findIndex(cache, getVisibleOffset()),
392
385
  $findEndIndex: () => findIndex(cache, getVisibleOffset() + viewportSize),
393
386
  $isUnmeasuredItem: isSizeEqual,
394
- $getItemOffset: getItemOffset,
387
+ $getItemOffset: getItemOffset$1,
395
388
  $getItemSize: getItemSize$1,
396
389
  $getItemsLength: () => cache._length,
397
390
  $getScrollOffset: () => scrollOffset,
@@ -489,13 +482,12 @@ const createVirtualStore = (elementsCount, itemSize = 40, ssrCount = 0, cacheSna
489
482
  // https://github.com/inokawa/virtua/issues/758
490
483
  index < _frozenRange[0]
491
484
  : // Otherwise we should maintain visible position
492
- getItemOffset(index) +
485
+ getItemOffset$1(index +
493
486
  // https://github.com/inokawa/virtua/issues/385
494
487
  (_scrollDirection === SCROLL_IDLE &&
495
488
  _scrollMode === SCROLL_BY_NATIVE
496
- ? getItemSize$1(index)
497
- : 0) <
498
- getRelativeScrollOffset())) {
489
+ ? 1
490
+ : 0)) < getRelativeScrollOffset())) {
499
491
  acc += size - getItemSize$1(index);
500
492
  }
501
493
  return acc;
@@ -1317,14 +1309,14 @@ const Virtualizer = (props) => {
1317
1309
  const resizer = createResizer(store, horizontal);
1318
1310
  const scroller = createScroller(store, horizontal);
1319
1311
  const [stateVersion, setRerender] = createSignal(store.$getStateVersion());
1320
- const unsubscribeStore = store.$subscribe(UPDATE_VIRTUAL_STATE, () => {
1312
+ store.$subscribe(UPDATE_VIRTUAL_STATE, () => {
1321
1313
  setRerender(store.$getStateVersion());
1322
1314
  });
1323
- const unsubscribeOnScroll = store.$subscribe(UPDATE_SCROLL_EVENT, () => {
1315
+ store.$subscribe(UPDATE_SCROLL_EVENT, () => {
1324
1316
  var _a;
1325
1317
  (_a = props.onScroll) === null || _a === void 0 ? void 0 : _a.call(props, store.$getScrollOffset());
1326
1318
  });
1327
- const unsubscribeOnScrollEnd = store.$subscribe(UPDATE_SCROLL_END_EVENT, () => {
1319
+ store.$subscribe(UPDATE_SCROLL_END_EVENT, () => {
1328
1320
  var _a;
1329
1321
  (_a = props.onScrollEnd) === null || _a === void 0 ? void 0 : _a.call(props);
1330
1322
  });
@@ -1369,9 +1361,7 @@ const Virtualizer = (props) => {
1369
1361
  if (props.ref) {
1370
1362
  props.ref();
1371
1363
  }
1372
- unsubscribeStore();
1373
- unsubscribeOnScroll();
1374
- unsubscribeOnScrollEnd();
1364
+ store.$dispose();
1375
1365
  resizer.$dispose();
1376
1366
  scroller.$dispose();
1377
1367
  });
@@ -1428,7 +1418,6 @@ const Virtualizer = (props) => {
1428
1418
  return (<Dynamic component={props.as} ref={containerRef} style={{
1429
1419
  contain: "size paint style", // https://github.com/inokawa/virtua/pull/775
1430
1420
  "overflow-anchor": "none", // opt out browser's scroll anchoring because it will conflict to scroll anchoring of virtualizer
1431
- overflow: "clip", // https://github.com/inokawa/virtua/pull/485 https://github.com/inokawa/virtua/issues/717
1432
1421
  flex: "none", // flex style can break layout
1433
1422
  position: "relative",
1434
1423
  width: horizontal ? totalSize() + "px" : "100%",
@@ -1494,15 +1483,15 @@ const WindowVirtualizer = (props) => {
1494
1483
  const resizer = createWindowResizer(store, horizontal);
1495
1484
  const scroller = createWindowScroller(store, horizontal);
1496
1485
  const [stateVersion, setRerender] = createSignal(store.$getStateVersion());
1497
- const unsubscribeStore = store.$subscribe(UPDATE_VIRTUAL_STATE, () => {
1486
+ store.$subscribe(UPDATE_VIRTUAL_STATE, () => {
1498
1487
  setRerender(store.$getStateVersion());
1499
1488
  });
1500
- const unsubscribeOnScroll = store.$subscribe(UPDATE_SCROLL_EVENT, () => {
1489
+ store.$subscribe(UPDATE_SCROLL_EVENT, () => {
1501
1490
  var _a;
1502
1491
  // https://github.com/inokawa/virtua/discussions/580
1503
1492
  (_a = props.onScroll) === null || _a === void 0 ? void 0 : _a.call(props);
1504
1493
  });
1505
- const unsubscribeOnScrollEnd = store.$subscribe(UPDATE_SCROLL_END_EVENT, () => {
1494
+ store.$subscribe(UPDATE_SCROLL_END_EVENT, () => {
1506
1495
  var _a;
1507
1496
  (_a = props.onScrollEnd) === null || _a === void 0 ? void 0 : _a.call(props);
1508
1497
  });
@@ -1533,9 +1522,7 @@ const WindowVirtualizer = (props) => {
1533
1522
  if (props.ref) {
1534
1523
  props.ref();
1535
1524
  }
1536
- unsubscribeStore();
1537
- unsubscribeOnScroll();
1538
- unsubscribeOnScrollEnd();
1525
+ store.$dispose();
1539
1526
  resizer.$dispose();
1540
1527
  scroller.$dispose();
1541
1528
  });
@@ -1559,7 +1546,6 @@ const WindowVirtualizer = (props) => {
1559
1546
  return (<div ref={containerRef} style={{
1560
1547
  contain: "size paint style", // https://github.com/inokawa/virtua/pull/775
1561
1548
  "overflow-anchor": "none", // opt out browser's scroll anchoring because it will conflict to scroll anchoring of virtualizer
1562
- overflow: "clip", // https://github.com/inokawa/virtua/pull/485 https://github.com/inokawa/virtua/issues/717
1563
1549
  flex: "none", // flex style can break layout
1564
1550
  position: "relative",
1565
1551
  width: horizontal ? totalSize() + "px" : "100%",