comparison scripts/minifier/otr/dep/eventemitter.js @ 12:1596660ddf72

Add minifier script for otr.js and its dependencies
author souliane <souliane@mailoo.org>
date Wed, 03 Sep 2014 19:38:05 +0200
parents
children
comparison
equal deleted inserted replaced
11:4920c8da790b 12:1596660ddf72
1 /*!
2 * EventEmitter v4.2.3 - git.io/ee
3 * Oliver Caldwell
4 * MIT license
5 * @preserve
6 */
7
8 (function () {
9 'use strict';
10
11 /**
12 * Class for managing events.
13 * Can be extended to provide event functionality in other classes.
14 *
15 * @class EventEmitter Manages event registering and emitting.
16 */
17 function EventEmitter() {}
18
19 // Shortcuts to improve speed and size
20
21 // Easy access to the prototype
22 var proto = EventEmitter.prototype;
23
24 /**
25 * Finds the index of the listener for the event in it's storage array.
26 *
27 * @param {Function[]} listeners Array of listeners to search through.
28 * @param {Function} listener Method to look for.
29 * @return {Number} Index of the specified listener, -1 if not found
30 * @api private
31 */
32 function indexOfListener(listeners, listener) {
33 var i = listeners.length;
34 while (i--) {
35 if (listeners[i].listener === listener) {
36 return i;
37 }
38 }
39
40 return -1;
41 }
42
43 /**
44 * Alias a method while keeping the context correct, to allow for overwriting of target method.
45 *
46 * @param {String} name The name of the target method.
47 * @return {Function} The aliased method
48 * @api private
49 */
50 function alias(name) {
51 return function aliasClosure() {
52 return this[name].apply(this, arguments);
53 };
54 }
55
56 /**
57 * Returns the listener array for the specified event.
58 * Will initialise the event object and listener arrays if required.
59 * Will return an object if you use a regex search. The object contains keys for each matched event. So /ba[rz]/ might return an object containing bar and baz. But only if you have either defined them with defineEvent or added some listeners to them.
60 * Each property in the object response is an array of listener functions.
61 *
62 * @param {String|RegExp} evt Name of the event to return the listeners from.
63 * @return {Function[]|Object} All listener functions for the event.
64 */
65 proto.getListeners = function getListeners(evt) {
66 var events = this._getEvents();
67 var response;
68 var key;
69
70 // Return a concatenated array of all matching events if
71 // the selector is a regular expression.
72 if (typeof evt === 'object') {
73 response = {};
74 for (key in events) {
75 if (events.hasOwnProperty(key) && evt.test(key)) {
76 response[key] = events[key];
77 }
78 }
79 }
80 else {
81 response = events[evt] || (events[evt] = []);
82 }
83
84 return response;
85 };
86
87 /**
88 * Takes a list of listener objects and flattens it into a list of listener functions.
89 *
90 * @param {Object[]} listeners Raw listener objects.
91 * @return {Function[]} Just the listener functions.
92 */
93 proto.flattenListeners = function flattenListeners(listeners) {
94 var flatListeners = [];
95 var i;
96
97 for (i = 0; i < listeners.length; i += 1) {
98 flatListeners.push(listeners[i].listener);
99 }
100
101 return flatListeners;
102 };
103
104 /**
105 * Fetches the requested listeners via getListeners but will always return the results inside an object. This is mainly for internal use but others may find it useful.
106 *
107 * @param {String|RegExp} evt Name of the event to return the listeners from.
108 * @return {Object} All listener functions for an event in an object.
109 */
110 proto.getListenersAsObject = function getListenersAsObject(evt) {
111 var listeners = this.getListeners(evt);
112 var response;
113
114 if (listeners instanceof Array) {
115 response = {};
116 response[evt] = listeners;
117 }
118
119 return response || listeners;
120 };
121
122 /**
123 * Adds a listener function to the specified event.
124 * The listener will not be added if it is a duplicate.
125 * If the listener returns true then it will be removed after it is called.
126 * If you pass a regular expression as the event name then the listener will be added to all events that match it.
127 *
128 * @param {String|RegExp} evt Name of the event to attach the listener to.
129 * @param {Function} listener Method to be called when the event is emitted. If the function returns true then it will be removed after calling.
130 * @return {Object} Current instance of EventEmitter for chaining.
131 */
132 proto.addListener = function addListener(evt, listener) {
133 var listeners = this.getListenersAsObject(evt);
134 var listenerIsWrapped = typeof listener === 'object';
135 var key;
136
137 for (key in listeners) {
138 if (listeners.hasOwnProperty(key) && indexOfListener(listeners[key], listener) === -1) {
139 listeners[key].push(listenerIsWrapped ? listener : {
140 listener: listener,
141 once: false
142 });
143 }
144 }
145
146 return this;
147 };
148
149 /**
150 * Alias of addListener
151 */
152 proto.on = alias('addListener');
153
154 /**
155 * Semi-alias of addListener. It will add a listener that will be
156 * automatically removed after it's first execution.
157 *
158 * @param {String|RegExp} evt Name of the event to attach the listener to.
159 * @param {Function} listener Method to be called when the event is emitted. If the function returns true then it will be removed after calling.
160 * @return {Object} Current instance of EventEmitter for chaining.
161 */
162 proto.addOnceListener = function addOnceListener(evt, listener) {
163 return this.addListener(evt, {
164 listener: listener,
165 once: true
166 });
167 };
168
169 /**
170 * Alias of addOnceListener.
171 */
172 proto.once = alias('addOnceListener');
173
174 /**
175 * Defines an event name. This is required if you want to use a regex to add a listener to multiple events at once. If you don't do this then how do you expect it to know what event to add to? Should it just add to every possible match for a regex? No. That is scary and bad.
176 * You need to tell it what event names should be matched by a regex.
177 *
178 * @param {String} evt Name of the event to create.
179 * @return {Object} Current instance of EventEmitter for chaining.
180 */
181 proto.defineEvent = function defineEvent(evt) {
182 this.getListeners(evt);
183 return this;
184 };
185
186 /**
187 * Uses defineEvent to define multiple events.
188 *
189 * @param {String[]} evts An array of event names to define.
190 * @return {Object} Current instance of EventEmitter for chaining.
191 */
192 proto.defineEvents = function defineEvents(evts) {
193 for (var i = 0; i < evts.length; i += 1) {
194 this.defineEvent(evts[i]);
195 }
196 return this;
197 };
198
199 /**
200 * Removes a listener function from the specified event.
201 * When passed a regular expression as the event name, it will remove the listener from all events that match it.
202 *
203 * @param {String|RegExp} evt Name of the event to remove the listener from.
204 * @param {Function} listener Method to remove from the event.
205 * @return {Object} Current instance of EventEmitter for chaining.
206 */
207 proto.removeListener = function removeListener(evt, listener) {
208 var listeners = this.getListenersAsObject(evt);
209 var index;
210 var key;
211
212 for (key in listeners) {
213 if (listeners.hasOwnProperty(key)) {
214 index = indexOfListener(listeners[key], listener);
215
216 if (index !== -1) {
217 listeners[key].splice(index, 1);
218 }
219 }
220 }
221
222 return this;
223 };
224
225 /**
226 * Alias of removeListener
227 */
228 proto.off = alias('removeListener');
229
230 /**
231 * Adds listeners in bulk using the manipulateListeners method.
232 * If you pass an object as the second argument you can add to multiple events at once. The object should contain key value pairs of events and listeners or listener arrays. You can also pass it an event name and an array of listeners to be added.
233 * You can also pass it a regular expression to add the array of listeners to all events that match it.
234 * Yeah, this function does quite a bit. That's probably a bad thing.
235 *
236 * @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to add to multiple events at once.
237 * @param {Function[]} [listeners] An optional array of listener functions to add.
238 * @return {Object} Current instance of EventEmitter for chaining.
239 */
240 proto.addListeners = function addListeners(evt, listeners) {
241 // Pass through to manipulateListeners
242 return this.manipulateListeners(false, evt, listeners);
243 };
244
245 /**
246 * Removes listeners in bulk using the manipulateListeners method.
247 * If you pass an object as the second argument you can remove from multiple events at once. The object should contain key value pairs of events and listeners or listener arrays.
248 * You can also pass it an event name and an array of listeners to be removed.
249 * You can also pass it a regular expression to remove the listeners from all events that match it.
250 *
251 * @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to remove from multiple events at once.
252 * @param {Function[]} [listeners] An optional array of listener functions to remove.
253 * @return {Object} Current instance of EventEmitter for chaining.
254 */
255 proto.removeListeners = function removeListeners(evt, listeners) {
256 // Pass through to manipulateListeners
257 return this.manipulateListeners(true, evt, listeners);
258 };
259
260 /**
261 * Edits listeners in bulk. The addListeners and removeListeners methods both use this to do their job. You should really use those instead, this is a little lower level.
262 * The first argument will determine if the listeners are removed (true) or added (false).
263 * If you pass an object as the second argument you can add/remove from multiple events at once. The object should contain key value pairs of events and listeners or listener arrays.
264 * You can also pass it an event name and an array of listeners to be added/removed.
265 * You can also pass it a regular expression to manipulate the listeners of all events that match it.
266 *
267 * @param {Boolean} remove True if you want to remove listeners, false if you want to add.
268 * @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to add/remove from multiple events at once.
269 * @param {Function[]} [listeners] An optional array of listener functions to add/remove.
270 * @return {Object} Current instance of EventEmitter for chaining.
271 */
272 proto.manipulateListeners = function manipulateListeners(remove, evt, listeners) {
273 var i;
274 var value;
275 var single = remove ? this.removeListener : this.addListener;
276 var multiple = remove ? this.removeListeners : this.addListeners;
277
278 // If evt is an object then pass each of it's properties to this method
279 if (typeof evt === 'object' && !(evt instanceof RegExp)) {
280 for (i in evt) {
281 if (evt.hasOwnProperty(i) && (value = evt[i])) {
282 // Pass the single listener straight through to the singular method
283 if (typeof value === 'function') {
284 single.call(this, i, value);
285 }
286 else {
287 // Otherwise pass back to the multiple function
288 multiple.call(this, i, value);
289 }
290 }
291 }
292 }
293 else {
294 // So evt must be a string
295 // And listeners must be an array of listeners
296 // Loop over it and pass each one to the multiple method
297 i = listeners.length;
298 while (i--) {
299 single.call(this, evt, listeners[i]);
300 }
301 }
302
303 return this;
304 };
305
306 /**
307 * Removes all listeners from a specified event.
308 * If you do not specify an event then all listeners will be removed.
309 * That means every event will be emptied.
310 * You can also pass a regex to remove all events that match it.
311 *
312 * @param {String|RegExp} [evt] Optional name of the event to remove all listeners for. Will remove from every event if not passed.
313 * @return {Object} Current instance of EventEmitter for chaining.
314 */
315 proto.removeEvent = function removeEvent(evt) {
316 var type = typeof evt;
317 var events = this._getEvents();
318 var key;
319
320 // Remove different things depending on the state of evt
321 if (type === 'string') {
322 // Remove all listeners for the specified event
323 delete events[evt];
324 }
325 else if (type === 'object') {
326 // Remove all events matching the regex.
327 for (key in events) {
328 if (events.hasOwnProperty(key) && evt.test(key)) {
329 delete events[key];
330 }
331 }
332 }
333 else {
334 // Remove all listeners in all events
335 delete this._events;
336 }
337
338 return this;
339 };
340
341 /**
342 * Emits an event of your choice.
343 * When emitted, every listener attached to that event will be executed.
344 * If you pass the optional argument array then those arguments will be passed to every listener upon execution.
345 * Because it uses `apply`, your array of arguments will be passed as if you wrote them out separately.
346 * So they will not arrive within the array on the other side, they will be separate.
347 * You can also pass a regular expression to emit to all events that match it.
348 *
349 * @param {String|RegExp} evt Name of the event to emit and execute listeners for.
350 * @param {Array} [args] Optional array of arguments to be passed to each listener.
351 * @return {Object} Current instance of EventEmitter for chaining.
352 */
353 proto.emitEvent = function emitEvent(evt, args) {
354 var listeners = this.getListenersAsObject(evt);
355 var listener;
356 var i;
357 var key;
358 var response;
359
360 for (key in listeners) {
361 if (listeners.hasOwnProperty(key)) {
362 i = listeners[key].length;
363
364 while (i--) {
365 // If the listener returns true then it shall be removed from the event
366 // The function is executed either with a basic call or an apply if there is an args array
367 listener = listeners[key][i];
368
369 if (listener.once === true) {
370 this.removeListener(evt, listener.listener);
371 }
372
373 response = listener.listener.apply(this, args || []);
374
375 if (response === this._getOnceReturnValue()) {
376 this.removeListener(evt, listener.listener);
377 }
378 }
379 }
380 }
381
382 return this;
383 };
384
385 /**
386 * Alias of emitEvent
387 */
388 proto.trigger = alias('emitEvent');
389
390 /**
391 * Subtly different from emitEvent in that it will pass its arguments on to the listeners, as opposed to taking a single array of arguments to pass on.
392 * As with emitEvent, you can pass a regex in place of the event name to emit to all events that match it.
393 *
394 * @param {String|RegExp} evt Name of the event to emit and execute listeners for.
395 * @param {...*} Optional additional arguments to be passed to each listener.
396 * @return {Object} Current instance of EventEmitter for chaining.
397 */
398 proto.emit = function emit(evt) {
399 var args = Array.prototype.slice.call(arguments, 1);
400 return this.emitEvent(evt, args);
401 };
402
403 /**
404 * Sets the current value to check against when executing listeners. If a
405 * listeners return value matches the one set here then it will be removed
406 * after execution. This value defaults to true.
407 *
408 * @param {*} value The new value to check for when executing listeners.
409 * @return {Object} Current instance of EventEmitter for chaining.
410 */
411 proto.setOnceReturnValue = function setOnceReturnValue(value) {
412 this._onceReturnValue = value;
413 return this;
414 };
415
416 /**
417 * Fetches the current value to check against when executing listeners. If
418 * the listeners return value matches this one then it should be removed
419 * automatically. It will return true by default.
420 *
421 * @return {*|Boolean} The current value to check for or the default, true.
422 * @api private
423 */
424 proto._getOnceReturnValue = function _getOnceReturnValue() {
425 if (this.hasOwnProperty('_onceReturnValue')) {
426 return this._onceReturnValue;
427 }
428 else {
429 return true;
430 }
431 };
432
433 /**
434 * Fetches the events object and creates one if required.
435 *
436 * @return {Object} The events storage object.
437 * @api private
438 */
439 proto._getEvents = function _getEvents() {
440 return this._events || (this._events = {});
441 };
442
443 // Expose the class either via AMD, CommonJS or the global object
444 if (typeof define === 'function' && define.amd) {
445 define(function () {
446 return EventEmitter;
447 });
448 }
449 else if (typeof module === 'object' && module.exports){
450 module.exports = EventEmitter;
451 }
452 else {
453 this.EventEmitter = EventEmitter;
454 }
455 }.call(this));