showing results for - "environment modules macos"
Adriana
06 Nov 2020
1(() => {
2  'use strict';
3
4  class EventEmitter {
5    constructor() {
6      this.events = new Map();
7    }
8
9    on(event, listener) {
10      if (typeof listener !== 'function') {
11        throw new TypeError('The listener must be a function');
12      }
13      let listeners = this.events.get(event);
14      if (!listeners) {
15        listeners = new Set();
16        this.events.set(event, listeners); 
17      }
18      listeners.add(listener);
19      return this;
20    }
21
22    off(event, listener) {
23      if (!arguments.length) {
24        this.events.clear();
25      } else if (arguments.length === 1) {
26        this.events.delete(event);
27      } else {
28        const listeners = this.events.get(event);
29        if (listeners) {
30          listeners.delete(listener);
31        }
32      }
33      return this;
34    }
35
36    emit(event, ...args) {
37      const listeners = this.events.get(event);
38      if (listeners) {
39        for (let listener of listeners) {
40          listener.apply(this, args);
41        }
42      }
43      return this;
44    }
45  }
46
47  this.EventEmitter = EventEmitter;
48})();
49
similar questions
queries leading to this page
environment modules macos