-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevemit.js
185 lines (154 loc) · 4.08 KB
/
evemit.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
* @name Evemit
* @description Minimal and fast JavaScript event emitter for Node.js and front-end.
* @author Nicolas Tallefourtane <[email protected]>
* @link https://github.com/Nicolab/evemit
* @license MIT https://github.com/Nicolab/evemit/blob/master/LICENSE
*/
;(function() {
'use strict';
/**
* Evemit
*
* @constructor
* @api public
*/
function Evemit() {
this.events = {};
}
/**
* Register a new event listener for a given event.
*
* @param {string} event Event name.
* @param {function} fn Callback function (listener).
* @param {*} [context] Context for function execution.
* @return {Evemit} Current instance.
* @api public
*/
Evemit.prototype.on = function(event, fn, context) {
if (!this.events[event]) {
this.events[event] = [];
}
if(context) {
fn._E_ctx = context;
}
this.events[event].push(fn);
return this;
};
/**
* Add an event listener that's only called once.
*
* @param {string} event Event name.
* @param {function} fn Callback function (listener).
* @param {*} [context] Context for function execution.
* @return {Evemit} Current instance.
* @api public
*/
Evemit.prototype.once = function(event, fn, context) {
fn._E_once = true;
return this.on(event, fn, context);
};
/**
* Emit an event to all registered event listeners.
*
* @param {string} event Event name.
* @param {*} [...arg] One or more arguments to pass to the listeners.
* @return {bool} Indication, `true` if at least one listener was executed,
* otherwise returns `false`.
* @api public
*/
Evemit.prototype.emit = function(event, arg1, arg2, arg3, arg4) {
var fn, evs, args, aLn;
if(!this.events[event]) {
return false;
}
args = Array.prototype.slice.call(arguments, 1);
aLn = args.length;
evs = this.events[event];
for(var i = 0, ln = evs.length; i < ln; i++) {
fn = evs[i];
if (fn._E_once) {
this.off(event, fn);
}
// Function.apply() is a bit slower, so try to do without
switch (aLn) {
case 0:
fn.call(fn._E_ctx);
break;
case 1:
fn.call(fn._E_ctx, arg1);
break;
case 2:
fn.call(fn._E_ctx, arg1, arg2);
break;
case 3:
fn.call(fn._E_ctx, arg1, arg2, arg3);
break;
case 4:
fn.call(fn._E_ctx, arg1, arg2, arg3, arg4);
break;
default:
fn.apply(fn._E_ctx, args);
}
}
return true;
};
/**
* Remove event listeners.
*
* @param {string} event The event to remove.
* @param {function} fn The listener that we need to find.
* @return {Evemit} Current instance.
* @api public
*/
Evemit.prototype.off = function(event, fn) {
if (!this.events[event]) {
return this;
}
for (var i = 0, ln = this.events[event].length; i < ln; i++) {
if (this.events[event][i] === fn) {
this.events[event][i] = null;
delete this.events[event][i];
}
}
// re-index
this.events[event] = this.events[event].filter(function(ltns) {
return typeof ltns !== 'undefined';
});
return this;
};
/**
* Get a list of assigned event listeners.
*
* @param {string} [event] The events that should be listed.
* If not provided, all listeners are returned.
* Use the property `Evemit.events` if you want to get an object like
* ```
* {event1: [array of listeners], event2: [array of listeners], ...}
* ```
*
* @return {array}
* @api public
*/
Evemit.prototype.listeners = function(event) {
var evs, ltns;
if(event) {
return this.events[event] || [];
}
evs = this.events;
ltns = [];
for(var ev in evs) {
ltns = ltns.concat(evs[ev].valueOf());
}
return ltns;
};
/**
* Expose Evemit
* @type {Evemit}
*/
if(typeof module !== 'undefined' && module.exports) {
module.exports = Evemit;
} else {
window.Evemit = Evemit;
}
})();