Created
June 21, 2017 12:25
-
-
Save sqs/2c5b32c3f7e4822853f99d929dbf64db to your computer and use it in GitHub Desktop.
typescript eventemitter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* An EventEmitter that implements the NodeJS.EventEmitter interface. | |
*/ | |
export class EventEmitter implements NodeJS.EventEmitter { | |
private _events: { [name: string]: Function[] } = Object.create(null); | |
private _maxListeners = 10; | |
private _onceListeners: Function[] = []; | |
public addListener = this.on; | |
private _insertListener(event: string | symbol, listener: Function, prepend: boolean = false): void { | |
let eventListeners = this._events[event] = this._events[event] || []; | |
if (prepend) { | |
eventListeners.unshift(listener); | |
} else { | |
eventListeners.push(listener); | |
} | |
} | |
public on(event: string | symbol, listener: Function): this { | |
this._insertListener(event, listener); | |
return this; | |
} | |
public once(event: string | symbol, listener: Function): this { | |
this._onceListeners.push(listener); | |
this._insertListener(event, listener); | |
return this; | |
} | |
public removeListener(event: string | symbol, listener: Function): this { | |
if (this._events[event] !== undefined) { | |
const i = this._events[event].indexOf(listener); | |
if (i >= 0) { | |
this._events[event].splice(i, 1); | |
} | |
} | |
return this; | |
} | |
public removeAllListeners(event?: string | symbol): this { | |
if (event === undefined) { | |
this._events = Object.create(null); | |
} else { | |
delete this._events[event]; | |
} | |
return this; | |
} | |
public setMaxListeners(n: number): this { | |
this._maxListeners = n; | |
return this; | |
} | |
public getMaxListeners(): number { | |
return this._maxListeners; | |
} | |
public listeners(event: string | symbol): Function[] { | |
if (this._events[event] !== undefined) { | |
return this._events[event]; | |
} | |
return []; | |
} | |
public emit(event: string | symbol, ...args: any[]): boolean { | |
let hadListeners = false; | |
if (this._events[event] !== undefined) { | |
for (let i = 0; i < this._events[event].length; i++) { | |
const listener = this._events[event][i]; | |
hadListeners = true; | |
const j = this._onceListeners.indexOf(listener); | |
if (j >= 0) { | |
this._onceListeners.splice(j, 1); | |
this._events[event].splice(i, 1); | |
i--; // compensate for removal | |
} | |
listener(...args); | |
} | |
} | |
return hadListeners; | |
} | |
public listenerCount(event: string | symbol): number { | |
if (this._events[event] !== undefined) { | |
return this._events[event].length; | |
} | |
return 0; | |
} | |
public prependListener(event: string | symbol, listener: Function): this { | |
this._insertListener(event, listener, true); | |
return this; | |
} | |
public prependOnceListener(event: string | symbol, listener: Function): this { | |
this._onceListeners.push(listener); | |
this._insertListener(event, listener, true); | |
return this; | |
} | |
public eventNames(): (string | symbol)[] { | |
return Object.keys(this._events); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment