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 jump compensation immediately after prepending #276

Merged
merged 1 commit into from
Dec 6, 2023
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
23 changes: 17 additions & 6 deletions src/core/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export const createVirtualStore = (
let pendingJump = 0;
let _flushedJump = 0;
let _scrollDirection: ScrollDirection = SCROLL_IDLE;
let _prepended = false;
let _isManualScrolling = false;
let _smoothScrollRange: ItemsRange | null = null;
let _prevRange: ItemsRange = [0, initialItemCount];
Expand Down Expand Up @@ -252,6 +253,9 @@ export const createVirtualStore = (
const updated = payload.filter(
([index, size]) => cache._sizes[index] !== size
);
const isJustPrepended = _prepended;
_prepended = false;

// Skip if all items are cached and not updated
if (!updated.length) {
break;
Expand All @@ -267,12 +271,18 @@ export const createVirtualStore = (
// Keep end to stick to the end
diff = calculateJump(cache, updated, true);
} else {
const [startIndex] = _prevRange;
// Keep start at mid
diff = calculateJump(
cache,
updated.filter(([index]) => index < startIndex)
);
if (isJustPrepended) {
// 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)
);
}
}

if (diff) {
Expand Down Expand Up @@ -326,6 +336,7 @@ export const createVirtualStore = (
);
applyJump(isRemove ? -min(shift, distanceToEnd) : shift);

_prepended = !isRemove;
mutated = UPDATE_SCROLL_STATE;
} else {
updateCacheLength(cache as Writeable<Cache>, payload[0]);
Expand Down
15 changes: 0 additions & 15 deletions stories/advanced/Feed.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,10 @@ export const Default: StoryObj = {
const createItems = (num: number) => range(num, createItem);

const [shifting, setShifting] = useState(false);
const [startFetching, setStartFetching] = useState(false);
const [endFetching, setEndFetching] = useState(false);
const fetchItems = async (isStart: boolean = false) => {
setShifting(isStart);

const setFetching = isStart ? setStartFetching : setEndFetching;

setFetching(true);
await delay(1000);
setFetching(false);
};

const ref = useRef<VListHandle>(null);
Expand Down Expand Up @@ -128,16 +122,7 @@ export const Default: StoryObj = {
}
}}
>
{/* // TODO support the case when spinner is at index 0
<Spinner
key="head"
style={startFetching ? undefined : { visibility: "hidden" }}
/> */}
{elements}
<Spinner
key="foot"
style={endFetching ? undefined : { visibility: "hidden" }}
/>
</VList>
);
},
Expand Down
3 changes: 1 addition & 2 deletions stories/basics/VList.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -623,11 +623,10 @@ export const BiDirectionalInfiniteScrolling: StoryObj = {
}
}}
>
{/* // TODO support the case when spinner is at index 0
<Spinner
key="head"
style={startFetching ? undefined : { visibility: "hidden" }}
/> */}
/>
{items}
<Spinner
key="foot"
Expand Down
Loading