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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fallback to default cache if specified cache is invalid
  • Loading branch information
inokawa committed May 7, 2024
commit 6d25f950c1f26d8d1c594b07f1ef60ed23637fd9
6 changes: 4 additions & 2 deletions src/core/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,11 @@ export const initCache = (
itemSize: number,
snapshot?: InternalCacheSnapshot
): Cache => {
const hasValidCache =
snapshot && snapshot[0] && snapshot[0].length === length;
return {
_defaultItemSize: snapshot ? snapshot[1] : itemSize,
_sizes: snapshot ? snapshot[0] : fill([], length),
_defaultItemSize: hasValidCache ? snapshot[1] : itemSize,
_sizes: hasValidCache ? snapshot[0] : 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 @@
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.**
*/
cache?: CacheSnapshot;
/**
Expand Down Expand Up @@ -307,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 @@ -331,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: 2 additions & 0 deletions src/react/WindowVirtualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,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 WindowVirtualizerHandle.cache}.
*
* **The length of items must be the same as when you take the cache, otherwise restoration will fail.**
*/
cache?: CacheSnapshot;
/**
Expand Down Expand Up @@ -213,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 @@ -224,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
Loading