|
| 1 | +import { EventEmitter } from 'events'; |
| 2 | +import { Event } from './Event'; |
| 3 | +import { SelfDestructingCallbackInterface } from './EventManagerTypes'; |
| 4 | + |
| 5 | +export class EventManager extends EventEmitter { |
| 6 | + protected sharedEventManager: EventManager; |
| 7 | + |
| 8 | + private hooks: { [eventName: string]: Array<Function> } = {}; |
| 9 | + |
| 10 | + public constructor (sharedEventManager: EventManager = null) { |
| 11 | + super(); |
| 12 | + |
| 13 | + this.sharedEventManager = sharedEventManager; |
| 14 | + } |
| 15 | + |
| 16 | + async trigger(eventName: string, target: any, payload?: any): Promise<boolean> { |
| 17 | + if (!this.hooks[eventName]) { |
| 18 | + return false; |
| 19 | + } |
| 20 | + |
| 21 | + const callbacks = this.hooks[eventName]; |
| 22 | + |
| 23 | + let remaining = callbacks.length; |
| 24 | + |
| 25 | + for (let i = 0; i < remaining; i++) { |
| 26 | + const callback = callbacks[i] as SelfDestructingCallbackInterface; |
| 27 | + |
| 28 | + await callback(new Event<typeof target>(eventName, target, payload)); |
| 29 | + |
| 30 | + if (callback._isSelfDestructingCallback) { |
| 31 | + callbacks.splice(i, 1); |
| 32 | + |
| 33 | + remaining--; |
| 34 | + i--; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + if (!remaining) { |
| 39 | + delete this.hooks[eventName]; |
| 40 | + } |
| 41 | + |
| 42 | + return true; |
| 43 | + } |
| 44 | + |
| 45 | + has (event: string, callback: Function): boolean { |
| 46 | + return this.hooks[event] && this.hooks[event].indexOf(callback) > -1; |
| 47 | + } |
| 48 | + |
| 49 | + attachOnce(eventName: string, callback: Function, index?: number) { |
| 50 | + (callback as SelfDestructingCallbackInterface)._isSelfDestructingCallback = true; |
| 51 | + |
| 52 | + this.attach(eventName, callback, index); |
| 53 | + } |
| 54 | + |
| 55 | + attach(event: string, callback: Function, index?: number): this { |
| 56 | + this.hooks[event] = this.hooks[event] || []; |
| 57 | + |
| 58 | + if (index) { |
| 59 | + this.hooks[event].splice(index, 0, callback); |
| 60 | + } else { |
| 61 | + this.hooks[event].push(callback); |
| 62 | + } |
| 63 | + |
| 64 | + return this; |
| 65 | + } |
| 66 | + |
| 67 | + attachAt(index: number, event: string, callback: Function): this { |
| 68 | + return this.attach(event, callback, index); |
| 69 | + } |
| 70 | + |
| 71 | + detach(event: string, callback: Function): this { |
| 72 | + if (!this.hooks[event]) { |
| 73 | + return this; |
| 74 | + } |
| 75 | + |
| 76 | + const hookIndex: number = this.hooks[event].indexOf(callback); |
| 77 | + |
| 78 | + if (hookIndex === -1) { |
| 79 | + return this; |
| 80 | + } |
| 81 | + |
| 82 | + this.hooks[event].splice(hookIndex, 1); |
| 83 | + |
| 84 | + if (this.hooks[event].length === 0) { |
| 85 | + delete this.hooks[event]; |
| 86 | + } |
| 87 | + |
| 88 | + return this; |
| 89 | + } |
| 90 | + |
| 91 | + getSharedEventManager(): EventManager { |
| 92 | + return this.sharedEventManager; |
| 93 | + } |
| 94 | +} |
0 commit comments