Appearance
自定义事件监听管理器
js
class AListener {
constructor() {
this._pool = new Map();
}
once(eventName, callback) {
const callbackList = this._pool.get(eventName);
if ( !callbackList ){
this._pool.set(eventName, new Array())
}
this._pool.get(eventName).push(callback)
}
emit(eventName, commit) {
const callbackList = this._pool.get(eventName);
if (!callbackList) {
return
}
for(const callback of callbackList){
callback(commit);
}
this.removeAll(eventName);
}
// 删除所有的监听器
removeAll(eventName) {
this._pool.delete(eventName)
}
}
export default new AListener();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33