From 45a8c2d2dca265a017a173489a4f54c1bf662df2 Mon Sep 17 00:00:00 2001 From: inokawa <48897392+inokawa@users.noreply.github.com> Date: Sun, 21 Jan 2024 17:23:48 +0900 Subject: [PATCH] Refactor calculateJump --- src/core/store.ts | 45 +++++++++++++-------------------------------- 1 file changed, 13 insertions(+), 32 deletions(-) diff --git a/src/core/store.ts b/src/core/store.ts index 021fa2c4b..798f2a471 100644 --- a/src/core/store.ts +++ b/src/core/store.ts @@ -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 */ @@ -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); }; @@ -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) {