Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback to default cache if specified cache is shorter/longer #443

Merged
merged 2 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
Fill with default cache
  • Loading branch information
inokawa committed May 7, 2024
commit 18ae9dfff1e8f6c83168395450a2a8e36a74cee0
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
13 changes: 9 additions & 4 deletions src/core/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,16 @@ export const initCache = (
itemSize: number,
snapshot?: InternalCacheSnapshot
): Cache => {
const hasValidCache =
snapshot && snapshot[0] && snapshot[0].length === length;
return {
_defaultItemSize: hasValidCache ? snapshot[1] : itemSize,
_sizes: hasValidCache ? snapshot[0] : fill([], length),
_defaultItemSize: snapshot ? snapshot[1] : itemSize,
_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
4 changes: 2 additions & 2 deletions src/react/Virtualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@
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 must be the same as when you take the cache, otherwise restoration will fail.**
*
* **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 Expand Up @@ -309,7 +309,7 @@
if (!onRangeChangeProp) return;

onRangeChangeProp(startIndex, endIndex);
}, [startIndex, endIndex]);

Check warning on line 312 in src/react/Virtualizer.tsx

View workflow job for this annotation

GitHub Actions / check

React Hook useEffect has a missing dependency: 'onRangeChangeProp'. Either include it or remove the dependency array. If 'onRangeChangeProp' changes too often, find the parent component that defines it and wrap that definition in useCallback

useImperativeHandle(
ref,
Expand All @@ -333,7 +333,7 @@
scrollBy: scroller._scrollBy,
};
},
[]

Check warning on line 336 in src/react/Virtualizer.tsx

View workflow job for this annotation

GitHub Actions / check

React Hook useImperativeHandle has missing dependencies: 'scroller._scrollBy', 'scroller._scrollTo', 'scroller._scrollToIndex', and 'store'. Either include them or remove the dependency array
);

for (let i = overscanedRangeStart, j = overscanedRangeEnd; i <= j; i++) {
Expand Down
2 changes: 1 addition & 1 deletion src/react/WindowVirtualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
/**
* 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 must be the same as when you take the cache, otherwise restoration will fail.**
* **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 Expand Up @@ -215,7 +215,7 @@
if (!onRangeChangeProp) return;

onRangeChangeProp(startIndex, endIndex);
}, [startIndex, endIndex]);

Check warning on line 218 in src/react/WindowVirtualizer.tsx

View workflow job for this annotation

GitHub Actions / check

React Hook useEffect has a missing dependency: 'onRangeChangeProp'. Either include it or remove the dependency array. If 'onRangeChangeProp' changes too often, find the parent component that defines it and wrap that definition in useCallback

useImperativeHandle(
ref,
Expand All @@ -226,7 +226,7 @@
},
};
},
[]

Check warning on line 229 in src/react/WindowVirtualizer.tsx

View workflow job for this annotation

GitHub Actions / check

React Hook useImperativeHandle has a missing dependency: 'store'. Either include it or remove the dependency array
);

for (
Expand Down