public
Last active

A simple javascript event bus

  • Download Gist
Suteki.EventBus.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
 
 
var Suteki = Suteki || {};
 
Suteki.new_eventBus = (function(){
var self = {};
var subscriptions = {};
var unsubscribeTokens = [];
var subscriptionPointers = [];
 
self.subscribe = function(nameOfMessage, callback){
var i;
var subscriptionPointer;
var unsubscribeToken;
 
if(typeof nameOfMessage !== 'string'){
throw new Error("Suteki.eventBus.subscribe requires a first string argument 'nameOfMessage'");
}
if(typeof callback !== 'function'){
throw new Error("Suteki.eventBus.subscribe requires a second function argument 'callback'")
}
 
if(!subscriptions[nameOfMessage]){
subscriptions[nameOfMessage] = [];
}
subscriptions[nameOfMessage].push(callback);
 
// package and return an unsubscribe token.
subscriptionPointer = { nameOfMessage:nameOfMessage, callbackIndex: subscriptions[nameOfMessage].length-1 };
i = subscriptionPointers.length;
unsubscribeToken = {};
subscriptionPointers[i] = subscriptionPointer;
unsubscribeTokens[i] = unsubscribeToken;
return unsubscribeToken;
};
 
self.publish = function(nameOfMessage, data){
if(typeof nameOfMessage !== 'string'){
throw new Error("Suteki.eventBus.publish requires a first string argument 'nameOfMessage'");
}
if(!data){
throw new Error("Suteki.eventBus.publish requires a second argument 'data'");
}
 
var i;
var callback;
 
if(subscriptions[nameOfMessage]) {
for(i=0; i<subscriptions[nameOfMessage].length; i++){
callback = subscriptions[nameOfMessage][i];
if(callback){
callback(data);
}
}
}
};
 
self.unsubscribe = function(unsubscribeToken){
var subscriptionPointer = subscriptionPointers[unsubscribeTokens.indexOf(unsubscribeToken)];
subscriptions[subscriptionPointer.nameOfMessage][subscriptionPointer.callbackIndex] = null;
};
 
return self;
});
 
// for testing in node
if(exports) {
exports.new_eventBus = Suteki.new_eventBus;
}
Suteki.EventBusTests.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
// run tests in node
 
var Suteki = Suteki || {};
 
Suteki.eventBus = require('./Suteki.EventBus');
 
Suteki.eventBusTests = (function(new_bus){
 
if(!new_bus){
throw new Error("requires Suteki.EventBus.js");
}
 
var assert = function(passed, message){
if(!passed){
console.log("FAILED " + (message || ""))
};
};
var tests = {};
 
tests.should_be_able_to_publish_and_subscribe = function(){
var bus = new_bus();
var testMessage = "myTestMessage";
var handler1;
var handler2;
 
bus.subscribe(testMessage, function(data){
if(data === "Hello World"){
handler1 = "OK";
}
});
 
bus.subscribe(testMessage, function(data){
if(data === "Hello World"){
handler2 = "OK";
}
});
 
bus.publish(testMessage, "Hello World");
 
assert(handler1 === "OK", "Handler 1 Failed");
assert(handler2 === "OK", "Handler 2 Failed");
};
 
tests.should_be_able_to_subscribe_and_unsubscribe = function(){
var bus = new_bus();
var testMessage = "myTestMessage";
var resultData = "";
 
var unsubscribeToken = bus.subscribe(testMessage, function(data){
resultData = data;
});
 
bus.publish(testMessage, "Hello");
assert(resultData === "Hello", "subscription failed");
 
bus.unsubscribe(unsubscribeToken);
bus.publish(testMessage, "No!");
assert(resultData !== "No!", "unsubscribe failed, callback still present.");
};
 
(function(){
var testName;
for(testName in tests){
if(tests.hasOwnProperty(testName)){
console.log(">>> " + testName.replace(/_/g, " "));
tests[testName]();
}
}
}());
 
}(Suteki.eventBus.new_eventBus));

Run the test in node like this:
node Embassy.EventBusTests.js
should be able to publish and subscribe
should be able to subscribe and unsubscribe

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.