Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding `events` option to `Publisher` constructor.

This allows a publisher to be restricted to a given set of events, such that either publishing or subscribing to any other event throws an error. Useful for preventing typos or confusion.
  • Loading branch information...
commit b10b2f50895bd7000b9a6396e81b3c5384f40add 1 parent 5d3a901
@domenic authored
Showing with 40 additions and 3 deletions.
  1. +5 −1 lib/emitter.js
  2. +8 −2 lib/pubit.js
  3. +27 −0 test/3 options.coffee
View
6 lib/emitter.js
@@ -5,8 +5,12 @@ function isHash(unknown) {
return typeof unknown === "object" && unknown !== null;
}
-module.exports = function (normalListeners, oneTimeListeners) {
+module.exports = function (normalListeners, oneTimeListeners, events) {
function subscribeSingleListener(eventName, listener, listeners) {
+ if (events && events.indexOf(eventName) === -1) {
+ throw new Error('Tried to subscribe to an unknown event "' + eventName + '".');
+ }
+
if (!listeners.has(eventName)) {
listeners.set(eventName, []);
}
View
10 lib/pubit.js
@@ -3,7 +3,6 @@
// Still TODO:
// * publisher.mixinEmitter(that)? publish = pubit.makeEmitter(that)?
-// * restricted event names? would be a constructor parameter
// * more argument validation (or remove argument validation, not sure yet)
var dict = require("dict");
@@ -27,6 +26,10 @@ function normalizeOptions(options) {
throw new TypeError("options.onListenerError must be an object.");
}
+ if (options.events !== undefined && !Array.isArray(options.events)) {
+ throw new TypeError("options.events must be an array.");
+ }
+
return options;
}
@@ -68,6 +71,9 @@ exports.Publisher = function (options) {
if (typeof eventName !== "string") {
throw new TypeError("eventName argument must be a string.");
}
+ if (options.events && options.events.indexOf(eventName) === -1) {
+ throw new Error('Tried to publish an unknown event "' + eventName + '".');
+ }
if (normalListeners.has(eventName)) {
callListenersFor(eventName, args, normalListeners);
@@ -78,5 +84,5 @@ exports.Publisher = function (options) {
}
};
- that.emitter = emitter(normalListeners, oneTimeListeners);
+ that.emitter = emitter(normalListeners, oneTimeListeners, options.events);
};
View
27 test/3 options.coffee
@@ -45,6 +45,29 @@ describe "Publisher options", ->
sinon.assert.called(listener2)
next()
+ describe "events", ->
+ beforeEach ->
+ publisher = new Publisher(events: ["event1", "event2"])
+ emitter = publisher.emitter
+
+ it "should publish the supplied events as usual", ->
+ listener1 = sinon.spy()
+ listener2 = sinon.spy()
+ emitter.on("event1", listener1)
+ emitter.on("event2", listener2)
+
+ publisher.publish("event1")
+ publisher.publish("event2")
+
+ sinon.assert.called(listener1)
+ sinon.assert.called(listener2)
+
+ it "should throw an error upon attempting to subscribe to an unknown event", ->
+ expect(-> emitter.on("unknownEvent", ->)).to.throw()
+
+ it "should throw an error upon attempting to publish an unknown event", ->
+ expect(-> publisher.publish("unknownEvent")).to.throw()
+
it "should be validated", ->
expect(-> new Publisher(null)).to.throw(TypeError)
expect(-> new Publisher(5)).to.throw(TypeError)
@@ -54,3 +77,7 @@ describe "Publisher options", ->
expect(-> new Publisher(onListenerError: null)).to.throw(TypeError)
expect(-> new Publisher(onListenerError: {})).to.throw(TypeError)
expect(-> new Publisher(onListenerError: 5)).to.throw(TypeError)
+
+ expect(-> new Publisher(events: null)).to.throw(TypeError)
+ expect(-> new Publisher(events: {})).to.throw(TypeError)
+ expect(-> new Publisher(events: 5)).to.throw(TypeError)
Please sign in to comment.
Something went wrong with that request. Please try again.