-
-
Notifications
You must be signed in to change notification settings - Fork 611
Fix memory leak #1266
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
base: master
Are you sure you want to change the base?
Fix memory leak #1266
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | |||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -141,18 +141,25 @@ const StickyScrollBar: React.ForwardRefRenderFunction<unknown, StickyScrollBarPr | ||||||||||||||||||||||
})); | |||||||||||||||||||||||
|
|||||||||||||||||||||||
React.useEffect(() => { | |||||||||||||||||||||||
document.body.addEventListener(MOUSEUP_EVENT, onMouseUp, false); | |||||||||||||||||||||||
document.body.addEventListener(MOUSEMOVE_EVENT, onMouseMove, false); | |||||||||||||||||||||||
const abortController = new AbortController(); | |||||||||||||||||||||||
document.body.addEventListener(MOUSEUP_EVENT, onMouseUp, { | |||||||||||||||||||||||
capture: false, | |||||||||||||||||||||||
signal: abortController.signal, | |||||||||||||||||||||||
}); | |||||||||||||||||||||||
document.body.addEventListener(MOUSEMOVE_EVENT, onMouseMove, { | |||||||||||||||||||||||
capture: false, | |||||||||||||||||||||||
signal: abortController.signal, | |||||||||||||||||||||||
}); | |||||||||||||||||||||||
checkScrollBarVisible(); | |||||||||||||||||||||||
return () => { | |||||||||||||||||||||||
document.body.removeEventListener(MOUSEUP_EVENT, onMouseUp); | |||||||||||||||||||||||
document.body.removeEventListener(MOUSEMOVE_EVENT, onMouseMove); | |||||||||||||||||||||||
abortController.abort(); | |||||||||||||||||||||||
}; | |||||||||||||||||||||||
}, [scrollBarWidth, isActive]); | |||||||||||||||||||||||
|
|||||||||||||||||||||||
// Loop for scroll event check | |||||||||||||||||||||||
React.useEffect(() => { | |||||||||||||||||||||||
if (!scrollBodyRef.current) return; | |||||||||||||||||||||||
const abortController = new AbortController(); | |||||||||||||||||||||||
|
|||||||||||||||||||||||
const scrollParents: (HTMLElement | SVGElement)[] = []; | |||||||||||||||||||||||
let parent = getDOM(scrollBodyRef.current); | |||||||||||||||||||||||
|
@@ -161,16 +168,27 @@ const StickyScrollBar: React.ForwardRefRenderFunction<unknown, StickyScrollBarPr | ||||||||||||||||||||||
parent = parent.parentElement; | |||||||||||||||||||||||
} | |||||||||||||||||||||||
|
|||||||||||||||||||||||
scrollParents.forEach(p => p.addEventListener(SCROLL_EVENT, checkScrollBarVisible, false)); | |||||||||||||||||||||||
window.addEventListener(RESIZE_EVENT, checkScrollBarVisible, false); | |||||||||||||||||||||||
window.addEventListener(SCROLL_EVENT, checkScrollBarVisible, false); | |||||||||||||||||||||||
container.addEventListener(SCROLL_EVENT, checkScrollBarVisible, false); | |||||||||||||||||||||||
scrollParents.forEach(p => | |||||||||||||||||||||||
p.addEventListener(SCROLL_EVENT, checkScrollBarVisible, { | |||||||||||||||||||||||
capture: false, | |||||||||||||||||||||||
signal: abortController.signal, | |||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The compatibility requirements for using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain@yoyo837 您提出的关于
对于一个广泛使用的组件库来说,这确实可能带来兼容性问题。以下是可能的替代方案:
React.useEffect(() => {
// 添加事件监听
scrollParents.forEach(p =>
p.addEventListener(SCROLL_EVENT, checkScrollBarVisible, false));
window.addEventListener(RESIZE_EVENT, checkScrollBarVisible, false);
window.addEventListener(SCROLL_EVENT, checkScrollBarVisible, false);
container.addEventListener(SCROLL_EVENT, checkScrollBarVisible, false);
// 返回清理函数
return () => {
scrollParents.forEach(p =>
p.removeEventListener(SCROLL_EVENT, checkScrollBarVisible, false));
window.addEventListener(RESIZE_EVENT, checkScrollBarVisible, false);
window.addEventListener(SCROLL_EVENT, checkScrollBarVisible, false);
container.removeEventListener(SCROLL_EVENT, checkScrollBarVisible, false);
};
}, [container]);
React.useEffect(() => {
const supportsAbortController = typeof AbortController === 'function';
const abortController = supportsAbortController ? new AbortController() : null;
const options = supportsAbortController
? { capture: false, signal: abortController.signal }
: false;
scrollParents.forEach(p =>
p.addEventListener(SCROLL_EVENT, checkScrollBarVisible, options));
// ... 添加其他事件监听
return () => {
if (supportsAbortController) {
abortController.abort();
} else {
scrollParents.forEach(p =>
p.removeEventListener(SCROLL_EVENT, checkScrollBarVisible, false));
// ... 移除其他事件监听
}
};
}, [container]); 您认为哪种方案更适合当前的项目需求? 🌐 Web query:
💡 Result: The
In these versions, you can associate an event listener with an However, this functionality is not supported in Safari, Safari on iOS, or Opera for Android. (barker.codes) For browsers that do not support this feature, you can use a polyfill to achieve similar functionality. (barker.codes) Citations:
🌐 Web query:
💡 Result: When Was AbortController Signal Support Added to addEventListener?Support for using an Other browsers followed suit:
Browser Compatibility
Usage Exampleconst controller = new AbortController();
const signal = controller.signal;
element.addEventListener('click', handler, { signal });
// Later, automatically removes the handler:
controller.abort(); Summary
This feature is now broadly available in all major evergreen browsers from early 2021 onwards, making automatic listener cleanup much easier across web applications. Citations:
@yoyo837 您提出的关于
对于需要广泛兼容性的React组件库,这确实可能带来问题。以下是几种替代方案:
React.useEffect(() => {
scrollParents.forEach(p =>
p.addEventListener(SCROLL_EVENT, checkScrollBarVisible, false));
window.addEventListener(RESIZE_EVENT, checkScrollBarVisible, false);
window.addEventListener(SCROLL_EVENT, checkScrollBarVisible, false);
container.addEventListener(SCROLL_EVENT, checkScrollBarVisible, false);
return () => {
scrollParents.forEach(p =>
p.removeEventListener(SCROLL_EVENT, checkScrollBarVisible, false));
window.removeEventListener(RESIZE_EVENT, checkScrollBarVisible, false);
window.removeEventListener(SCROLL_EVENT, checkScrollBarVisible, false);
container.removeEventListener(SCROLL_EVENT, checkScrollBarVisible, false);
};
}, [container]);
React.useEffect(() => {
const supportsAbortController = typeof AbortController === 'function' &&
'signal' in AbortController.prototype;
const abortController = supportsAbortController ? new AbortController() : null;
const options = supportsAbortController
? { capture: false, signal: abortController.signal }
: false;
// 添加事件监听
scrollParents.forEach(p =>
p.addEventListener(SCROLL_EVENT, checkScrollBarVisible, options));
// ...其他事件监听
return () => {
if (supportsAbortController) {
abortController.abort();
} else {
scrollParents.forEach(p =>
p.removeEventListener(SCROLL_EVENT, checkScrollBarVisible, false));
// ...其他事件移除
}
};
}, [container]); 您认为哪种解决方案更适合当前项目的需求和兼容性目标? ✏️ Learnings added
|
|||||||||||||||||||||||
}), | |||||||||||||||||||||||
); | |||||||||||||||||||||||
window.addEventListener(RESIZE_EVENT, checkScrollBarVisible, { | |||||||||||||||||||||||
capture: false, | |||||||||||||||||||||||
signal: abortController.signal, | |||||||||||||||||||||||
}); | |||||||||||||||||||||||
window.addEventListener(SCROLL_EVENT, checkScrollBarVisible, { | |||||||||||||||||||||||
capture: false, | |||||||||||||||||||||||
signal: abortController.signal, | |||||||||||||||||||||||
}); | |||||||||||||||||||||||
container.addEventListener(SCROLL_EVENT, checkScrollBarVisible, { | |||||||||||||||||||||||
capture: false, | |||||||||||||||||||||||
signal: abortController.signal, | |||||||||||||||||||||||
}); | |||||||||||||||||||||||
|
|||||||||||||||||||||||
return () => { | |||||||||||||||||||||||
scrollParents.forEach(p => p.removeEventListener(SCROLL_EVENT, checkScrollBarVisible)); | |||||||||||||||||||||||
window.removeEventListener(RESIZE_EVENT, checkScrollBarVisible); | |||||||||||||||||||||||
window.removeEventListener(SCROLL_EVENT, checkScrollBarVisible); | |||||||||||||||||||||||
container.removeEventListener(SCROLL_EVENT, checkScrollBarVisible); | |||||||||||||||||||||||
abortController.abort(); | |||||||||||||||||||||||
}; | |||||||||||||||||||||||
}, [container]); | |||||||||||||||||||||||
|
|||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before move to signal, seems why
removeEventListener
not working is much important. IfscrollRef.current
is not valid when unmount, we can just simply modify as :There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you dont want to move to
AbortController
it will work as expected. But we have more problems than I thought. InstickyScrollBar.tsx
the handlers (onMouseUp
,onMouseMove
andcheckScrollBarVisible
) are declared inside the body of the component function, that will also lead to memory leak because after each render the reference for this functions will be re-created and the cleanup function will have reference to the first declaration of this functions andremoveEventListener
will not work because of mismatching handler references, so looks like we should wrap them touseCallback
?? But damn, I'm too scaried to do that because we should properly control the dependencies for thisuseEffects
and combine them with dependencies of anotheruseEffect
which also has their own depencies (scrollBarWidth
,isActive
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
onMouseUp
onMouseMove
&checkScrollBarVisible
are correctly handle with closure for each render. Could you provide a minimun reproduce of this?