1
|
|
|
/** |
2
|
|
|
* Utility class to handle with events (subscribe, notify etc) |
3
|
|
|
* |
4
|
|
|
* @class EventManager |
5
|
|
|
*/ |
6
|
|
|
class EventManager { |
7
|
|
|
constructor() { |
8
|
|
|
this._map = {}; |
9
|
|
|
} |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Add an event listener |
13
|
|
|
* |
14
|
|
|
* @param {String} eventName |
15
|
|
|
* @param {Function} fn |
16
|
|
|
* @returns {Function} the "unsubscriber". Call this function to unsubscribe this event (or use the unsubscribe method) |
17
|
|
|
* |
18
|
|
|
* @memberOf EventManager |
19
|
|
|
*/ |
20
|
|
|
subscribe(eventName, fn) { |
21
|
|
|
if (typeof eventName !== "string") { |
22
|
|
|
throw "eventName must be string"; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
if (!eventName.length) { |
26
|
|
|
throw "eventName cannot be empty"; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
if (!this._map.hasOwnProperty(eventName)) { |
30
|
|
|
this._map[eventName] = []; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
this._map[eventName].push(fn); |
34
|
|
|
return this.unsubscribe.bind(this, eventName, fn); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @see subscribe |
39
|
|
|
* Add an event listener to multiple event aht the sabe time |
40
|
|
|
* |
41
|
|
|
* @param {any} eventNames |
42
|
|
|
* @param {any} fn |
43
|
|
|
* |
44
|
|
|
* @memberOf EventManager |
45
|
|
|
*/ |
46
|
|
|
subscribeMultiple(eventNames, fn) { |
47
|
|
|
let i, length = eventNames.length; |
48
|
|
|
|
49
|
|
|
var unsubscribes = []; |
50
|
|
|
for (i = 0; i < length; i++) { |
51
|
|
|
unsubscribes.push(this.subscribe(eventNames[i], fn)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return () => { |
55
|
|
|
unsubscribes.map(unsubscribe => unsubscribe()); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Removes an event listener from an event |
61
|
|
|
* |
62
|
|
|
* @param {string} eventName |
63
|
|
|
* @param {Function} fn |
64
|
|
|
* @returns |
65
|
|
|
* |
66
|
|
|
* @memberOf EventManager |
67
|
|
|
*/ |
68
|
|
|
unsubscribe(eventName, fn) { |
69
|
|
|
if (!this._map[eventName]) { |
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
let index = this._map[eventName].indexOf(fn); |
74
|
|
|
if (index !== -1) { |
75
|
|
|
this._map[eventName].splice(index, 1); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @see unsubscribe |
81
|
|
|
* Removes the event listener from multiple events |
82
|
|
|
* |
83
|
|
|
* @param {string} eventNames |
84
|
|
|
* @param {Function} fn |
85
|
|
|
* |
86
|
|
|
* @memberOf EventManager |
87
|
|
|
*/ |
88
|
|
|
unsubscribeMultiple(eventNames, fn) { |
89
|
|
|
let i, length = eventNames.length; |
90
|
|
|
|
91
|
|
|
for (i = 0; i < length; i++) { |
92
|
|
|
this.unsubscribe(eventNames[i], fn); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Removes all event listeners from the given events |
98
|
|
|
* |
99
|
|
|
* @param {String[]} eventNames |
100
|
|
|
* |
101
|
|
|
* @memberOf EventManager |
102
|
|
|
*/ |
103
|
|
|
unsubscribeAll(eventNames) { |
104
|
|
|
let i, length = eventNames.length; |
105
|
|
|
for (i = 0; i < length; i++) { |
106
|
|
|
if (this._map.hasOwnProperty(eventNames[i])) { |
107
|
|
|
delete this._map(eventNames[i]); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Fire an event. Will send all arguments afeter eventName to the existent |
114
|
|
|
* event listeners |
115
|
|
|
* |
116
|
|
|
* @param {String} eventName |
117
|
|
|
* @returns |
118
|
|
|
* |
119
|
|
|
* @memberOf EventManager |
120
|
|
|
*/ |
121
|
|
|
notify(eventName) { |
122
|
|
|
if (!this._map.hasOwnProperty(eventName)) { |
123
|
|
|
return; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// make an copy of the arguments to prevent someone to change it |
127
|
|
|
let args = Array.prototype.slice.call(arguments, 1); |
128
|
|
|
this._map[eventName].forEach(function (fn) { |
129
|
|
|
fn.apply(this, args); |
130
|
|
|
}); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
export default EventManager; |
135
|
|
|
|