-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultivents.js
471 lines (375 loc) Β· 18.3 KB
/
multivents.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/* global module,setTimeout */
/**
* @author Peter Steinberg <[email protected]>
* @summary Multifunctional Event System
* @fileOverview This is the only source file of multivents. It includes all the functionality and API.
*/
var Channel; // eslint-disable-line no-unused-vars
/**
* This constructor function creates a new message channel. You can invoke the constructor without passing any arguments (using the `new` keyword if you want to) and a new channel object is created for you. You can also call `Channel` as a function and pass in an object, that you want to transform into a message channel. A third option would be to pass the constructor a string, which creates a named channel.
*
* @param {Object} [target] An object that is to be transormed into an event channel. If no object is given, a new one is being created.
* @exports Channel
* @constructor
*/
Channel = function (target) {
'use strict';
var addEvent,
channel,
emit,
events,
locked,
silenced;
locked = silenced = false;
addEvent = function (eventName, override) {
if (typeof events[eventName] === "undefined" || override) {
events[eventName] = {
"callbacks": [],
"silenced": false,
"locked": false
};
}
return events[eventName];
};
/**
* The `events` variable saves the names of registered events and associates those names with arrays of callback functions to be called, when the event is triggered.
*
* @member
*/
events = {};
if (typeof target === 'object') {
channel = target;
} else {
channel = this || {};
}
emit = function emit (type, data, async) { // eslint-disable-line no-shadow
var asyncEvt,
asyncScore,
callback,
index,
len,
list;
if (silenced || (events[type] && events[type].silenced)) {
return channel;
}
if (type === '*') {
list = (Object.keys(events).reduce(
function (callbacks, event) {
if (events[event] && events[event].silenced) {
return callbacks;
} else {
return callbacks.concat(events[event].callbacks);
}
},
[])
) || [];
} else {
list = (events[type] && events[type].callbacks) || [];
list = list.concat(events['*'] && events['*'].callbacks || []);
}
len = list.length;
index = 0;
asyncEvt = async === false ? -1 : async || 0;
for (; index < len; index = index + 1) {
callback = list[index];
if (!callback.silenced) {
asyncScore = callback.async + asyncEvt;
if ((callback.async === 0 && asyncEvt === 0) || asyncScore > 0 || asyncEvt === 1) {
setTimeout(
function () {
this.func.apply(null,
this.data.concat([ this ])
);
}.bind({
"func": callback.callbackFunction,
"name": type,
"channel": channel,
"async": true,
"data": data
}),
0
);
} else if (asyncScore < 0 || asyncEvt === -1) {
list[index].callbackFunction.apply(null,
data.concat([ {
"func": callback.callbackFunction,
"name": type,
"channel": channel,
"async": false,
"data": data
} ])
);
}
}
}
return channel;
};
Object.assign(channel, /** @lends Channel# */{
/**
* With this method you can register callbacks to be executed when a certain event is triggered. You have to pass in the event name and the callback function, but you can optionally provide a third argument. This third argument should be an object which is then used as the callback function's `this` (context injection). The fourth parameter of the `on` function is a boolean that gives a preference on whether the callback function shall be executed asynchronously. Note, however, that asynchronous execution is not guarenteed.
*
* @param {String} type The name of the event is specified by a string. It doesn't matter,
* whether this event name already exists or not.
* @param {Function} func The function to be called, when the event is triggered.
* @param {boolean} [async] A preference regarding whether this callback shall the executed asynchronously. (Not a guarantee!)
* @returns {Object} The channel object. Or rather: 'this'. So be careful with rebinding 'this'.
*/
"on": function on (type, func, async) { // eslint-disable-line id-length
if (locked || (events[type] && events[type].locked)) {
return this;
}
if (typeof type !== 'string' || typeof type === 'string' && (!type.match(/^[a-zA-Z_#][a-zA-Z0-9_-]*$/) && type !== '*')) {
return this;
}
addEvent(type).callbacks.push({
"callbackFunction": func,
"silenced": false,
"async": async === false ? -1 : async || 0
});
return this;
},
/**
* This method does the same as `on` but it registers a callback that will only be executed once.
*
* @param {String} type The name of the event is specified by a string. It doesn't matter, whether this event name already exists or not.
* @param {Function} func The function to be called, when the event is triggered.
* @param {boolean} [async] A preference regarding whether this callback shall the executed asynchronously. (Not a guarantee!)
* @returns {Object} The channel object. Or rather: 'this'. So be careful with rebinding 'this'.
*/
"once": function once (type, func, async) {
var callbackFunction,
called;
called = false;
callbackFunction = function () {
if (!called) {
// I explicitly want to say, that disabling this rule is fine here, because I am sure, that the correct binding of 'this' gets taken care of!
func.apply(this, [].slice.call(arguments)); // eslint-disable-line no-invalid-this
channel.off(type, callbackFunction);
called = true;
}
};
this.on(type, callbackFunction, async);
return this;
},
/**
* Removes event listeners. Functions will no longer be invoked when the specified event is triggered. You can pass in the event name and a function reference to remove a specific function. If you just provide the first parameter, all callbacks for the given event type are removed. You can remove all event handlers from all events on this channel, by calling `off` without any arguments.
*
* @param {String} [type] The event name of the callbacks to be removed.
* @param {Function} [func] The callback function to be removed.
* @returns {Object} The channel object. Or rather: 'this'. So be careful with rebinding 'this'.
*/
"off": function off (type, func) {
var index,
typeIndex;
if (locked === true || type === '*') {
return this;
}
if (typeof type === "undefined") {
for (typeIndex in events) {
if (events.hasOwnProperty(typeIndex)) {
events[typeIndex].callbacks = [];
}
}
} else if (events[type] && events[type].locked === false) {
if (typeof func === "undefined") {
events[type].callbacks = [];
} else {
for (index = 0; index < events[type].callbacks.length; index = index + 1) {
if (events[type].callbacks[index].callbackFunction === func) {
events[type].callbacks.splice(index, 1);
index = index - 1;
}
}
}
}
return this;
},
/**
* Calling this method triggers the specified event and will result in all registered callbacks being executed. All arguments that get passed to `emit` after the event name are provided as arguments to each callback function.
* You should not rely on the order in which the callbacks are being invoked.
*
* @param {String} type The name of the event to be triggered. Any additional arguments will be passed to the callback function.
* @returns {Object} The channel object. Or rather: 'this'. So be careful with rebinding 'this'.
*/
"emit": function (type) {
return emit.call(this, type, Array.prototype.slice.call(arguments, 1));
},
/**
* This method works like `emit` but guarantees synchronous execution of all callbacks for this event.
*
* @param {String} type The name of the event to be triggered. Any additional arguments will be passed to the callback function.
* @returns {Object} The channel object. Or rather: 'this'. So be careful with rebinding 'this'.
*/
"emitSync": function (type) {
return emit.call(this, type, Array.prototype.slice.call(arguments, 1), false);
},
/**
* This method works like `emit` but guarantees asynchronous execution of all callbacks for this event.
*
* @param {String} type The name of the event to be triggered. Any additional arguments will be passed to the callback function.
* @returns {Object} The channel object. Or rather: 'this'. So be careful with rebinding 'this'.
*/
"emitAsync": function (type) {
return emit.call(this, type, Array.prototype.slice.call(arguments, 1), true);
},
/**
* The `silence` method prevents any new messages from being sent over the message channel. Affects the entire channel or specific events or even specific callbacks.
*
* @method
* @param {String} [type] The name of the event that is meant to be silenced. If no event type is specified, the whole channel will be silenced.
* @param {Function} [func] The function that is no longer to be executed when the event is triggered. If no function is specified, the whole event type will be silenced.
* @returns {Object} The channel object. Or rather: 'this'. So be careful with rebinding 'this'.
*/
"silence": function silence (type, func) {
var callbackCount,
evt,
index;
if (typeof type === "undefined") {
silenced = true;
return this;
}
if (typeof func === "undefined") {
addEvent(type).silenced = true;
return this;
}
evt = events[type];
if (typeof evt !== "undefined") {
callbackCount = evt.callbacks.length;
for (index = 0; index < callbackCount; index = index + 1) {
if (evt.callbacks[index].callbackFunction === func) {
evt.callbacks[index].silenced = true;
}
}
}
return this;
},
/**
* With this method you can enable message sending after it was disable using `silence`.
* If you unsilence a single event, but the entire channel is still silenced, triggering the event will still have no effect.
* Unsilencing a channel that was not silenced does not throw but instead just does nothing.
*
* @param {String} [type] The name of the event that is meant to be unsilenced. If no event type is given, the whole channel well be unlocked.
* @param {Function} [func] The function that shall be executed again, after being silenced. If no function is given, the whole event type will be unsilenced.
* @returns {Object} The channel object. Or rather: 'this'. So be careful with rebinding 'this'.
*/
"unsilence": function unsilence (type, func) {
var callbackCount,
evt,
index;
if (typeof type === "undefined") {
silenced = false;
return this;
}
if (typeof func === "undefined") {
addEvent(type).silenced = false;
return this;
}
evt = events[type];
if (typeof evt !== "undefined") {
callbackCount = evt.callbacks.length;
for (index = 0; index < callbackCount; index = index + 1) {
if (evt.callbacks[index].callbackFunction === func) {
evt.callbacks[index].silenced = false;
}
}
}
return this;
},
/**
* `Lock` prevents new callbacks from being registered. Similar to silencing, you can either lock an entire channel by calling `lock` with no arguments or lock a single event type by providing `lock` with that event's type.
*
* @method
* @param {String} [type] The name of the event to which no new callbacks shall be registerd. If no event name is specified, the whole channel will be locked.
* @returns {Object} The channel object. Or rather: 'this'. So be careful with rebinding 'this'.
*/
"lock": function lock (type) {
if (typeof type === "undefined") {
locked = true;
} else {
addEvent(type).locked = true;
}
return this;
},
/**
* `Unlock` allows callbacks from being added to a channel after it was locked. You can unlock a locked channel or just a single event type by using the optional parameter.
* Unlocking a channel that was already unlocked does not throw but instead just does nothing.
*
* @param {String} [type] The name of the event that shall accept new callbacks again. If no event type is given, the whole channel will be unlocked.
* @returns {Object} The channel object. Or rather: 'this'. So be careful with rebinding 'this'.
*/
"unlock": function unlock (type) {
if (typeof type === "undefined") {
locked = false;
} else {
addEvent(type).locked = false;
}
return this;
},
/**
* This lets you remove all event listeners from the message channel or from a specified event type.
* (Also sets `silenced` and `locked` to `false`).
*
* @param {String} [type] Optional: The name of the event whose callbacks shall be removed. If no event type is given, the whole channel will be reset.
* @returns {Object} The channel object. Or rather: 'this'. So be careful with rebinding 'this'.
*/
"reset": function reset (type) {
if (events !== null) {
if (typeof type === "undefined") {
events = { };
} else {
addEvent(type, true);
}
}
return this;
},
/**
* Returns whether the channel or an event type or a specific callback is silenced.
*
* @param {String} [type] Optional: The name of the event whose status is being requested.
* If no event type is specified, the whole channel's status will be returned.
* @param {Function} [func] Optional: The function whose status is being requested.
* If no function is specified the whole event type's status will be returned.
* @returns {Object} The channel object. Or rather: 'this'. So be careful with rebinding 'this'.
*/
"isSilenced": function isSilenced (type, func) {
var callbackCount,
channelSilenced,
evt,
index;
channelSilenced = false;
if (typeof type === "undefined" || silenced) {
channelSilenced = silenced;
} else if (events[type] && (typeof func === "undefined" || events[type].silenced)) {
channelSilenced = events[type].silenced;
} else {
evt = events[type];
if (typeof evt !== "undefined") {
callbackCount = evt.callbacks.length;
for (index = 0; index < callbackCount; index = index + 1) {
if (evt.callbacks[index].callbackFunction === func) {
channelSilenced = evt.callbacks[index].silenced;
break;
}
}
}
}
return channelSilenced;
},
/**
* Returns whether the channel or a specifc event type on this channel is locked.
*
* @param {String} [type] Optional: The name of the event whose status is being requested. If no event type is specified, the whole channel's status will be returned.
* @returns {Object} The channel object. Or rather: 'this'. So be careful with rebinding 'this'.
*/
"isLocked": function isLocked (type) {
var channelLocked;
if (typeof type === "undefined" || locked) {
channelLocked = locked;
} else {
channelLocked = (events[type] || false) && events[type].locked;
}
return channelLocked;
}
});
return channel;
};