File size: 1,111 Bytes
19605ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
 * Copyright (c) 2019, Microsoft Corporation (MIT License).
 */

import { IDisposable } from './types';

interface IListener<T> {
  (e: T): void;
}

export interface IEvent<T> {
  (listener: (e: T) => any): IDisposable;
}

export class EventEmitter2<T> {
  private _listeners: IListener<T>[] = [];
  private _event?: IEvent<T>;

  public get event(): IEvent<T> {
    if (!this._event) {
      this._event = (listener: (e: T) => any) => {
        this._listeners.push(listener);
        const disposable = {
          dispose: () => {
            for (let i = 0; i < this._listeners.length; i++) {
              if (this._listeners[i] === listener) {
                this._listeners.splice(i, 1);
                return;
              }
            }
          }
        };
        return disposable;
      };
    }
    return this._event;
  }

  public fire(data: T): void {
    const queue: IListener<T>[] = [];
    for (let i = 0; i < this._listeners.length; i++) {
      queue.push(this._listeners[i]);
    }
    for (let i = 0; i < queue.length; i++) {
      queue[i].call(undefined, data);
    }
  }
}