Permalink
Browse files

Listener error callback now part of options argument to `Publisher` c…

…onstructor.

* Previously set through `publisher.setSubscriberErrorCallback(...)`.
* Now: `new Publisher({ onListenerError: ... })`.
* New options test suite; listener error callback test moved there.
  • Loading branch information...
1 parent a53c6c7 commit ecab43a2c52a754a256f06cb5afd72d5404ace7e @domenic committed Jan 26, 2012
View
@@ -15,18 +15,38 @@ Object.keys(listenerHelpers).forEach(function (functionName) {
exports[functionName] = listenerHelpers[functionName];
});
-exports.Publisher = function () {
+function normalizeOptions(options) {
+ if (typeof options !== "object" || options === null) {
+ throw new TypeError("options argument must be an object.");
+ }
+
+ if (options.onListenerError === undefined) {
+ options.onListenerError = function () { };
+ }
+
+ if (typeof options.onListenerError !== "function") {
+ throw new TypeError("options.onListenerError must be an object.");
+ }
+
+ return options;
+}
+
+exports.Publisher = function (options) {
var that = this;
var normalListeners = dict();
var oneTimeListeners = dict();
- var onSubscriberError = function () { };
+
+ if (options === undefined) {
+ options = {};
+ }
+ normalizeOptions(options);
function callListener(listener, args) {
try {
listener.apply(null, args);
} catch (e) {
- onSubscriberError(e);
+ options.onListenerError(e);
}
}
@@ -37,11 +57,6 @@ exports.Publisher = function () {
});
}
- // TODO constructor injection instead of setter injection?
- that.setSubscriberErrorCallback = function (newOnSubscriberError) {
- onSubscriberError = newOnSubscriberError;
- };
-
that.publish = function (eventName, args) {
if (typeof eventName !== "string") {
throw new TypeError("eventName argument must be a string.");
@@ -6,7 +6,6 @@ Publisher = require("../lib/pubit").Publisher
describe "Publisher/emitter in unusual situations", ->
publisher = null
emitter = null
- fakeError = new Error("OMG it's an error")
beforeEach ->
publisher = new Publisher()
@@ -49,7 +48,7 @@ describe "Publisher/emitter in unusual situations", ->
beforeEach ->
normalListener1 = sinon.spy()
normalListener2 = sinon.spy()
- errorThrowingListener = sinon.spy(-> throw fakeError)
+ errorThrowingListener = sinon.spy(-> throw new Error)
emitter.on("eventName", normalListener1)
emitter.on("eventName", errorThrowingListener)
@@ -62,14 +61,6 @@ describe "Publisher/emitter in unusual situations", ->
sinon.assert.calledOnce(normalListener2)
sinon.assert.calledOnce(errorThrowingListener)
- it "should deliver the original error to the subscriber error callback when the event is published", ->
- onSubscriberError = sinon.spy()
- publisher.setSubscriberErrorCallback(onSubscriberError)
-
- publisher.publish("eventName")
-
- sinon.assert.calledWithExactly(onSubscriberError, fakeError)
-
it 'gracefully deals with events named "hasOwnProperty"', ->
listener = sinon.spy()
View
@@ -0,0 +1,34 @@
+sinon = require("sinon")
+expect = require("chai").expect
+
+Publisher = require("../lib/pubit").Publisher
+
+describe "Publisher options", ->
+ publisher = null
+ emitter = null
+
+ describe "onListenerError", ->
+ onListenerError = null
+
+ beforeEach ->
+ onListenerError = sinon.spy()
+ publisher = new Publisher(onListenerError: onListenerError)
+ emitter = publisher.emitter
+
+ it "should deliver errors thrown by listeners to the supplied callback", ->
+ error = new Error("Boo")
+ emitter.on("eventName", -> throw error)
+
+ publisher.publish("eventName")
+
+ sinon.assert.calledWith(onListenerError, error)
+
+ it "should be validated", ->
+ expect(-> new Publisher(null)).to.throw(TypeError)
+ expect(-> new Publisher(5)).to.throw(TypeError)
+ expect(-> new Publisher("hi")).to.throw(TypeError)
+ expect(-> new Publisher(->)).to.throw(TypeError)
+
+ expect(-> new Publisher(onListenerError: null)).to.throw(TypeError)
+ expect(-> new Publisher(onListenerError: {})).to.throw(TypeError)
+ expect(-> new Publisher(onListenerError: 5)).to.throw(TypeError)
File renamed without changes.
File renamed without changes.

0 comments on commit ecab43a

Please sign in to comment.