-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy path19-implement-eventemitter-part1.js
34 lines (29 loc) · 1.24 KB
/
19-implement-eventemitter-part1.js
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
// Minutes of Javascript #19 - How to implement a local EventEmitter? (part 1.)
// See: https://playcode.io/655981/
// First create a new class called "EventEmitter"
class EventEmitter {
constructor() { // Let's register a Map() for storing (event, callbacks[]) values
this.callbackMap = new Map(); // We use a map so we can pass anything as key (an object uses string as key)
}
// The .on() method is storing (event, callback) in our in-memory map
on(event, cb) {
const callbacks = this.callbackMap.get(event);
this.callbackMap.set(event, callbacks ? [...callbacks, cb] : [cb]);
}
// The .emit() method is calling all registered callbacks for a given event
emit(event, data) {
const callbacks = this.callbackMap.get(event);
if (!Array.isArray(callbacks)) return; // returns early
callbacks.map((callback) => callback(data));
}
}
module.exports = EventEmitter;
// Now let's use it
const myEventEmitter = new EventEmitter();
myEventEmitter.on('event', (data) => console.info('listener #1', data));
myEventEmitter.on('event', (data) => console.info('listener #2', data));
myEventEmitter.emit('event', {'foo': 'bar'});
/*
listener #1 { foo: 'bar' }
listener #2 { foo: 'bar' }
*/