]> git.proxmox.com Git - mirror_xterm.js.git/blobdiff - src/EventEmitter.ts
Merge pull request #926 from ficristo/search-fix
[mirror_xterm.js.git] / src / EventEmitter.ts
index 092e439ed63429dc9d8b1b2dc6f4f2ae5ab9a951..00c10941864e37634ee827115a6c14a8e5c26c8c 100644 (file)
@@ -2,30 +2,28 @@
  * @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() {
+    // 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 || {};
   }
 
-  // TODO: Merge addListener and on, no reason for an alias in a private component
-  public addListener(type, listener): void {
+  public on(type, listener): void {
     this._events[type] = this._events[type] || [];
     this._events[type].push(listener);
   }
 
-  public on(type, listener): void {
-    this.addListener(type, listener);
-  }
-
-  // TODO: Merge removeListener and off, no reason for an alias in a private component
-  public removeListener(type, listener): void {
+  public off(type, listener): void {
     if (!this._events[type]) {
       return;
     }
@@ -41,10 +39,6 @@ export class EventEmitter {
     }
   }
 
-  public off(type, listener): void {
-    this.removeListener(type, listener);
-  }
-
   public removeAllListeners(type): void {
     if (this._events[type]) {
        delete this._events[type];
@@ -54,21 +48,18 @@ export class EventEmitter {
   public once(type, listener): any {
     function on() {
       let args = Array.prototype.slice.call(arguments);
-      this.removeListener(type, on);
+      this.off(type, on);
       return listener.apply(this, args);
     }
     (<any>on).listener = listener;
     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);
     }