annotate scripts/minifier/otr/dep/eventemitter.js @ 32:a3f262d5b594

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