fix(directives): fix memory leak in v-mousewheel directive
PR Description
🔗 Related Issue
Fixes #[20022]
🤔 Type of Change
- [x] Bug fix (non-breaking change which fixes an issue)
🌟 What's Changed
Fixed memory leak in v-mousewheel directive:
- Added proper cleanup in unmounted hook
- Added handler for directive value updates
- Added TypeScript interface for WheelElement
- Added comprehensive tests
📝 Technical Details
The v-mousewheel directive was not properly cleaning up event listeners, which could lead to memory leaks. This PR:
- Properly stores and removes event listeners
- Handles directive value updates correctly
- Improves type safety with TypeScript interfaces
- Adds tests to prevent regression
🧪 Testing Done
- Added unit tests for mounting, unmounting, and value updates
- Manually tested with example components
- Verified memory cleanup using Chrome DevTools
📋 Checklist
- [x] I have read the contribution guide
- [x] I have added necessary tests
- [x] I am merging to
devbranch - [x] I have added descriptions and linked to relative issues
👋 @cylgdzz111, seems like this is your first time contribution to element-plus.
- Please make sure that you have read our guidelines and code of conduct before making a contribution.
- You can comment with
/label Components:[component_name]to add a label for which component you are working on. - You may join our
for staying tuned.
Hello @cylgdzz111, thank you for contributing to element-plus, please see our guideline to see how to make contribution
Hi, thanks for your contribution.
Could you provide an example with details of the specific problem before and after the modification?
it seems to be duplicated with https://github.com/element-plus/element-plus/pull/20121
Hi, thanks for your contribution.
Could you provide an example with details of the specific problem before and after the modification?
Problem Before Modification
Before the modification, the v-mousewheel directive had a memory leak issue, specifically:
- Event listeners were not removed when components were unmounted
The screenshots you took seem to use element-ui? Can you provide a link to reproduce it?
The screenshots you took seem to use
element-ui? Can you provide a link to reproduce it?
Sorry, I used the wrong picture
Use element-plus official website:https://element-plus.org/zh-CN/component/table.html
Switch back and forth between these two tab
You will discover a memory leak
Can you provide a more accurate, reproducible example through playground?
The memory leak problem mentioned by others before has many influences, such as problems caused by developer tools and memory leaks caused by Vue.
The latest version of Vue has fixed several memory leak issues, but our official website does not use the latest version of Vue.
In the playground, we can switch between different versions of Vue to compare and locate the specific problem. This should give a more accurate result.
@cylgdzz111 我用测试版本 138.0.7204.23 (Official Build) beta (64-bit) 看起来是能正常降下来的。
LGTM.
指令中的 addEventListener 是需要手动移除,不然是可能存在内存泄露的风险
@cylgdzz111 格式化和测试报错,你有时间处理下吗?
🧪 Playground Preview: https://element-plus.run/?pr=20781 Please comment the example via this playground if needed.
