-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathsrc_core_EventEmitter.js.html
477 lines (418 loc) · 15.1 KB
/
src_core_EventEmitter.js.html
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
472
473
474
475
476
477
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>src/core/EventEmitter.js - webduino-js</title>
<link rel="stylesheet" href="">
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
<link rel="stylesheet" href="../assets/css/main.css" id="site_styles">
<link rel="stylesheet" href="../assets/css/custom.css">
<link rel="stylesheet" href="../assets/css/lucid.css">
<link rel="stylesheet" href="../assets/vendor/bootstrap/css/bootstrap.css">
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png">
</head>
<body class="yui3-skin-sam">
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<h1 class="brand" style="padding: 10px 16px 10px; height: 20px; line-height: 20px; margin-left: 0;">
webduino-js
</h1>
<div class="nav">
<li class="divider-vertical"></li>
<li>
<p class="navbar-text">
API Docs for Version: <b>0.7.0</b>
</p>
</li>
</div>
<form class="navbar-form pull-right" style="line-height: 40px; height: 40px;">
<input style="margin-top: 0;" type="text" class="search-query" placeholder="Search for classes/modules..." data-obj='["classes/webduino.Board", "classes/webduino.EventEmitter", "classes/webduino.Module", "classes/webduino.module.ADXL345", "classes/webduino.module.Barcode", "classes/webduino.module.Button", "classes/webduino.module.Buzzer", "classes/webduino.module.Dht", "classes/webduino.module.HX711", "classes/webduino.module.IRLed", "classes/webduino.module.IRRecv", "classes/webduino.module.Led", "classes/webduino.module.Max7219", "classes/webduino.module.Photocell", "classes/webduino.module.RFID", "classes/webduino.module.RGBLed", "classes/webduino.module.Soil", "classes/webduino.module.Ultrasonic", "classes/webduino.Transport", "classes/webduino.transport.MqttTransport", "classes/webduino.transport.NodeMqttTransport"]'>
</form>
</div>
</div>
<div class="container">
<div class="row">
<div class="span3">
<div>
<div id="sidebar">
<div id="classes">
<ul id="api-classes" class="nav nav-list">
<li><a href="../classes/webduino.Board.html">webduino.Board</a></li>
<li><a href="../classes/webduino.EventEmitter.html">webduino.EventEmitter</a></li>
<li><a href="../classes/webduino.Module.html">webduino.Module</a></li>
<li><a href="../classes/webduino.module.ADXL345.html">webduino.module.ADXL345</a></li>
<li><a href="../classes/webduino.module.Barcode.html">webduino.module.Barcode</a></li>
<li><a href="../classes/webduino.module.Button.html">webduino.module.Button</a></li>
<li><a href="../classes/webduino.module.Buzzer.html">webduino.module.Buzzer</a></li>
<li><a href="../classes/webduino.module.Dht.html">webduino.module.Dht</a></li>
<li><a href="../classes/webduino.module.HX711.html">webduino.module.HX711</a></li>
<li><a href="../classes/webduino.module.IRLed.html">webduino.module.IRLed</a></li>
<li><a href="../classes/webduino.module.IRRecv.html">webduino.module.IRRecv</a></li>
<li><a href="../classes/webduino.module.Led.html">webduino.module.Led</a></li>
<li><a href="../classes/webduino.module.Max7219.html">webduino.module.Max7219</a></li>
<li><a href="../classes/webduino.module.Photocell.html">webduino.module.Photocell</a></li>
<li><a href="../classes/webduino.module.RFID.html">webduino.module.RFID</a></li>
<li><a href="../classes/webduino.module.RGBLed.html">webduino.module.RGBLed</a></li>
<li><a href="../classes/webduino.module.Soil.html">webduino.module.Soil</a></li>
<li><a href="../classes/webduino.module.Ultrasonic.html">webduino.module.Ultrasonic</a></li>
<li><a href="../classes/webduino.Transport.html">webduino.Transport</a></li>
<li><a href="../classes/webduino.transport.MqttTransport.html">webduino.transport.MqttTransport</a></li>
<li><a href="../classes/webduino.transport.NodeMqttTransport.html">webduino.transport.NodeMqttTransport</a></li>
</ul>
</div>
</div>
</div>
</div>
<div class="span9">
<form id="options-form" class="form-inline pull-right">
Show:
<label for="api-show-inherited" class="checkbox">
<input type="checkbox" id="api-show-inherited" checked>
Inherited
</label>
<label for="api-show-protected" class="checkbox">
<input type="checkbox" id="api-show-protected">
Protected
</label>
<label for="api-show-private" class="checkbox">
<input type="checkbox" id="api-show-private">
Private
</label>
<label for="api-show-deprecated" class="checkbox">
<input type="checkbox" id="api-show-deprecated">
Deprecated
</label>
</form>
<div class="apidocs">
<div id="docs-main">
<div class="content">
<div class="page-header">
<h1>src/core/EventEmitter.js <small>File</small></h1>
</div>
<div class="file">
<pre class="prettyprint linenums">
+(function (global, factory) {
if (typeof exports === 'undefined') {
factory(global.webduino || {});
} else {
module.exports = factory;
}
}(this, function (scope) {
'use strict';
// source:
// https://raw.githubusercontent.com/Gozala/events/master/events.js
var proto;
/**
* An event emitter in browser.
*
* @namespace webduino
* @class EventEmitter
* @constructor
*/
function EventEmitter() {
this._events = this._events || {};
this._maxListeners = this._maxListeners || undefined;
}
proto = EventEmitter.prototype;
proto._events = undefined;
proto._maxListeners = undefined;
// By default EventEmitters will print a warning if more than 10 listeners are
// added to it. This is a useful default which helps finding memory leaks.
/**
* Default maximum number of listeners (10).
*
* @property defaultMaxListeners
* @type {Number}
* @static
*/
EventEmitter.defaultMaxListeners = 10;
// Obviously not all Emitters should be limited to 10. This function allows
// that to be increased. Set to zero for unlimited.
/**
* Set maximum number of listeners that is allow to bind on an emitter.
*
* @method setMaxListeners
* @param {Number} n Number of listeners.
*/
proto.setMaxListeners = function (n) {
if (!isNumber(n) || n < 0 || isNaN(n))
throw TypeError('n must be a positive number');
this._maxListeners = n;
return this;
};
/**
* Emit an event of certain type.
*
* @method emit
* @param {String} type Event type.
* @param {Object} [object,...] Event object(s).
*/
proto.emit = function (type) {
var handler, len, args, i, listeners;
if (!this._events)
this._events = {};
// If there is no 'error' event listener then throw.
// EDIT: Do not throw unhandled 'error' in the browser. (mz)
// if (type === 'error') {
// if (!this._events.error ||
// (isObject(this._events.error) && !this._events.error.length)) {
// er = arguments[1];
// if (er instanceof Error) {
// throw er; // Unhandled 'error' event
// }
// throw TypeError('Uncaught, unspecified "error" event.');
// }
// }
handler = this._events[type];
if (isUndefined(handler))
return false;
if (isFunction(handler)) {
switch (arguments.length) {
// fast cases
case 1:
handler.call(this);
break;
case 2:
handler.call(this, arguments[1]);
break;
case 3:
handler.call(this, arguments[1], arguments[2]);
break;
// slower
default:
args = Array.prototype.slice.call(arguments, 1);
handler.apply(this, args);
}
} else if (isObject(handler)) {
args = Array.prototype.slice.call(arguments, 1);
listeners = handler.slice();
len = listeners.length;
for (i = 0; i < len; i++)
listeners[i].apply(this, args);
}
return true;
};
/**
* Add a listener for a certain type of event.
*
* @method addListener
* @param {String} type Event type.
* @param {Function} listener Event listener.
*/
proto.addListener = function (type, listener) {
var m;
if (!isFunction(listener))
throw TypeError('listener must be a function');
if (!this._events)
this._events = {};
// To avoid recursion in the case that type === "newListener"! Before
// adding it to the listeners, first emit "newListener".
if (this._events.newListener)
this.emit('newListener', type,
isFunction(listener.listener) ?
listener.listener : listener);
if (!this._events[type])
// Optimize the case of one listener. Don't need the extra array object.
this._events[type] = listener;
else if (isObject(this._events[type]))
// If we've already got an array, just append.
this._events[type].push(listener);
else
// Adding the second element, need to change to array.
this._events[type] = [this._events[type], listener];
// Check for listener leak
if (isObject(this._events[type]) && !this._events[type].warned) {
if (!isUndefined(this._maxListeners)) {
m = this._maxListeners;
} else {
m = EventEmitter.defaultMaxListeners;
}
if (m && m > 0 && this._events[type].length > m) {
this._events[type].warned = true;
console.error('(node) warning: possible EventEmitter memory ' +
'leak detected. %d listeners added. ' +
'Use emitter.setMaxListeners() to increase limit.',
this._events[type].length);
if (typeof console.trace === 'function') {
// not supported in IE 10
console.trace();
}
}
}
return this;
};
/**
* Alias for EventEmitter.addListener(type, listener)
*
* @method on
* @param {String} type Event type.
* @param {Function} listener Event listener.
*/
proto.on = proto.addListener;
/**
* Add a one-time listener for a certain type of event.
*
* @method once
* @param {String} type Event type.
* @param {Function} listener Event listener.
*/
proto.once = function (type, listener) {
if (!isFunction(listener))
throw TypeError('listener must be a function');
var fired = false;
function g() {
this.removeListener(type, g);
if (!fired) {
fired = true;
listener.apply(this, arguments);
}
}
g.listener = listener;
this.on(type, g);
return this;
};
/**
* Remove a listener for certain type of event.
*
* @method removeListener
* @param {String} type Event type.
* @param {Function} listener Event listener.
*/
// emits a 'removeListener' event iff the listener was removed
proto.removeListener = function (type, listener) {
var list, position, length, i;
if (!isFunction(listener))
throw TypeError('listener must be a function');
if (!this._events || !this._events[type])
return this;
list = this._events[type];
length = list.length;
position = -1;
if (list === listener ||
(isFunction(list.listener) && list.listener === listener)) {
delete this._events[type];
if (this._events.removeListener)
this.emit('removeListener', type, listener);
} else if (isObject(list)) {
for (i = length; i-- > 0;) {
if (list[i] === listener ||
(list[i].listener && list[i].listener === listener)) {
position = i;
break;
}
}
if (position < 0)
return this;
if (list.length === 1) {
list.length = 0;
delete this._events[type];
} else {
list.splice(position, 1);
}
if (this._events.removeListener)
this.emit('removeListener', type, listener);
}
return this;
};
/**
* Remove all listeners of certain type.
*
* @method removeAllListeners
* @param {String} type Event type.
*/
proto.removeAllListeners = function (type) {
var key, listeners;
if (!this._events)
return this;
// not listening for removeListener, no need to emit
if (!this._events.removeListener) {
if (arguments.length === 0)
this._events = {};
else if (this._events[type])
delete this._events[type];
return this;
}
// emit removeListener for all listeners on all events
if (arguments.length === 0) {
for (key in this._events) {
if (key === 'removeListener') continue;
this.removeAllListeners(key);
}
this.removeAllListeners('removeListener');
this._events = {};
return this;
}
listeners = this._events[type];
if (isFunction(listeners)) {
this.removeListener(type, listeners);
} else if (listeners) {
// LIFO order
while (listeners.length)
this.removeListener(type, listeners[listeners.length - 1]);
}
delete this._events[type];
return this;
};
/**
* Return the listener list bound to certain type of event.
*
* @method listeners
* @param {String} type Evnet type.
*/
proto.listeners = function (type) {
var ret;
if (!this._events || !this._events[type])
ret = [];
else if (isFunction(this._events[type]))
ret = [this._events[type]];
else
ret = this._events[type].slice();
return ret;
};
/**
* Count the number of listeners of an emitter.
*
* @method listenerCount
* @param {webduino.EventEmitter} emitter The EventEmitter instance to count.
* @param {String} type Event type.
* @static
*/
EventEmitter.listenerCount = function (emitter, type) {
var ret;
if (!emitter._events || !emitter._events[type])
ret = 0;
else if (isFunction(emitter._events[type]))
ret = 1;
else
ret = emitter._events[type].length;
return ret;
};
function isFunction(arg) {
return typeof arg === 'function';
}
function isNumber(arg) {
return typeof arg === 'number';
}
function isObject(arg) {
return typeof arg === 'object' && arg !== null;
}
function isUndefined(arg) {
return arg === void 0;
}
scope.EventEmitter = EventEmitter;
}));
</pre>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="../assets/vendor/jquery/jquery-1.8.2.min.js"></script>
<script src="../assets/vendor/bootstrap/js/bootstrap.js"></script>
<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script src="../assets/js/yuidoc-bootstrap.js"></script>
<script>prettyPrint();</script>
</body>
</html>