Skip to content

Commit

Permalink
Merge pull request #443 from inokawa/fallback-cache
Browse files Browse the repository at this point in the history
Fallback to default cache if specified cache is shorter/longer
  • Loading branch information
inokawa committed May 7, 2024
2 parents 788375f + 18ae9df commit 2fada92
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 9 deletions.
104 changes: 96 additions & 8 deletions src/core/cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -782,9 +782,52 @@ describe(initCache.name, () => {
`);
});

it("should create cache from snapshot", () => {
expect(initCache(10, 23, [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 123]))
.toMatchInlineSnapshot(`
it("should restore cache from snapshot", () => {
const itemLength = 10;
const cache = initCache(itemLength, 23, [
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
123,
]);
expect(cache).toMatchInlineSnapshot(`
{
"_computedOffsetIndex": -1,
"_defaultItemSize": 123,
"_length": 10,
"_offsets": [
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
],
"_sizes": [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
],
}
`);
expect(cache._length).toBe(itemLength);
expect(cache._sizes.length).toBe(itemLength);
expect(cache._offsets.length).toBe(itemLength);
});

it("should restore cache from snapshot which has shorter length", () => {
const itemLength = 10;
const cache = initCache(itemLength, 23, [[0, 1, 2, 3, 4], 123]);
expect(cache).toMatchInlineSnapshot(`
{
"_computedOffsetIndex": -1,
"_defaultItemSize": 123,
Expand All @@ -807,14 +850,59 @@ describe(initCache.name, () => {
2,
3,
4,
5,
6,
7,
8,
9,
-1,
-1,
-1,
-1,
-1,
],
}
`);
expect(cache._length).toBe(itemLength);
expect(cache._sizes.length).toBe(itemLength);
expect(cache._offsets.length).toBe(itemLength);
});

it("should restore cache from snapshot which has longer length", () => {
const itemLength = 10;
const cache = initCache(itemLength, 23, [
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
123,
]);
expect(cache).toMatchInlineSnapshot(`
{
"_computedOffsetIndex": -1,
"_defaultItemSize": 123,
"_length": 10,
"_offsets": [
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
],
"_sizes": [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
],
}
`);
expect(cache._length).toBe(itemLength);
expect(cache._sizes.length).toBe(itemLength);
expect(cache._offsets.length).toBe(itemLength);
});
});

Expand Down
9 changes: 8 additions & 1 deletion src/core/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,14 @@ export const initCache = (
): Cache => {
return {
_defaultItemSize: snapshot ? snapshot[1] : itemSize,
_sizes: snapshot ? snapshot[0] : fill([], length),
_sizes:
snapshot && snapshot[0]
? // https://github.com/inokawa/virtua/issues/441
fill(
snapshot[0].slice(0, min(length, snapshot[0].length)),
max(0, length - snapshot[0].length)
)
: fill([], length),
_length: length,
_computedOffsetIndex: -1,
_offsets: fill([], length),
Expand Down
2 changes: 2 additions & 0 deletions src/react/Virtualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ export interface VirtualizerProps {
horizontal?: boolean;
/**
* You can restore cache by passing a {@link CacheSnapshot} on mount. This is useful when you want to restore scroll position after navigation. The snapshot can be obtained from {@link VirtualizerHandle.cache}.
*
* **The length of items should be the same as when you take the cache, otherwise restoration may not work as expected.**
*/
cache?: CacheSnapshot;
/**
Expand Down
2 changes: 2 additions & 0 deletions src/react/WindowVirtualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ export interface WindowVirtualizerProps {
horizontal?: boolean;
/**
* You can restore cache by passing a {@link CacheSnapshot} on mount. This is useful when you want to restore scroll position after navigation. The snapshot can be obtained from {@link WindowVirtualizerHandle.cache}.
*
* **The length of items should be the same as when you take the cache, otherwise restoration may not work as expected.**
*/
cache?: CacheSnapshot;
/**
Expand Down

0 comments on commit 2fada92

Please sign in to comment.