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
| class EventBus<T extends Record<string, (...args: any[]) => void>> { private events: Map<keyof T, Set<T[keyof T]>>
constructor() { this.events = new Map() }
on<K extends keyof T>(name: K, callback: T[K]) { if (!this.events.has(name)) { this.events.set(name, new Set()) } this.events.get(name)?.add(callback) }
emit<K extends keyof T>(name: K, ...args: Parameters<T[K]>) { if (this.events.has(name)) { this.events.get(name)?.forEach(cb => cb(...args)) } }
off<K extends keyof T>(name: K, callback?: T[K]) { if (this.events.has(name)) { if (callback) this.events.get(name)?.delete(callback) else this.events.delete(name) } }
once<K extends keyof T>(name: K, callback: T[K]) { const once = (...args: any[]) => { callback(...args) this.off(name, once as T[K]) } this.on(name, once as T[K]) } }
type Events = { evt1: () => void evt2: (data: number) => void evt3: (data: string) => void }
const bus = new EventBus<Events>()
function handleEvt1() { console.log('evt1 triggered') }
function handleEvt2(data: number) { console.log('evt2 triggered', data) }
function handleEvt3(data: string) { console.log('evt3 triggered', data) }
bus.on('evt1', () => { console.log('evt1 triggered 2') })
bus.on('evt1', handleEvt1) bus.on('evt2', handleEvt2) bus.once('evt3', handleEvt3)
bus.off('evt1', handleEvt1) bus.emit('evt1')
bus.emit('evt2', 123)
bus.emit('evt3', '执行一次') bus.emit('evt3', '再执行一次')
|