diff --git a/lib/.basestar.js.swp b/lib/.basestar.js.swp new file mode 100644 index 0000000..0026ea0 Binary files /dev/null and b/lib/.basestar.js.swp differ diff --git a/lib/basestar.js b/lib/basestar.js index ff49806..e6794c7 100644 --- a/lib/basestar.js +++ b/lib/basestar.js @@ -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)); } }; diff --git a/spec/lib/basestar.spec.js b/spec/lib/basestar.spec.js index 769df22..10511a5 100644 --- a/spec/lib/basestar.spec.js +++ b/spec/lib/basestar.spec.js @@ -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() {