|
| 1 | +export type TEventName = string | symbol; |
| 2 | +export type TEventCallback<T> = (a: T, ...args: any[]) => void; |
| 3 | + |
| 4 | +export interface ListenEvents<T> { |
| 5 | + on: (eventName: TEventName, fn: TEventCallback<T>) => EventEmitter<T>; |
| 6 | + once: (eventName: TEventName, fn: TEventCallback<T>) => EventEmitter<T>; |
| 7 | + off: (eventName: TEventName, fn: TEventCallback<T>) => EventEmitter<T>; |
| 8 | + addListener: ( |
| 9 | + eventName: TEventName, |
| 10 | + fn: TEventCallback<T> |
| 11 | + ) => EventEmitter<T>; |
| 12 | + removeListener: ( |
| 13 | + eventName: TEventName, |
| 14 | + fn: TEventCallback<T> |
| 15 | + ) => EventEmitter<T>; |
| 16 | + removeAllListeners: (eventName: TEventName) => EventEmitter<T>; |
| 17 | + |
| 18 | + eventNames: () => TEventName[]; |
| 19 | +} |
| 20 | + |
| 21 | +export interface EmitEvents<T> { |
| 22 | + emit: (eventName: TEventName, value: T, ...args) => boolean; |
| 23 | +} |
| 24 | + |
| 25 | +export class EventEmitter<T> implements EmitEvents<T>, ListenEvents<T> { |
| 26 | + private listenersMap = new Map<TEventName, Array<Array<TEventCallback<T>>>>(); |
| 27 | + |
| 28 | + /** |
| 29 | + * @param {TEventName} eventName |
| 30 | + * @param {TEventCallback} cb |
| 31 | + * @returns {EventEmitter} |
| 32 | + */ |
| 33 | + public on(eventName: TEventName, cb: TEventCallback<T>): this { |
| 34 | + if (!this.hasEventContainer(eventName)) { |
| 35 | + this.createEventContainer(eventName); |
| 36 | + } |
| 37 | + |
| 38 | + const container = this.getEventContainer(eventName); |
| 39 | + |
| 40 | + container.push([cb]); |
| 41 | + |
| 42 | + return this; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @param {TEventName} eventName |
| 47 | + * @param {TEventCallback} cb |
| 48 | + * @returns {EventEmitter} |
| 49 | + */ |
| 50 | + public once(eventName: TEventName, cb: TEventCallback<T>): this { |
| 51 | + if (!this.hasEventContainer(eventName)) { |
| 52 | + this.createEventContainer(eventName); |
| 53 | + } |
| 54 | + |
| 55 | + const container = this.getEventContainer(eventName); |
| 56 | + |
| 57 | + const wrapped = (value: T, ...args) => { |
| 58 | + this.off(eventName, wrapped); |
| 59 | + cb(value, ...args); |
| 60 | + }; |
| 61 | + |
| 62 | + container.push([wrapped, cb]); |
| 63 | + |
| 64 | + return this; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @param {TEventName} eventName |
| 69 | + * @param {TEventCallback} cb |
| 70 | + * @returns {EventEmitter} |
| 71 | + */ |
| 72 | + public off(eventName: TEventName, cb: TEventCallback<T>): this { |
| 73 | + const container = this.getEventContainer(eventName); |
| 74 | + |
| 75 | + if (!container) { |
| 76 | + return this; |
| 77 | + } |
| 78 | + |
| 79 | + const index = container.findIndex(item => { |
| 80 | + return item.includes(cb); |
| 81 | + }); |
| 82 | + |
| 83 | + if (index >= 0) { |
| 84 | + container.splice(index, 1); |
| 85 | + } |
| 86 | + |
| 87 | + return this; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @param {TEventName} eventName |
| 92 | + * @param {*} value |
| 93 | + * @param {*} args |
| 94 | + * @returns {boolean} |
| 95 | + */ |
| 96 | + public emit(eventName: TEventName, value: T, ...args): boolean { |
| 97 | + const container = this.getEventContainer(eventName); |
| 98 | + |
| 99 | + if (!container) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + for (const [cb] of container) { |
| 104 | + cb(value, ...args); |
| 105 | + } |
| 106 | + |
| 107 | + return container.length > 0; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @param {TEventName} eventName |
| 112 | + * @param {TEventCallback} cb |
| 113 | + * @returns {EventEmitter} |
| 114 | + */ |
| 115 | + public addListener(eventName: TEventName, cb: TEventCallback<T>): this { |
| 116 | + return this.on(eventName, cb); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * @param {TEventName} eventName |
| 121 | + * @param {TEventCallback} cb |
| 122 | + * @returns {EventEmitter} |
| 123 | + */ |
| 124 | + public removeListener(eventName: TEventName, cb: TEventCallback<T>): this { |
| 125 | + return this.off(eventName, cb); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * @param {TEventName?} [eventName] |
| 130 | + * @returns {EventEmitter} |
| 131 | + */ |
| 132 | + public removeAllListeners(eventName: TEventName = null): this { |
| 133 | + if (eventName) { |
| 134 | + this.deleteEventContainer(eventName); |
| 135 | + } else { |
| 136 | + this.listenersMap.clear(); |
| 137 | + } |
| 138 | + |
| 139 | + return this; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * @returns {TEventName[]} |
| 144 | + */ |
| 145 | + public eventNames(): TEventName[] { |
| 146 | + return [...this.listenersMap.keys()]; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * @param {TEventName} eventName |
| 151 | + */ |
| 152 | + private createEventContainer(eventName: TEventName): void { |
| 153 | + this.listenersMap.set(eventName, []); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * @param {TEventName} eventName |
| 158 | + */ |
| 159 | + private deleteEventContainer(eventName: TEventName): void { |
| 160 | + this.listenersMap.delete(eventName); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * @param {TEventName} eventName |
| 165 | + */ |
| 166 | + private getEventContainer( |
| 167 | + eventName: TEventName |
| 168 | + ): Array<Array<TEventCallback<T>>> { |
| 169 | + return this.listenersMap.get(eventName); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * @param {TEventName} eventName |
| 174 | + */ |
| 175 | + private hasEventContainer(eventName: TEventName): boolean { |
| 176 | + return this.listenersMap.has(eventName); |
| 177 | + } |
| 178 | +} |
0 commit comments