* @license MIT
*/
+import { IEventEmitter } from './Interfaces';
+
interface ListenerType {
(): void;
listener?: () => void;
};
-export class EventEmitter {
+export class EventEmitter implements IEventEmitter {
private _events: {[type: string]: ListenerType[]};
constructor() {
- this._events = {};
+ // Restore the previous events if available, this will happen if the
+ // constructor is called multiple times on the same object (terminal reset).
+ this._events = this._events || {};
}
public on(type, listener): void {
return this.on(type, on);
}
- public emit(type): void {
+ public emit(type: string, ...args: any[]): void {
if (!this._events[type]) {
return;
}
-
- let args = Array.prototype.slice.call(arguments, 1);
let obj = this._events[type];
-
for (let i = 0; i < obj.length; i++) {
obj[i].apply(this, args);
}