eventemitter-ex
Advanced tools
Comparing version 1.5.0 to 1.6.0
@@ -70,3 +70,3 @@ 'use strict'; | ||
if (typeof ee.onAllExcept === 'function') { | ||
if (isFunction(ee.onAllExcept)) { | ||
// This is an EventEmitterEx | ||
@@ -113,23 +113,36 @@ except.push(function (/* arguments */) { | ||
this.on('end', function (/* arguments */) { | ||
var result; | ||
try { | ||
var endArgs = arguments; | ||
result = mapArgs.map(function (f) { | ||
var res = f.apply(eex, endArgs); | ||
return Array.isArray(res) ? res : [res]; | ||
}); | ||
// FIXME: wait for all promises to resolve/reject before emitting end/error | ||
// flatten the array | ||
Promise.all(concat.apply([], result)).then( | ||
function (res) { | ||
res.unshift('end'); | ||
eex.emit.apply(eex, res); | ||
}, | ||
function (err) { | ||
eex.emit('error', err); | ||
}); | ||
} catch (err) { | ||
eex.emit('error', err); | ||
return; | ||
var result = [], firstError, len = mapArgs.length, lenLoop = len; | ||
var endArgs = arguments; | ||
for (var i = 0; i < lenLoop; i++) { | ||
new Promise(invokeFunc.bind(null, mapArgs[i])) | ||
.then(onResolved.bind(null, i), onRejected); | ||
} | ||
function invokeFunc (func, resolve) { | ||
resolve(func.apply(eex, endArgs)); | ||
} | ||
function onResolved (idx, value) { | ||
result[idx] = Array.isArray(value) ? value : [value]; | ||
maybeFinish(); | ||
} | ||
function onRejected (reason) { | ||
if (! isError(firstError)) { | ||
firstError = reason; | ||
} | ||
maybeFinish(); | ||
} | ||
function maybeFinish () { | ||
len--; | ||
if (! len) { | ||
if (isError(firstError)) { | ||
eex.emit('error', firstError); | ||
} else { | ||
// flatten the array | ||
eex.emit.apply(eex, concat.apply(['end'], result)); | ||
} | ||
} | ||
} | ||
}); | ||
@@ -164,7 +177,9 @@ | ||
if (err === null) { | ||
if (isError(err)) { | ||
if (! firstError) { | ||
firstError = slice(arguments, 1); | ||
} | ||
result[position] = []; | ||
} else { | ||
result[position] = slice(arguments, 2); | ||
} else { | ||
if (! firstError) firstError = slice(arguments, 1); | ||
result[position] = []; | ||
} | ||
@@ -215,5 +230,7 @@ len--; | ||
function errorListener (position, err) { | ||
function errorListener (position/* arguments */) { | ||
checkUsage(position); | ||
firstError = firstError || err; | ||
if (! firstError) { | ||
firstError = slice(arguments, 1); | ||
} | ||
result[position] = []; | ||
@@ -227,3 +244,4 @@ maybeNext(); | ||
if (firstError) { | ||
eex.emit('error', firstError); | ||
firstError.unshift('error'); | ||
eex.emit.apply(eex, firstError); | ||
} else { | ||
@@ -259,3 +277,3 @@ // flatten the array | ||
EventEmitterEx.listenerCount = function listenerCount (eex, type) { | ||
return (typeof eex.listenerCountOnAll === 'function' ? eex.listenerCountOnAll(type) : 0) + | ||
return (isFunction(eex.listenerCountOnAll) ? eex.listenerCountOnAll(type) : 0) + | ||
EE.listenerCount(eex, type); | ||
@@ -308,5 +326,14 @@ }; | ||
function isFunction (f) { | ||
return typeof f === 'function'; | ||
} | ||
function assertIsFunction (f) { | ||
if (typeof f !== 'function') | ||
if (! isFunction(f)) { | ||
throw new TypeError('Argument must be a function. Got ' + typeof f); | ||
} | ||
} | ||
function isError (e) { | ||
return e !== null && typeof e !== 'undefined'; | ||
} |
{ | ||
"name": "eventemitter-ex", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"description": "EventEmitter extensions", | ||
@@ -9,3 +9,3 @@ "main": "EventEmitterEx.js", | ||
"test": "jshint --exclude-path=.gitignore . && mocha test", | ||
"coveralls": "istanbul cover _mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage" | ||
"coveralls": "istanbul cover _mocha --report lcovonly && cat ./coverage/lcov.info | coveralls" | ||
}, | ||
@@ -12,0 +12,0 @@ "repository": { |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
76156
19
269
1