-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
63 lines (53 loc) · 1.53 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
'use strict';
/**
* Returns a function that when invoked executes all the listeners of the
* given event with the given arguments.
*
* @returns {Function} The function that emits all the things.
* @api public
*/
module.exports = function emits() {
var self = this
, parser;
for (var i = 0, l = arguments.length, args = new Array(l); i < l; i++) {
args[i] = arguments[i];
}
//
// If the last argument is a function, assume that it's a parser.
//
if ('function' !== typeof args[l - 1]) return function emitter() {
for (var i = 0, l = arguments.length, arg = new Array(l); i < l; i++) {
arg[i] = arguments[i];
}
return self.emit.apply(self, args.concat(arg));
};
parser = args.pop();
/**
* The actual function that emits the given event. It returns a boolean
* indicating if the event was emitted.
*
* @returns {Boolean}
* @api public
*/
return function emitter() {
for (var i = 0, l = arguments.length, arg = new Array(l + 1); i < l; i++) {
arg[i + 1] = arguments[i];
}
/**
* Async completion method for the parser.
*
* @param {Error} err Optional error when parsing failed.
* @param {Mixed} returned Emit instructions.
* @api private
*/
arg[0] = function next(err, returned) {
if (err) return self.emit('error', err);
arg = returned === undefined
? arg.slice(1) : returned === null
? [] : returned;
self.emit.apply(self, args.concat(arg));
};
parser.apply(self, arg);
return true;
};
};