|
| 1 | +type EventName = string | symbol |
| 2 | +type EventCallbacks = Record<EventName, Record<'next' | 'tail', unknown>> |
| 3 | + |
| 4 | +export class Events { |
| 5 | + protected callbacks?: EventCallbacks |
| 6 | + static eventSplitter = ',' // Note: Harmony ACE API 8 开发板不支持使用正则 split 字符串 /\s+/ |
| 7 | + |
| 8 | + constructor (opts?) { |
| 9 | + this.callbacks = opts?.callbacks ?? {} |
| 10 | + } |
| 11 | + |
| 12 | + on (eventName: EventName, callback: (...args: any[]) => void, context?: any): this { |
| 13 | + let event: EventName | undefined, tail, _eventName: EventName[] |
| 14 | + if (!callback) { |
| 15 | + return this |
| 16 | + } |
| 17 | + if (typeof eventName === 'symbol') { |
| 18 | + _eventName = [eventName] |
| 19 | + } else { |
| 20 | + _eventName = eventName.split(Events.eventSplitter) |
| 21 | + } |
| 22 | + this.callbacks ||= {} |
| 23 | + const calls = this.callbacks |
| 24 | + while ((event = _eventName.shift())) { |
| 25 | + const list = calls[event] |
| 26 | + const node: any = list ? list.tail : {} |
| 27 | + node.next = tail = {} |
| 28 | + node.context = context |
| 29 | + node.callback = callback |
| 30 | + calls[event] = { |
| 31 | + tail, |
| 32 | + next: list ? list.next : node |
| 33 | + } |
| 34 | + } |
| 35 | + return this |
| 36 | + } |
| 37 | + |
| 38 | + once (events: EventName, callback: (...r: any[]) => void, context?: any): this { |
| 39 | + const wrapper = (...args: any[]) => { |
| 40 | + callback.apply(this, args) |
| 41 | + this.off(events, wrapper, context) |
| 42 | + } |
| 43 | + |
| 44 | + this.on(events, wrapper, context) |
| 45 | + |
| 46 | + return this |
| 47 | + } |
| 48 | + |
| 49 | + off (events?: EventName, callback?: (...args: any[]) => void, context?: any) { |
| 50 | + let event: EventName | undefined, calls: EventCallbacks | undefined, _events: EventName[] |
| 51 | + if (!(calls = this.callbacks)) { |
| 52 | + return this |
| 53 | + } |
| 54 | + if (!(events || callback || context)) { |
| 55 | + delete this.callbacks |
| 56 | + return this |
| 57 | + } |
| 58 | + if (typeof events === 'symbol') { |
| 59 | + _events = [events] |
| 60 | + } else { |
| 61 | + _events = events ? events.split(Events.eventSplitter) : Object.keys(calls) |
| 62 | + } |
| 63 | + while ((event = _events.shift())) { |
| 64 | + let node: any = calls[event] |
| 65 | + delete calls[event] |
| 66 | + if (!node || !(callback || context)) { |
| 67 | + continue |
| 68 | + } |
| 69 | + const tail = node.tail |
| 70 | + while ((node = node.next) !== tail) { |
| 71 | + const cb = node.callback |
| 72 | + const ctx = node.context |
| 73 | + if ((callback && cb !== callback) || (context && ctx !== context)) { |
| 74 | + this.on(event, cb, ctx) |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + return this |
| 79 | + } |
| 80 | + |
| 81 | + trigger (events: EventName, ...args: any[]) { |
| 82 | + let event: EventName | undefined, node, calls: EventCallbacks | undefined, _events: EventName[] |
| 83 | + if (!(calls = this.callbacks)) { |
| 84 | + return this |
| 85 | + } |
| 86 | + if (typeof events === 'symbol') { |
| 87 | + _events = [events] |
| 88 | + } else { |
| 89 | + _events = events.split(Events.eventSplitter) |
| 90 | + } |
| 91 | + while ((event = _events.shift())) { |
| 92 | + if ((node = calls[event])) { |
| 93 | + const tail = node.tail |
| 94 | + while ((node = node.next) !== tail) { |
| 95 | + node.callback.apply(node.context || this, args) |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + return this |
| 100 | + } |
| 101 | +} |
0 commit comments