Skip to content

Commit

Permalink
Refactor startOffset in WindowVirtualizer
Browse files Browse the repository at this point in the history
  • Loading branch information
inokawa committed Mar 4, 2024
1 parent a04b6eb commit 73f0066
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
24 changes: 11 additions & 13 deletions src/core/scroller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
ACTION_MANUAL_SCROLL,
SCROLL_IDLE,
ACTION_BEFORE_MANUAL_SMOOTH_SCROLL,
ACTION_START_OFFSET_CHANGE,
} from "./store";
import { ScrollToIndexOpts } from "./types";
import { debounce, timeout, clamp, microtask } from "./utils";
Expand Down Expand Up @@ -42,7 +43,8 @@ const createScrollObserver = (
value: number,
shift: boolean,
isMomentumScrolling: boolean
) => void
) => void,
getStartOffset?: () => number
) => {
const now = Date.now;

Expand Down Expand Up @@ -73,7 +75,11 @@ const createScrollObserver = (
stillMomentumScrolling = true;
}

if (getStartOffset) {
store._update(ACTION_START_OFFSET_CHANGE, getStartOffset());
}
store._update(ACTION_SCROLL, getScrollOffset());

onScrollEnd();
};

Expand Down Expand Up @@ -353,8 +359,6 @@ export const createWindowScroller = (

return {
_observe(container) {
let prevStartOffset = 0;

const scrollOffsetKey = isHorizontal ? "scrollX" : "scrollY";

const document = getCurrentDocument(container);
Expand Down Expand Up @@ -392,24 +396,18 @@ export const createWindowScroller = (
store,
window,
isHorizontal,
() =>
normalizeOffset(window[scrollOffsetKey], isHorizontal) -
(prevStartOffset = calcOffsetToViewport(
container,
documentBody,
isHorizontal
)),
() => normalizeOffset(window[scrollOffsetKey], isHorizontal),
(jump, shift) => {
// TODO support case two window scrollers exist in the same view
if (shift) {
window.scroll({
[isHorizontal ? "left" : "top"]:
store._getScrollOffset() + prevStartOffset + jump,
[isHorizontal ? "left" : "top"]: store._getScrollOffset() + jump,
});
} else {
window.scrollBy(isHorizontal ? jump : 0, isHorizontal ? 0 : jump);
}
}
},
() => calcOffsetToViewport(container, documentBody, isHorizontal)
);
},
_dispose() {
Expand Down
11 changes: 9 additions & 2 deletions src/core/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ export const ACTION_SCROLL = 4;
/** @internal */
export const ACTION_SCROLL_END = 5;
/** @internal */
export const ACTION_MANUAL_SCROLL = 6;
export const ACTION_START_OFFSET_CHANGE = 6;
/** @internal */
export const ACTION_BEFORE_MANUAL_SMOOTH_SCROLL = 7;
export const ACTION_MANUAL_SCROLL = 7;
/** @internal */
export const ACTION_BEFORE_MANUAL_SMOOTH_SCROLL = 8;

type Actions =
| [type: typeof ACTION_ITEM_RESIZE, entries: ItemResize[]]
Expand All @@ -66,6 +68,7 @@ type Actions =
]
| [type: typeof ACTION_SCROLL, offset: number]
| [type: typeof ACTION_SCROLL_END, dummy?: void]
| [type: typeof ACTION_START_OFFSET_CHANGE, offset: number]
| [type: typeof ACTION_MANUAL_SCROLL, dummy?: void]
| [type: typeof ACTION_BEFORE_MANUAL_SMOOTH_SCROLL, offset: number];

Expand Down Expand Up @@ -439,6 +442,10 @@ export const createVirtualStore = (
_frozenRange = null;
break;
}
case ACTION_START_OFFSET_CHANGE: {
startSpacerSize = payload;
break;
}
case ACTION_MANUAL_SCROLL: {
_scrollMode = SCROLL_BY_MANUAL_SCROLL;
break;
Expand Down

0 comments on commit 73f0066

Please sign in to comment.