5 interface ListenerType {
10 export class EventEmitter {
11 private _events: {[type: string]: ListenerType[]};
14 // Restore the previous events if available, this will happen if the
15 // constructor is called multiple times on the same object (terminal reset).
16 this._events = this._events || {};
19 public on(type, listener): void {
20 this._events[type] = this._events[type] || [];
21 this._events[type].push(listener);
24 public off(type, listener): void {
25 if (!this._events[type]) {
29 let obj = this._events[type];
33 if (obj[i] === listener || obj[i].listener === listener) {
40 public removeAllListeners(type): void {
41 if (this._events[type]) {
42 delete this._events[type];
46 public once(type, listener): any {
48 let args = Array.prototype.slice.call(arguments);
50 return listener.apply(this, args);
52 (<any>on).listener = listener;
53 return this.on(type, on);
56 public emit(type): void {
57 if (!this._events[type]) {
61 let args = Array.prototype.slice.call(arguments, 1);
62 let obj = this._events[type];
64 for (let i = 0; i < obj.length; i++) {
65 obj[i].apply(this, args);
69 public listeners(type): ListenerType[] {
70 return this._events[type] || [];