Adds error handling to basetar#respond.
This commit is contained in:
parent
527b0febed
commit
ec4dde7448
Binary file not shown.
|
@ -40,13 +40,17 @@ Basestar.prototype.proxyMethods = Utils.proxyFunctionsToObject;
|
|||
// ...data - additional arguments to be passed to both event/callback
|
||||
//
|
||||
// Returns nothing
|
||||
Basestar.prototype.respond = function(event, callback) {
|
||||
var args = Array.prototype.slice.call(arguments, 2);
|
||||
Basestar.prototype.respond = function(event, callback, err) {
|
||||
var args = Array.prototype.slice.call(arguments, 3);
|
||||
|
||||
this.emit.apply(this, [event].concat(args));
|
||||
if (!!err) {
|
||||
this.emit('error', err);
|
||||
} else {
|
||||
this.emit.apply(this, [event].concat(args));
|
||||
}
|
||||
|
||||
if (typeof callback === "function") {
|
||||
callback.apply(this, args);
|
||||
callback.apply(this, [err].concat(args));
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -52,7 +52,6 @@ describe("Basestar", function() {
|
|||
|
||||
describe("#respond", function() {
|
||||
var listener, callback, child;
|
||||
|
||||
var Child = function Child() {};
|
||||
|
||||
Utils.subclass(Child, Basestar);
|
||||
|
@ -65,16 +64,31 @@ describe("Basestar", function() {
|
|||
|
||||
child.on("event", listener);
|
||||
|
||||
child.respond("event", callback, "arg1", 2, { three: true });
|
||||
child.respond("event", callback, null, "arg1", 2, { three: true });
|
||||
});
|
||||
|
||||
it("triggers the callback with all additional arguments", function() {
|
||||
expect(callback).to.be.calledWith("arg1", 2, { three: true });
|
||||
expect(callback).to.be.calledWith(null, "arg1", 2, { three: true });
|
||||
});
|
||||
|
||||
it("emits an event with all additional arguments", function() {
|
||||
expect(listener).to.be.calledWith("arg1", 2, { three: true });
|
||||
});
|
||||
|
||||
context("when err is not null", function() {
|
||||
var errListener, err;
|
||||
|
||||
beforeEach(function() {
|
||||
errListener = spy();
|
||||
child = new Child();
|
||||
child.on("error", errListener);
|
||||
child.respond("event", callback, "Error on event!", "arg1", 2, { three: true });
|
||||
});
|
||||
|
||||
it ("emits an error event", function() {
|
||||
expect(errListener).to.be.calledWith("Error on event!");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("#defineEvent", function() {
|
||||
|
|
Loading…
Reference in New Issue