Skip to content

Commit

Permalink
refactor: destroy previous resize observer from its callback
Browse files Browse the repository at this point in the history
  • Loading branch information
wellyshen committed Jun 5, 2021
1 parent 2254451 commit 5d44bf0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-bugs-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-cool-virtual": patch
---

refactor: destroy previous resize observer from its callback
16 changes: 6 additions & 10 deletions src/useVirtual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,8 @@ export default <
DEBOUNCE_INTERVAL
);

const [resetOthers, cancelResetOthers] = useDebounce(() => {
const [resetUserScroll, cancelResetUserScroll] = useDebounce(() => {
userScrollRef.current = true;

const len = rosRef.current.size - items.length;
const iter = rosRef.current[Symbol.iterator]();
for (let i = 0; i < len; i += 1)
rosRef.current.delete(iter.next().value[0]);
}, DEBOUNCE_INTERVAL);

const handleScroll = useCallback(
Expand Down Expand Up @@ -332,6 +327,7 @@ export default <

if (!measuredSize) {
ro.disconnect();
rosRef.current.delete(target);
return;
}

Expand Down Expand Up @@ -394,7 +390,7 @@ export default <
prevVStopRef.current = vStop;

if (uxScrolling) resetIsScrolling();
resetOthers();
if (!userScrollRef.current) resetUserScroll();
},
[
getCalcData,
Expand All @@ -405,7 +401,7 @@ export default <
marginKey,
onScrollRef,
resetIsScrolling,
resetOthers,
resetUserScroll,
scrollTo,
sizeKey,
]
Expand Down Expand Up @@ -461,7 +457,7 @@ export default <

return () => {
cancelResetIsScrolling();
cancelResetOthers();
cancelResetUserScroll();
if (scrollToRafRef.current) {
cancelAnimationFrame(scrollToRafRef.current);
scrollToRafRef.current = undefined;
Expand All @@ -474,7 +470,7 @@ export default <
};
}, [
cancelResetIsScrolling,
cancelResetOthers,
cancelResetUserScroll,
handleScroll,
scrollKey,
useIsScrollingRef,
Expand Down

0 comments on commit 5d44bf0

Please sign in to comment.