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

Refactor calculateJump #334

Merged
merged 1 commit into from
Jan 21, 2024
Merged
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
45 changes: 13 additions & 32 deletions src/core/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ import { isIOSWebKit } from "./environment";
import type { CacheSnapshot } from "./types";
import { abs, clamp, max, min } from "./utils";

// Scroll offset and sizes can have sub-pixel value if window.devicePixelRatio has decimal value
const SUBPIXEL_THRESHOLD = 1.5; // 0.5 * 3

/** @internal */
export const SCROLL_IDLE = 0;
/** @internal */
Expand Down Expand Up @@ -125,17 +122,9 @@ export const overscanEndIndex = (
);
};

const calculateJump = (
cache: Cache,
items: readonly ItemResize[],
keepEnd?: boolean
): number => {
const calculateJump = (cache: Cache, items: readonly ItemResize[]): number => {
return items.reduce((acc, [index, size]) => {
const diff = size - getItemSize(cache, index);
if (!keepEnd || diff > 0) {
acc += diff;
}
return acc;
return acc + size - getItemSize(cache, index);
}, 0);
};

Expand Down Expand Up @@ -315,26 +304,18 @@ export const createVirtualStore = (

// Calculate jump
// Should maintain visible position to minimize junks in appearance
let diff = 0;

if (scrollOffset === 0) {
// Do nothing to stick to the start
} else if (scrollOffset > getMaxScrollOffset() - SUBPIXEL_THRESHOLD) {
// Keep end to stick to the end
diff = calculateJump(cache, updated, true);
let diff: number;
if (_scrollMode === SCROLL_BY_PREPENDING) {
// Keep distance from end immediately after prepending
// We can assume jumps occurred on the upper outside
diff = calculateJump(cache, updated);
} else {
if (_scrollMode === SCROLL_BY_PREPENDING) {
// Keep distance from end immediately after prepending
// We can assume jumps occurred on the upper outside
diff = calculateJump(cache, updated);
} else {
// Keep start at mid
const [startIndex] = _prevRange;
diff = calculateJump(
cache,
updated.filter(([index]) => index < startIndex)
);
}
// Keep start at mid
const [startIndex] = _prevRange;
diff = calculateJump(
cache,
updated.filter(([index]) => index < startIndex)
);
}

if (diff) {
Expand Down
Loading