3 * Copyright(c) 2014 Jonathan Ong
14 module.exports = first
17 * Get the first event in a set of event emitters and event pairs.
19 * @param {array} stuff
20 * @param {function} done
24 function first(stuff, done) {
25 if (!Array.isArray(stuff))
26 throw new TypeError('arg must be an array of [ee, events...] arrays')
30 for (var i = 0; i < stuff.length; i++) {
33 if (!Array.isArray(arr) || arr.length < 2)
34 throw new TypeError('each array member must be [ee, events...]')
38 for (var j = 1; j < arr.length; j++) {
40 var fn = listener(event, callback)
42 // listen to the event
44 // push this listener to the list of cleanups
55 done.apply(null, arguments)
60 for (var i = 0; i < cleanups.length; i++) {
62 x.ee.removeListener(x.event, x.fn)
70 thunk.cancel = cleanup
76 * Create the event listener.
80 function listener(event, done) {
81 return function onevent(arg1) {
82 var args = new Array(arguments.length)
84 var err = event === 'error'
88 // copy args to prevent arguments escaping scope
89 for (var i = 0; i < args.length; i++) {
90 args[i] = arguments[i]
93 done(err, ee, event, args)