- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent-bus.ts
135 lines (112 loc) · 3.38 KB
/
event-bus.ts
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* @description eventBus
* @author tangc1
* @date 2022-04-23 15:54:41
*/
// 通过isOnce区分on和once事件
exportclassEventBus1{
/**
* {
* 'key1':[
* { fn: fn1, isOnce: false },
* { fn: fn2, isOnce: false },
* { fn: fn3, isOnce: true }
* ],
* 'key2':[],
* 'key3':[]
* }
*/
privateevents: {
[key: string]: Array<{fn: Function,isOnce: boolean}>
}
constructor(){
this.events={}
}
on(type: string,fn: Function,isOnce: boolean=false){
if(this.events[type]==null){
this.events[type]=[]// 初始化 key 的 fn 数组
}
this.events[type].push({ fn, isOnce })
}
once(type: string,fn: Function){
this.on(type,fn,true)
}
off(type: string,fn?: Function){
if(fn){
constfnList=this.events[type]
// 解绑某一个fn
if(fnList){
this.events[type]=fnList.filter(item=>item.fn!==fn)
}
}else{
// 解绑全部
this.events[type]=[]
}
}
emit(type: string, ...args: any[]){
constfnList=this.events[type]
if(fnList==null)return
// 这里用fitter不用forEach, 是因为要过滤once
this.events[type]=fnList.filter(item=>{
const{ fn, isOnce }=item
fn(...args)
// once执行一次就要过滤掉
if(!isOnce)returntrue
returnfalse
})
}
}
// 分开储存on和once事件
exportclassEventBus2{
privateevents: {[key: string]: Array<Function>}// { key1: [fn1,fn2,...], key2: [...]}
privateonceEvents: {[key: string]: Array<Function>}
constructor(){
this.events={}
this.onceEvents={}
}
on(type: string,fn: Function){
if(!this.events[type])this.events[type]=[]
this.events[type].push(fn)
}
once(type: string,fn: Function){
if(!this.onceEvents[type])this.onceEvents[type]=[]
this.onceEvents[type].push(fn)
}
off(type: string,fn?: Function){
if(fn){
// 解绑单个事件
constfnList=this.events[type]
constonceFnList=this.onceEvents[type]
if(fnList){
this.events[type]=fnList.filter(item=>item!==fn)
}
if(onceFnList){
this.onceEvents[type]=onceFnList.filter(item=>item!==fn)
}
}else{
// 解绑所有事件
this.events[type]=[]
this.onceEvents[type]=[]
}
}
emit(type: string, ...args: any[]){
this.events[type]&&this.events[type].forEach(cb=>cb(...args))
if(this.onceEvents[type]){
this.onceEvents[type].forEach(cb=>cb(...args))
this.onceEvents[type]=[]
}
}
}
// const e = new EventBus1()
// const e = new EventBus2()
// function fn1(a: any, b: any) { console.info('fn1', a, b); }
// function fn2(a: any, b: any) { console.info('fn2', a, b); }
// function fn3(a: any, b: any) { console.info('fn3', a, b); }
// e.on('key1', fn1)
// e.on('key1', fn2)
// e.once('key1', fn3)
// e.on('key2', fn3)
// e.emit('key1', 10, 20)
// e.emit('key1', 30, 40)
// e.off('key1', fn1)
// e.emit('key1', 100, 200)