Skip to content

Commit 06ea4fb

Browse files
committed
chore: test cr
1 parent 715a092 commit 06ea4fb

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
class ResourceManager {
2+
private resources: Map<string, any[]> = new Map();
3+
private eventListeners: { [key: string]: EventListener } = {};
4+
5+
constructor() {
6+
// 创建大量事件监听器但没有清理机制
7+
window.addEventListener('resize', this.handleResize);
8+
document.addEventListener('click', this.handleClick);
9+
10+
// 启动定时器但没有清理
11+
setInterval(this.collectData, 1000);
12+
}
13+
14+
private handleResize = () => {
15+
// 每次窗口大小变化时,都添加新数据到资源中但从不清理
16+
const newData = new Array(10000).fill(0).map(() => ({
17+
id: Math.random().toString(),
18+
value: new Array(1000).fill('large string data'),
19+
}));
20+
21+
this.resources.set(`resize_${Date.now()}`, newData);
22+
console.log('Added new resize data');
23+
};
24+
25+
private handleClick = (event: MouseEvent) => {
26+
// 为每个点击创建一个新闭包,捕获点击事件但从不释放
27+
const clickData = { x: event.clientX, y: event.clientY, timestamp: Date.now() };
28+
29+
const element = document.getElementById('tracking-area');
30+
if (element) {
31+
// 创建新的事件监听器但从不移除旧的
32+
const listener = () => {
33+
console.log('Processing click data:', clickData);
34+
};
35+
36+
element.addEventListener('mouseover', listener);
37+
this.eventListeners[`click_${Date.now()}`] = listener;
38+
}
39+
};
40+
41+
private collectData = () => {
42+
// 定期收集数据但从不清理
43+
const largeData = new Array(5000).fill(0).map(() => new Uint8Array(1024));
44+
this.resources.set(`data_${Date.now()}`, largeData);
45+
};
46+
47+
// 缺少析构函数或清理方法来移除事件监听器和释放资源
48+
}
49+
50+
// 创建实例但从不销毁
51+
let manager: ResourceManager | null = new ResourceManager();
52+
53+
// 模拟单页应用路由变化,但没有清理旧的资源管理器
54+
function navigateToNewPage() {
55+
// 创建新的管理器,但没有清理旧的
56+
manager = new ResourceManager(); // 旧的manager实例仍然存在,但已经无法访问
57+
}

0 commit comments

Comments
 (0)