-
Notifications
You must be signed in to change notification settings - Fork 9
chore: test cr #14
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
chore: test cr #14
Changes from all 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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,57 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class ResourceManager { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private resources: Map<string, any[]> = new Map(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private eventListeners: { [key: string]: EventListener } = {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| constructor() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 创建大量事件监听器但没有清理机制 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| window.addEventListener('resize', this.handleResize); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| document.addEventListener('click', this.handleClick); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 启动定时器但没有清理 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| setInterval(this.collectData, 1000); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private handleResize = () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 每次窗口大小变化时,都添加新数据到资源中但从不清理 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const newData = new Array(10000).fill(0).map(() => ({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| id: Math.random().toString(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| value: new Array(1000).fill('large string data'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| })); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.resources.set(`resize_${Date.now()}`, newData); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.log('Added new resize data'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+14
to
+23
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. 🛠️ Refactor suggestion Consider bounding the array size or implementing a cleanup policy. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private handleClick = (event: MouseEvent) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 为每个点击创建一个新闭包,捕获点击事件但从不释放 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const clickData = { x: event.clientX, y: event.clientY, timestamp: Date.now() }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const element = document.getElementById('tracking-area'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (element) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 创建新的事件监听器但从不移除旧的 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const listener = () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| console.log('Processing click data:', clickData); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| element.addEventListener('mouseover', listener); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.eventListeners[`click_${Date.now()}`] = listener; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+25
to
+39
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. Prevent stacking multiple event listeners on the same element. Example fix: private handleClick = (event: MouseEvent) => {
const clickData = { x: event.clientX, y: event.clientY, timestamp: Date.now() };
const element = document.getElementById('tracking-area');
if (element) {
- const listener = () => {
+ const handleMouseOver = () => {
console.log('Processing click data:', clickData);
};
- element.addEventListener('mouseover', listener);
- this.eventListeners[\`click_\${Date.now()}\`] = listener;
+ // Remove any existing listener if needed
+ const oldListenerKey = Object.keys(this.eventListeners).find(key => key.startsWith('click_'));
+ if (oldListenerKey) {
+ element.removeEventListener('mouseover', this.eventListeners[oldListenerKey]);
+ delete this.eventListeners[oldListenerKey];
+ }
+ element.addEventListener('mouseover', handleMouseOver);
+ this.eventListeners[`click_${Date.now()}`] = handleMouseOver;
}
};📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private collectData = () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 定期收集数据但从不清理 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const largeData = new Array(5000).fill(0).map(() => new Uint8Array(1024)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.resources.set(`data_${Date.now()}`, largeData); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+41
to
+46
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. 🛠️ Refactor suggestion Consider controlling or cleaning up periodic data collection. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 缺少析构函数或清理方法来移除事件监听器和释放资源 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+47
to
+49
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. Implement a destructor or dispose method to remove event listeners and clear intervals. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 创建实例但从不销毁 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let manager: ResourceManager | null = new ResourceManager(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 模拟单页应用路由变化,但没有清理旧的资源管理器 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function navigateToNewPage() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 创建新的管理器,但没有清理旧的 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| manager = new ResourceManager(); // 旧的manager实例仍然存在,但已经无法访问 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
Add a cleanup mechanism for event listeners and intervals.
The constructor registers event listeners on the
windowanddocumentobjects, and starts asetIntervaltimer. None of these are cleared or removed later, leading to potential memory leaks over time. A best practice is to store the references of these listeners and the timer ID, then expose a cleanup or dispose method to remove all references upon teardown.Here's a sample diff to illustrate a cleanup approach:
class ResourceManager { private resources: Map<string, any[]> = new Map(); private eventListeners: { [key: string]: EventListener } = {}; + private intervalId: number | undefined; constructor() { window.addEventListener('resize', this.handleResize); document.addEventListener('click', this.handleClick); - setInterval(this.collectData, 1000); + this.intervalId = window.setInterval(this.collectData, 1000); } + public dispose() { + window.removeEventListener('resize', this.handleResize); + document.removeEventListener('click', this.handleClick); + if (this.intervalId !== undefined) { + clearInterval(this.intervalId); + this.intervalId = undefined; + } + // Optionally clear stored resources as well + this.resources.clear(); + // Remove all recorded event listeners from internal references if necessary + this.eventListeners = {}; + } }📝 Committable suggestion