Skip to content

Conversation

bbb169
Copy link

@bbb169 bbb169 commented Sep 5, 2023

因为是cacheHeight影响了scrollHeight,导致useScrollTo这个hooks触发的三次scroll的offesetTop不同,所以我的思路是,当要加载远距离的节点并且该节点未访问时,先在当前的节点下预加载远距离节点,但不滚动,等远距离节点的缓存高度拿到后,再真正加载远距离节点和滚动
image
image

close: ant-design/ant-design#44100

@vercel
Copy link

vercel bot commented Sep 5, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
virtual-list ❌ Failed (Inspect) Sep 19, 2023 9:48am

@zombieJ
Copy link
Member

zombieJ commented Sep 6, 2023

大致逻辑看了一下,可以优化一下成状态机:

  • scroll hook 里中添加 state:
    • 状态:STABLE、MEASURE
    • 目标范围 start ~ end
  • 在触发 scrollTo 的时候,计算 index 并且切换至 MEASURE
  • 将 start & end useChildren 做额外渲染
  • scroll hook 的 useLayoutEffect 中,检查是否为 MEASURE 且 start ~ end 是否 ready
    • ready 时 syncTop 并且设置回 STABLE

@zombieJ
Copy link
Member

zombieJ commented Sep 12, 2023

CI 挂了

@bbb169
Copy link
Author

bbb169 commented Sep 13, 2023

image

@codecov
Copy link

codecov bot commented Sep 13, 2023

Codecov Report

Merging #226 (10a4eb8) into master (e578bda) will increase coverage by 0.02%.
The diff coverage is 100.00%.

❗ Current head 10a4eb8 differs from pull request most recent head 9ced492. Consider uploading reports for the commit 9ced492 to get more accurate results

@@            Coverage Diff             @@
##           master     #226      +/-   ##
==========================================
+ Coverage   99.51%   99.53%   +0.02%     
==========================================
  Files          17       19       +2     
  Lines         613      645      +32     
  Branches      128      137       +9     
==========================================
+ Hits          610      642      +32     
  Misses          3        3              
Files Changed Coverage Δ
src/utils/renderChildren.tsx 100.00% <ø> (ø)
src/List.tsx 99.42% <100.00%> (-0.05%) ⬇️
src/hooks/useCalcPosition.ts 100.00% <100.00%> (ø)
src/hooks/useScrollTo.tsx 100.00% <100.00%> (ø)
src/utils/scrollToCacheState.ts 100.00% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

// make `collectHeight` work
offsetParent: {
get: () => true,
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

诶?这里只是修了 test case。但是没有给新的代码加 test case~

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok 我加一下

};
// check cacheHeiht before calculating
if (!checkIndexIsCache()) {
setScrollToCacheState(MEASURE);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里应该算出需要知道的范围后直接短路

syncScrollTop(targetTop);
unstable_batchedUpdates(() => {
if (startIndex !== undefined ? checkIndexIsCache(startIndex) : checkIndexIsCache()) {
setScrollToCacheState(STABLE);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同理,STABLE 不应该是计算时使用。应该是 useLayoutEffect 里更新状态

if (times < 0 || !containerRef.current) return;

// ================== switch cacheState ===================
if (times === 3) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

等等,这里还有 times?那是不是直接 times 保持 sync 就好了?我试试去

@bbb169 bbb169 closed this Sep 20, 2023
@yoyo837
Copy link
Member

yoyo837 commented Sep 20, 2023

Move to #232

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants