Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ability to register for all messages #1

Merged
merged 1 commit into from May 3, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 28 additions & 2 deletions src/hub.js
@@ -1,7 +1,8 @@
Hub = (function() {
"use strict";

var listeners = {},
var globalListeners = [],
listeners = {},
currID = 0;

function on(message, callback, context) {
Expand All @@ -17,16 +18,32 @@ Hub = (function() {
return id;
}

function all(callback, context) {
globalListeners.push({
id: currID,
callback: context ? callback.bind(context) : callback
});

return currID++;
}

function fire(message) {
var messageListeners = listeners[message];

if(messageListeners) {
// arguments to specific message listeners don't include the
// message name as the first argument, it's implied
var args = [].slice.call(arguments, 1);

for(var i = 0, listener; listener = messageListeners[i]; ++i) {
listener.callback.apply(null, args);
}
}

for(var j = 0, glistener; glistener = globalListeners[j]; ++j) {
// global listeners get the message name as the first argument
glistener.callback.apply(null, arguments);
}
}

function off(id) {
Expand All @@ -35,21 +52,30 @@ Hub = (function() {
for(var i = 0, listener; listener = messageListeners[i]; ++i) {
if(listener.id === id) {
messageListeners.splice(i, 1);
break;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!

}
}
}

for(var j = 0, glistener; glistener = globalListeners[j]; ++j) {
if(glistener.id === id) {
globalListeners.splice(j, 1);
break;
}
}
}

function reset() {
listeners = {};
globalListeners = [];
currID = 0;
}

return {
all: all,
on: on,
fire: fire,
reset: reset,
off: off
};
}());

26 changes: 24 additions & 2 deletions test/test.js
Expand Up @@ -43,6 +43,11 @@
asyncTest("different messages", function() {
var firstTriggered = false;
var secondTriggered = false;
var totalMessages = 0;

hub.all(function() {
totalMessages++;
});

hub.on("message1", function() {
equal(secondTriggered, false, "listener for second message has not been triggered");
Expand All @@ -57,22 +62,32 @@

hub.fire("message1");
hub.fire("message2");

equal(totalMessages, 2, ".all() handlers receive all messages");
});

asyncTest("off unregisters listener", function() {
var firstTriggered = false;
var secondTriggered = false;


var id0 = hub.all(function() {
ok(false, "this should never be triggered");
firstTriggered = true;
});

var id1 = hub.on("message", function() {
ok(false, "this should never be triggered");
firstTriggered = false;
secondTriggered = true;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The diff confused me as to what the intent was here, but after reading the code everything was crystal clear.

});

var id2 = hub.on("message", function() {
equal(firstTriggered, false, "first listener was not triggered");
equal(secondTriggered, false, "second listener was not triggered");
start();
});


hub.off(id0);
hub.off(id1);
hub.fire("message");
});
Expand All @@ -84,6 +99,13 @@
start();
});

hub.all(function(name, arg1, arg2) {
equal(name, "message", "message name to .all passed correctly");
equal(arg1, "arg1", "first argument to .all passed correctly");
equal(arg2, "arg2", "second argument to .all passed correctly");
start();
});

hub.fire("message", "arg1", "arg2");
});
}());
Expand Down