Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow event list instead of options object.

  • Loading branch information...
commit b45794525ccc4772b0fa0ab2f0907faf71de097c 1 parent 171f9e7
@domenic authored
Showing with 33 additions and 18 deletions.
  1. +6 −2 lib/Publisher.js
  2. +27 −16 test/3 options.coffee
View
8 lib/Publisher.js
@@ -5,7 +5,11 @@ var emitter = require("./emitter");
function normalizeOptions(options) {
if (typeof options !== "object" || options === null) {
- throw new TypeError("options argument must be an object.");
+ throw new TypeError("options argument must be an object or array of events.");
+ }
+
+ if (Array.isArray(options)) {
+ options = { events: options };
}
if (options.onListenerError === undefined) {
@@ -43,7 +47,7 @@ module.exports = function Publisher(options) {
if (options === undefined) {
options = {};
}
- normalizeOptions(options);
+ options = normalizeOptions(options);
function callListener(listener, args) {
try {
View
43 test/3 options.coffee
@@ -58,27 +58,38 @@ describe "Publisher options", ->
next()
describe "events", ->
- beforeEach ->
- publisher = new Publisher(events: ["event1", "event2"])
- emitter = publisher.emitter
+ testEvents = () ->
+ it "should publish the supplied events as usual", ->
+ listener1 = sinon.spy()
+ listener2 = sinon.spy()
+ emitter.on("event1", listener1)
+ emitter.on("event2", listener2)
- 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")
- publisher.publish("event1")
- publisher.publish("event2")
+ listener1.should.have.been.called
+ listener2.should.have.been.called
+
+ it "should throw an error upon attempting to subscribe to an unknown event", ->
+ (-> emitter.on("unknownEvent", ->)).should.throw()
+
+ it "should throw an error upon attempting to publish an unknown event", ->
+ (-> publisher.publish("unknownEvent")).should.throw()
+
+ describe "as an explicit option", ->
+ beforeEach ->
+ publisher = new Publisher(events: ["event1", "event2"])
+ emitter = publisher.emitter
- listener1.should.have.been.called
- listener2.should.have.been.called
+ testEvents()
- it "should throw an error upon attempting to subscribe to an unknown event", ->
- (-> emitter.on("unknownEvent", ->)).should.throw()
+ describe "used in place of options", ->
+ beforeEach ->
+ publisher = new Publisher(["event1", "event2"])
+ emitter = publisher.emitter
- it "should throw an error upon attempting to publish an unknown event", ->
- (-> publisher.publish("unknownEvent")).should.throw()
+ testEvents()
it "should be validated", ->
(-> new Publisher(null)).should.throw(TypeError)
Please sign in to comment.
Something went wrong with that request. Please try again.