Permalink
Browse files

Adding support for `.on("event1 event2", listener)` syntax.

Bumping version to 1.1.0.
  • Loading branch information...
1 parent 098e65f commit d0c9d682c531ac74dc44d09b032c640f7073b7cd @domenic committed Feb 9, 2012
Showing with 92 additions and 6 deletions.
  1. +14 −5 lib/emitter.js
  2. +1 −1 package.json
  3. +37 −0 test/1 basicFunctionality.coffee
  4. +40 −0 test/4 onNext.coffee
View
@@ -7,6 +7,8 @@ function isHash(unknown) {
return typeof unknown === "object" && unknown !== null;
}
+var SEPARATOR = " ";
+
module.exports = function emitter(normalListeners, oneTimeListeners, events) {
function subscribeSingleListener(eventName, listener, listeners) {
if (events && events.indexOf(eventName) === -1) {
@@ -59,7 +61,10 @@ module.exports = function emitter(normalListeners, oneTimeListeners, events) {
if (typeof listener !== "function") {
throw new TypeError("listener argument must be a function.");
}
- subscribeSingleListener(eventNameOrHash, listener, listeners);
+
+ eventNameOrHash.split(SEPARATOR).forEach(function (eventName) {
+ subscribeSingleListener(eventName, listener, listeners);
+ });
}
}
@@ -73,11 +78,15 @@ module.exports = function emitter(normalListeners, oneTimeListeners, events) {
off: function (eventNameOrHash, listener) {
if (typeof eventNameOrHash === "string") {
if (typeof listener === "undefined") {
- unsubscribeAllListeners(eventNameOrHash, normalListeners);
- unsubscribeAllListeners(eventNameOrHash, oneTimeListeners);
+ eventNameOrHash.split(SEPARATOR).forEach(function (eventName) {
+ unsubscribeAllListeners(eventName, normalListeners);
+ unsubscribeAllListeners(eventName, oneTimeListeners);
+ });
} else if (typeof listener === "function") {
- unsubscribeSingleListener(eventNameOrHash, listener, normalListeners);
- unsubscribeSingleListener(eventNameOrHash, listener, oneTimeListeners);
+ eventNameOrHash.split(SEPARATOR).forEach(function (eventName) {
+ unsubscribeSingleListener(eventName, listener, normalListeners);
+ unsubscribeSingleListener(eventName, listener, oneTimeListeners);
+ });
} else {
throw new TypeError("listener argument must be a function.");
}
View
@@ -7,7 +7,7 @@
"eventemitter",
"events"
],
- "version": "1.0.0",
+ "version": "1.1.0",
"author": "Domenic Denicola <domenic@domenicdenicola.com> (http://domenicdenicola.com)",
"repository": {
"type": "git",
@@ -102,6 +102,43 @@ describe "Publisher/emitter under normal usage", ->
sinon.assert.notCalled(listener1)
sinon.assert.notCalled(listener2)
+ describe "when three events are subscribed to in one call", ->
+ events = ["event1", "event2", "event3"]
+ listener = null
+
+ beforeEach ->
+ listener = sinon.spy()
+ emitter.on(events.join(" "), listener)
+
+ it "publishes the first event correctly", ->
+ publisher.publish("event1")
+
+ sinon.assert.calledOnce(listener)
+
+ it "publishes the second event correctly", ->
+ publisher.publish("event2")
+
+ sinon.assert.calledOnce(listener)
+
+ it "publishes the third event correctly", ->
+ publisher.publish("event3")
+
+ sinon.assert.calledOnce(listener)
+
+ it "unsubscribes from two events at once correctly, when passed the listener explicitly", ->
+ emitter.off("event2 event3", listener)
+ publisher.publish("event2")
+ publisher.publish("event3")
+
+ sinon.assert.notCalled(listener)
+
+ it "unsubscribes from two events at once correctly, when doing blanket unsubscription", ->
+ emitter.off("event2 event3")
+ publisher.publish("event2")
+ publisher.publish("event3")
+
+ sinon.assert.notCalled(listener)
+
describe "when a hash object mapping event names to listeners is used for subscription", ->
hash = null
View
@@ -127,6 +127,46 @@ describe "emitter.onNext", ->
sinon.assert.calledOnce(listener)
+ describe "when three events are subscribed to in one call", ->
+ events = ["event1", "event2", "event3"]
+ listener = null
+
+ beforeEach ->
+ listener = sinon.spy()
+ emitter.onNext(events.join(" "), listener)
+
+ it "publishes the first event correctly, and the listener is called only the first time", ->
+ publisher.publish("event1")
+ publisher.publish("event1")
+
+ sinon.assert.calledOnce(listener)
+
+ it "publishes the second event correctly, and the listener is called only the first time", ->
+ publisher.publish("event2")
+ publisher.publish("event2")
+
+ sinon.assert.calledOnce(listener)
+
+ it "publishes the third event correctly, and the listener is called only the first time", ->
+ publisher.publish("event3")
+ publisher.publish("event3")
+
+ sinon.assert.calledOnce(listener)
+
+ it "unsubscribes from two events at once correctly, when passed the listener explicitly", ->
+ emitter.off("event2 event3", listener)
+ publisher.publish("event2")
+ publisher.publish("event3")
+
+ sinon.assert.notCalled(listener)
+
+ it "unsubscribes from two events at once correctly, when doing blanket unsubscription", ->
+ emitter.off("event2 event3")
+ publisher.publish("event2")
+ publisher.publish("event3")
+
+ sinon.assert.notCalled(listener)
+
describe "when a hash object mapping event names to listeners is used for subscription", ->
hash = null

0 comments on commit d0c9d68

Please sign in to comment.