Skip to content

Commit

Permalink
Merge pull request #144 from inokawa/refactor-reset-cache
Browse files Browse the repository at this point in the history
Refactor resetCache
  • Loading branch information
inokawa authored Jul 27, 2023
2 parents df11d52 + 3fb25df commit db3d324
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
41 changes: 25 additions & 16 deletions src/core/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,33 +133,42 @@ export const estimateDefaultItemSize = (cache: Writeable<Cache>) => {
median(measuredSizes);
};

export const resetCache = (
length: number,
itemSize: number,
cache?: Cache
): Cache => {
export const initCache = (length: number, itemSize: number): Cache => {
const sizes: number[] = [];
const offsets: number[] = [];
for (let i = 0; i < length; i++) {
const size = cache && cache._sizes[i];
sizes.push(UNCACHED);
// first offset must be 0
offsets.push(i === 0 ? 0 : UNCACHED);
}

return {
_defaultItemSize: itemSize,
_length: length,
_measuredOffsetIndex: 0,
_sizes: sizes,
_offsets: offsets,
};
};

export const updateCache = (cache: Writeable<Cache>, length: number) => {
const sizes: number[] = [];
const offsets: number[] = [];
for (let i = 0; i < length; i++) {
const size = cache._sizes[i];
sizes.push(exists(size) ? size : UNCACHED);

if (i === 0) {
// first offset must be 0
offsets.push(0);
} else {
const offset = cache && cache._offsets[i];
const offset = cache._offsets[i];
offsets.push(exists(offset) ? offset : UNCACHED);
}
}

return {
_defaultItemSize: cache ? cache._defaultItemSize : itemSize,
_length: length,
_measuredOffsetIndex: cache
? min(cache._measuredOffsetIndex, length - 1)
: 0,
_sizes: sizes,
_offsets: offsets,
};
cache._length = length;
cache._measuredOffsetIndex = min(cache._measuredOffsetIndex, length - 1);
cache._sizes = sizes;
cache._offsets = offsets;
};
11 changes: 6 additions & 5 deletions src/core/store.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
findStartIndexWithOffset,
resetCache,
initCache,
getItemSize,
computeTotalSize,
findEndIndex,
Expand All @@ -10,6 +10,7 @@ import {
setItemSize,
hasUnmeasuredItemsInRange,
estimateDefaultItemSize,
updateCache,
} from "./cache";
import type { CacheSnapshot, Writeable } from "./types";
import { abs, exists, max, min } from "./utils";
Expand Down Expand Up @@ -92,13 +93,13 @@ export const createVirtualStore = (
): VirtualStore => {
const shouldAutoEstimateItemSize = !itemSize;
const initialItemSize = itemSize || 40;
const cache =
(cacheSnapshot as unknown as Cache | undefined) ??
initCache(elementsCount, initialItemSize);
let viewportSize = initialItemSize * max(initialItemCount - 1, 0);
let scrollOffset = 0;
let jumpCount = 0;
let jump: ScrollJump = 0;
let cache =
(cacheSnapshot as unknown as Cache | undefined) ??
resetCache(elementsCount, initialItemSize);
let _scrollDirection: ScrollDirection = SCROLL_IDLE;
let _isManualScrolling = false;
let _resized = false;
Expand Down Expand Up @@ -341,7 +342,7 @@ export const createVirtualStore = (
_updateCacheLength(length) {
// It's ok to be updated in render because states should be calculated consistently regardless cache length
if (cache._length === length) return;
cache = resetCache(length, initialItemSize, cache);
updateCache(cache as Writeable<Cache>, length);
},
};
};

0 comments on commit db3d324

Please sign in to comment.