-
Notifications
You must be signed in to change notification settings - Fork 4
Add "array" and "error" options #1
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ var Bluebird = require('bluebird') | |
|
||
// Faster `Function.bind()`. | ||
function bind (fn, ctx) { | ||
return function bindedFunction () { | ||
return function boundFunction () { | ||
return fn.apply(ctx, arguments) | ||
} | ||
} | ||
|
@@ -21,8 +21,10 @@ var toArray = Array.from || (function (slice) { | |
|
||
// =================================================================== | ||
|
||
function eventToPromise (emitter, event, opts) { | ||
var ignoreErrors = opts && opts.ignoreErrors | ||
function eventToPromise (emitter, event, _opts) { | ||
var opts = _opts || {} | ||
var ignoreErrors = opts.ignoreErrors | ||
var errorEvent = opts.error || 'error' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that could work (assuming it doesn't replace There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could do, if you don't mind a breaking change :-) I personally prefer an explicit option, though, even if it is slightly "longer", since it means I don't have to add a comment explaining that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough, lets keep it like that. |
||
|
||
return new Bluebird(function (resolve, reject) { | ||
var addListener = | ||
|
@@ -44,7 +46,7 @@ function eventToPromise (emitter, event, opts) { | |
|
||
cleanUp = function cleanUp () { | ||
removeListener(event, eventListener) | ||
errorListener && removeListener('error', errorListener) | ||
errorListener && removeListener(errorEvent, errorListener) | ||
} | ||
} else { | ||
cleanUp = noop | ||
|
@@ -53,20 +55,28 @@ function eventToPromise (emitter, event, opts) { | |
function eventListener () { | ||
cleanUp() | ||
|
||
if (arguments.length < 2) { | ||
resolve(arguments[0]) | ||
} else { | ||
resolve(toArray(arguments)) | ||
if ('array' in opts) { | ||
if (opts.array) { | ||
resolve(toArray(arguments)) | ||
} else { | ||
resolve(arguments[0]) | ||
} | ||
} else { // legacy behaviour for backwards compatibility | ||
if (arguments.length < 2) { | ||
resolve(arguments[0]) | ||
} else { | ||
resolve(toArray(arguments)) | ||
} | ||
} | ||
} | ||
emitter.on(event, eventListener) | ||
addListener(event, eventListener) | ||
|
||
if (!ignoreErrors) { | ||
errorListener = function errorListener (error) { | ||
cleanUp() | ||
reject(error) | ||
} | ||
emitter.on('error', errorListener) | ||
addListener(errorEvent, errorListener) | ||
} | ||
}).bind(emitter) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,8 +17,6 @@ var EventEmitter = require('events').EventEmitter | |
// =================================================================== | ||
|
||
// TODO: | ||
// - Forcing the parameters to be forwarded in an array for | ||
// consistency and predictability. | ||
// - Maybe handling multiple events. | ||
// - Tests the different way to add/remove an event listener. | ||
|
||
|
@@ -81,6 +79,57 @@ describe('event-to-promise', function () { | |
|
||
// ----------------------------------------------------------------- | ||
|
||
describe('array option', function () { | ||
it('array: false: fowards the first parameter', function () { | ||
var param0 = {} | ||
var param1 = {} | ||
|
||
var promise = eventToPromise(emitter, 'foo', { array: false }) | ||
emitter.emit('foo', param0, param1) | ||
|
||
return promise.then(function () { | ||
expect(arguments).to.have.length(1) | ||
expect(arguments[0]).to.equal(param0) | ||
}) | ||
}) | ||
|
||
it('array: true: fowards all parameters as an array', function () { | ||
var param0 = {} | ||
var param1 = {} | ||
|
||
var promise = eventToPromise(emitter, 'foo', { array: true }) | ||
emitter.emit('foo', param0, param1) | ||
|
||
return promise.then(function (values) { | ||
expect(values).to.have.length(2) | ||
expect(values[0]).to.equal(param0) | ||
expect(values[1]).to.equal(param1) | ||
}) | ||
}) | ||
}) | ||
|
||
// ----------------------------------------------------------------- | ||
|
||
describe('error option', function () { | ||
it('handles a custom error event', function () { | ||
var error = new Error() | ||
|
||
var promise = eventToPromise(emitter, 'foo', { error: 'test-error' }) | ||
emitter.emit('test-error', error) | ||
|
||
return promise.then( | ||
function () { | ||
expect('should not be executed').to.be.true | ||
}, | ||
function (err) { | ||
expect(err).to.equal(error) | ||
} | ||
) | ||
}) | ||
}) | ||
|
||
// ----------------------------------------------------------------- | ||
|
||
it('handles error event', function () { | ||
var error = new Error() | ||
|
||
|
@@ -101,7 +150,7 @@ describe('event-to-promise', function () { | |
var error = new Error() | ||
|
||
// Node requires at least one error listener. | ||
emitter.on('error', function noop () {}) | ||
emitter.once('error', function noop () {}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's called once, after which we have no further use for it. Why would you not use If there was a bug in this library that caused the error event to be fired twice, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean a bug in the test, because the library does not emit any events. But fair point. |
||
|
||
var promise = eventToPromise(emitter, 'foo', { | ||
ignoreErrors: true | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks :)