virtua 0.46.2 → 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,
@@ -417,6 +410,10 @@ const createVirtualStore = (elementsCount, itemSize = 40, ssrCount = 0, cacheSna
417
410
  let mutated = 0;
418
411
  switch (type) {
419
412
  case ACTION_SCROLL: {
413
+ if (payload === scrollOffset && _scrollMode === SCROLL_BY_NATIVE) {
414
+ // Ignore scroll events from different direction
415
+ break;
416
+ }
420
417
  const flushedJump = _flushedJump;
421
418
  _flushedJump = 0;
422
419
  const delta = payload - scrollOffset;
@@ -485,13 +482,12 @@ const createVirtualStore = (elementsCount, itemSize = 40, ssrCount = 0, cacheSna
485
482
  // https://github.com/inokawa/virtua/issues/758
486
483
  index < _frozenRange[0]
487
484
  : // Otherwise we should maintain visible position
488
- getItemOffset(index) +
485
+ getItemOffset$1(index +
489
486
  // https://github.com/inokawa/virtua/issues/385
490
487
  (_scrollDirection === SCROLL_IDLE &&
491
488
  _scrollMode === SCROLL_BY_NATIVE
492
- ? getItemSize$1(index)
493
- : 0) <
494
- getRelativeScrollOffset())) {
489
+ ? 1
490
+ : 0)) < getRelativeScrollOffset())) {
495
491
  acc += size - getItemSize$1(index);
496
492
  }
497
493
  return acc;
@@ -1313,14 +1309,14 @@ const Virtualizer = (props) => {
1313
1309
  const resizer = createResizer(store, horizontal);
1314
1310
  const scroller = createScroller(store, horizontal);
1315
1311
  const [stateVersion, setRerender] = createSignal(store.$getStateVersion());
1316
- const unsubscribeStore = store.$subscribe(UPDATE_VIRTUAL_STATE, () => {
1312
+ store.$subscribe(UPDATE_VIRTUAL_STATE, () => {
1317
1313
  setRerender(store.$getStateVersion());
1318
1314
  });
1319
- const unsubscribeOnScroll = store.$subscribe(UPDATE_SCROLL_EVENT, () => {
1315
+ store.$subscribe(UPDATE_SCROLL_EVENT, () => {
1320
1316
  var _a;
1321
1317
  (_a = props.onScroll) === null || _a === void 0 ? void 0 : _a.call(props, store.$getScrollOffset());
1322
1318
  });
1323
- const unsubscribeOnScrollEnd = store.$subscribe(UPDATE_SCROLL_END_EVENT, () => {
1319
+ store.$subscribe(UPDATE_SCROLL_END_EVENT, () => {
1324
1320
  var _a;
1325
1321
  (_a = props.onScrollEnd) === null || _a === void 0 ? void 0 : _a.call(props);
1326
1322
  });
@@ -1365,9 +1361,7 @@ const Virtualizer = (props) => {
1365
1361
  if (props.ref) {
1366
1362
  props.ref();
1367
1363
  }
1368
- unsubscribeStore();
1369
- unsubscribeOnScroll();
1370
- unsubscribeOnScrollEnd();
1364
+ store.$dispose();
1371
1365
  resizer.$dispose();
1372
1366
  scroller.$dispose();
1373
1367
  });
@@ -1424,7 +1418,6 @@ const Virtualizer = (props) => {
1424
1418
  return (<Dynamic component={props.as} ref={containerRef} style={{
1425
1419
  contain: "size paint style", // https://github.com/inokawa/virtua/pull/775
1426
1420
  "overflow-anchor": "none", // opt out browser's scroll anchoring because it will conflict to scroll anchoring of virtualizer
1427
- overflow: "clip", // https://github.com/inokawa/virtua/pull/485 https://github.com/inokawa/virtua/issues/717
1428
1421
  flex: "none", // flex style can break layout
1429
1422
  position: "relative",
1430
1423
  width: horizontal ? totalSize() + "px" : "100%",
@@ -1490,15 +1483,15 @@ const WindowVirtualizer = (props) => {
1490
1483
  const resizer = createWindowResizer(store, horizontal);
1491
1484
  const scroller = createWindowScroller(store, horizontal);
1492
1485
  const [stateVersion, setRerender] = createSignal(store.$getStateVersion());
1493
- const unsubscribeStore = store.$subscribe(UPDATE_VIRTUAL_STATE, () => {
1486
+ store.$subscribe(UPDATE_VIRTUAL_STATE, () => {
1494
1487
  setRerender(store.$getStateVersion());
1495
1488
  });
1496
- const unsubscribeOnScroll = store.$subscribe(UPDATE_SCROLL_EVENT, () => {
1489
+ store.$subscribe(UPDATE_SCROLL_EVENT, () => {
1497
1490
  var _a;
1498
1491
  // https://github.com/inokawa/virtua/discussions/580
1499
1492
  (_a = props.onScroll) === null || _a === void 0 ? void 0 : _a.call(props);
1500
1493
  });
1501
- const unsubscribeOnScrollEnd = store.$subscribe(UPDATE_SCROLL_END_EVENT, () => {
1494
+ store.$subscribe(UPDATE_SCROLL_END_EVENT, () => {
1502
1495
  var _a;
1503
1496
  (_a = props.onScrollEnd) === null || _a === void 0 ? void 0 : _a.call(props);
1504
1497
  });
@@ -1529,9 +1522,7 @@ const WindowVirtualizer = (props) => {
1529
1522
  if (props.ref) {
1530
1523
  props.ref();
1531
1524
  }
1532
- unsubscribeStore();
1533
- unsubscribeOnScroll();
1534
- unsubscribeOnScrollEnd();
1525
+ store.$dispose();
1535
1526
  resizer.$dispose();
1536
1527
  scroller.$dispose();
1537
1528
  });
@@ -1555,7 +1546,6 @@ const WindowVirtualizer = (props) => {
1555
1546
  return (<div ref={containerRef} style={{
1556
1547
  contain: "size paint style", // https://github.com/inokawa/virtua/pull/775
1557
1548
  "overflow-anchor": "none", // opt out browser's scroll anchoring because it will conflict to scroll anchoring of virtualizer
1558
- overflow: "clip", // https://github.com/inokawa/virtua/pull/485 https://github.com/inokawa/virtua/issues/717
1559
1549
  flex: "none", // flex style can break layout
1560
1550
  position: "relative",
1561
1551
  width: horizontal ? totalSize() + "px" : "100%",