Skip to content

Commit

Permalink
Merge pull request #334 from inokawa/refactor-calculate-jump
Browse files Browse the repository at this point in the history
Refactor calculateJump
  • Loading branch information
inokawa authored Jan 21, 2024
2 parents ec679a0 + 45a8c2d commit e166a4f
Showing 1 changed file with 13 additions and 32 deletions.
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

0 comments on commit e166a4f

Please sign in to comment.