-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
37 lines (33 loc) · 915 Bytes
/
index.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
/**
* 发布订阅模式
*/
export default class EventBus {
private cache: { [key: string]: Array<( data?:unknown ) => void> } = {}
on(eventName:string, fn: (data?:unknown) => void){
this.cache[eventName] = this.cache[eventName] || []
this.cache[eventName].push(fn)
}
emit(eventName:string, data?: unknown){
(this.cache[eventName] || []).forEach((fn: (data?:unknown)=>void) => fn(data))
}
off(eventName:string, fn: (data?:unknown) => void){
let index: number = indexOf(this.cache[eventName], fn)
if(index === -1) return
this.cache[eventName].splice(index, 1)
}
}
/**
* 等价于Array.prototype.indexOf
* @param array
* @param item
*/
function indexOf(array: Array<unknown>, item) {
if (array === undefined) return -1
let index:number = -1
for (let i:number = 0; i < array.length; i ++ ){
if (array[i] === item) {
index = i
}
}
return index
}