-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
269 lines (233 loc) · 6.24 KB
/
index.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
module.exports = (function () {
// Author: Matthew Forrester <matt_at_keyboardwritescode.com>
// Copyright: Matthew Forrester
// License: MIT/BSD-style
"use strict";
/**
* # addEvents()
*
* Adds events to an existing pseudo-classical Javascript class.
*
* NOTE: Overwrites the following variables within the prototype:
*
* * _eventTypes
* * _emit
* * on
* * once
* * removeAllListeners
* * removeAllOnceListeners
* * removeOnceListener
* * removeOnceListener
*
* NOTE: Overwrites the following variables within the instance of a class
*
* * _onceListeners
* * _listeners
*
* ## Example
*
* ```javascript
* var MyClass = function() {
* };
*
* MyClass.prototype.doSomething = function() {
* return this._emit('doneit','a','b');
* };
*
* addEvents(MyClass,['doneit']);
*
* var myClass = new MyClass();
* myClass.on('doneit',function (a, b) {
* console.log('a = ' + a + ', b = ' + b);
* });
* myClass.doSomething();
* ```
*
* ## Parameters
* * **@param {Function} `classFunc`** The class to add events to.
* * **@param {Array} `events`** The events you want the class to support.
*/
var addEvents = function(classFunc, events) {
classFunc.prototype._eventTypes = events;
classFunc.prototype._emit = function(event /*, other arguments */) {
var i = 0,
args = Array.prototype.slice.call(arguments, 1);
if (this._eventTypes.indexOf(event) === -1) {
throw "SyncIt._emit(): Attempting to fire unknown event '" + event + "'";
}
var toFire = [];
if (
this.hasOwnProperty('_onceListeners') &&
this._onceListeners.hasOwnProperty(event)
) {
while (this._onceListeners[event].length) {
toFire.push(this._onceListeners[event].shift());
}
}
if (
this.hasOwnProperty('_listeners') &&
this._listeners.hasOwnProperty(event)
) {
for (i=0; i<this._listeners[event].length; i++) {
toFire.push(this._listeners[event][i]);
}
}
while (toFire.length) {
toFire.shift().apply(this, args);
}
};
var pushTo = function(objKey, event, func, ctx) {
if (ctx._eventTypes.indexOf(event) === -1) {
throw "addEvents: Attempting to listen for unknown event '"+event+"'";
}
if (!ctx.hasOwnProperty(objKey)) {
ctx[objKey] = {};
}
if (!ctx[objKey].hasOwnProperty(event)) {
ctx[objKey][event] = [];
}
ctx[objKey][event].push(func);
};
/**
* ### CLASS.on()
*
* Adds an event listeners to an event
*
* #### Parameters
*
* * **@param {String} `event`** The name of the event to listen for
* * **@param {Function} `listener`** The listener to fire when event occurs.
*
* #### Returns
*
* * **@return {Boolean}** True if that event is available to listen to.
*/
classFunc.prototype.on = function(event, func) {
pushTo('_listeners', event, func, this);
};
classFunc.prototype.listen = classFunc.prototype.on;
/**
* ### CLASS.once()
*
* Adds an event listeners which will be called only once then removed
*
* #### Parameters
*
* * **@param {String} `event`** The name of the event to listen for
* * **@param {Function} `listener`** The listener to fire when event occurs.
*
* #### Returns
*
* * **@return {Boolean}** True if that event is available to listen to.
*/
classFunc.prototype.once = function(event,func) {
pushTo('_onceListeners', event, func, this);
};
var removeAllListeners = function(objKey, event, ctx) {
var propertyNames = (function(ob) {
var r = [];
for (var k in ob) { if (ob.hasOwnProperty(k)) {
r.push(k);
} }
return r;
})(ctx[objKey]);
if (propertyNames.indexOf(event) == -1) {
return [];
}
var r = ctx[objKey][event];
ctx[objKey][event] = [];
return r;
};
/**
* ### CLASS.removeAllListeners()
*
* Removes all non `once` listeners for a specific event.
*
* #### Parameters
*
* * **@param {String} `event`** The name of the event you want to remove all listeners for.
*
* #### Returns
*
* * **@return {Array}** The listeners that have just been removed.
*/
classFunc.prototype.removeAllListeners = function(event) {
return removeAllListeners('_listeners', event, this);
};
/**
* ### CLASS.removeAllOnceListeners()
*
* Removes all `once` listeners for a specific event.
*
* #### Parameters
*
* * **@param {String} `event`** The name of the event you want to remove all listeners for.
*
* #### Returns
*
* * **@return {Array}** The listeners that have just been removed.
*/
classFunc.prototype.removeAllOnceListeners = function(event) {
return removeAllListeners('_onceListeners', event, this);
};
var removeListener = function(objKey, event, listener, ctx) {
var i = 0,
replacement = [],
successful = false;
var propertyNames = (function(ob) {
var r = [];
for (var k in ob) { if (ob.hasOwnProperty(k)) {
r.push(k);
} }
return r;
})(ctx[objKey]);
if (propertyNames.indexOf(event) == -1) {
return false;
}
for (i=0; i<ctx[objKey][event].length; i++) {
if (ctx[objKey][event][i] !== listener) {
replacement.push(ctx[objKey][event][i]);
} else {
successful = true;
}
}
ctx[objKey][event] = replacement;
return successful;
};
/**
* ### CLASS.removeListener()
*
* Removes a specific listener from an event (note, not from the `once()` call).
*
* #### Parameters
*
* * **@param {String} `event`** The name of the event you want to remove a listener from.
* * **@param {Function} `listener`** The listener you want to remove.
*
* #### Returns
*
* * **@return {Boolean}** True if the listener was removed, false otherwise.
*/
classFunc.prototype.removeListener = function(event, listener) {
return removeListener('_listeners', event, listener, this);
};
/**
* ### CLASS.removeOnceListener()
*
* Removes a specific listener from an event (note, not from the `once()` call).
*
* #### Parameters
*
* * **@param {String} `event`** The name of the event you want to remove a listener from.
* * **@param {Function} `listener`** The listener you want to remove.
*
* #### Returns
*
* * **@return {Boolean}** True if the listener was removed, false otherwise.
*/
classFunc.prototype.removeOnceListener = function(event, listener) {
return removeListener('_onceListeners', event, listener, this);
};
};
return addEvents;
}());