|
@@ -1,56 +0,0 @@
|
1
|
|
-/**
|
2
|
|
- * Implements utility to forward events from one eventEmitter to another.
|
3
|
|
- * @param src {object} instance of EventEmitter or another class that implements
|
4
|
|
- * addListener method which will register listener to EventEmitter instance.
|
5
|
|
- * @param dest {object} instance of EventEmitter or another class that
|
6
|
|
- * implements emit method which will emit an event.
|
7
|
|
- */
|
8
|
|
-function EventEmitterForwarder(src, dest) {
|
9
|
|
- if (!src || !dest || typeof src.addListener !== 'function'
|
10
|
|
- || typeof dest.emit !== 'function') {
|
11
|
|
- throw new Error('Invalid arguments passed to EventEmitterForwarder');
|
12
|
|
- }
|
13
|
|
- this.src = src;
|
14
|
|
- this.dest = dest;
|
15
|
|
- this.listeners = new Map();
|
16
|
|
-}
|
17
|
|
-
|
18
|
|
-/**
|
19
|
|
- * Adds event to be forwarded from src to dest.
|
20
|
|
- * @param srcEvent {string} the event that EventEmitterForwarder is listening
|
21
|
|
- * for.
|
22
|
|
- * @param dstEvent {string} the event that will be fired from dest.
|
23
|
|
- * @param arguments all other passed arguments are going to be fired with
|
24
|
|
- * dstEvent.
|
25
|
|
- */
|
26
|
|
-EventEmitterForwarder.prototype.forward = function(...args) {
|
27
|
|
- const srcEvent = args[0];
|
28
|
|
-
|
29
|
|
- // This will be the "this" value for emit function.
|
30
|
|
-
|
31
|
|
- args[0] = this.dest;
|
32
|
|
-
|
33
|
|
- // Using bind.apply to pass the arguments as Array-like object ("arguments")
|
34
|
|
- const newListener = Function.prototype.bind.apply(this.dest.emit, args);
|
35
|
|
-
|
36
|
|
- this.src.addListener(srcEvent, newListener);
|
37
|
|
- this.listeners.set(srcEvent, newListener);
|
38
|
|
-};
|
39
|
|
-
|
40
|
|
-/**
|
41
|
|
- * Clears the listeners for the supplied events.
|
42
|
|
- *
|
43
|
|
- * @param args all the events which listeners to be cleaned.
|
44
|
|
- */
|
45
|
|
-EventEmitterForwarder.prototype.removeListeners = function(...args) {
|
46
|
|
- args.forEach(a => {
|
47
|
|
- const l = this.listeners.get(a);
|
48
|
|
-
|
49
|
|
- if (l) {
|
50
|
|
- this.src.removeListener(a, l);
|
51
|
|
- this.listeners.delete(a);
|
52
|
|
- }
|
53
|
|
- });
|
54
|
|
-};
|
55
|
|
-
|
56
|
|
-module.exports = EventEmitterForwarder;
|