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

Improve timeout handling on imperative scroll when document is inactive #471

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 9 additions & 3 deletions src/core/scroller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,19 @@ export const createScroller = (
const waitForMeasurement = (): [Promise<void>, () => void] => {
// Wait for the scroll destination items to be measured.
// The measurement will be done asynchronously and the timing is not predictable so we use promise.
// For example, ResizeObserver may not fire when window is not visible.
let queue: (() => void) | undefined;
return [
new Promise<void>((resolve, reject) => {
queue = resolve;
// Reject when items around scroll destination completely measured
timeout((cancelScroll = reject), 150);
cancelScroll = reject;

// Resize event may not happen when the window/tab is not visible, or during browser back in Safari.
// We have to wait for the initial measurement to avoid failing imperative scroll on mount.
// https://github.com/inokawa/virtua/issues/450
if (store._isInitialMeasurementDone()) {
// Reject when items around scroll destination completely measured
timeout(reject, 150);
}
}),
store._subscribe(UPDATE_SIZE_EVENT, () => {
queue && queue();
Expand Down
4 changes: 4 additions & 0 deletions src/core/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export type VirtualStore = {
_getCacheSnapshot(): CacheSnapshot;
_getRange(): ItemsRange;
_isUnmeasuredItem(index: number): boolean;
_isInitialMeasurementDone(): boolean;
_hasUnmeasuredItemsInFrozenRange(): boolean;
_getItemOffset(index: number): number;
_getItemSize(index: number): number;
Expand Down Expand Up @@ -216,6 +217,9 @@ export const createVirtualStore = (
_isUnmeasuredItem(index) {
return cache._sizes[index] === UNCACHED;
},
_isInitialMeasurementDone() {
return !!viewportSize;
},
_hasUnmeasuredItemsInFrozenRange() {
if (!_frozenRange) return false;
return cache._sizes
Expand Down
Loading