Skip to content

Commit

Permalink
Add some docs to the message queue
Browse files Browse the repository at this point in the history
  • Loading branch information
cramforce committed Oct 7, 2010
1 parent 8fef311 commit d56de65
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/message-queue.js
@@ -1,12 +1,18 @@

/*
A Module that exposes a message queue. One can subscribe to channels and receive
callbacks when someone publishes messages to these channels
*/

var Subscribers = {};

// Subscription objects are returned by the subscribe function.
function Subscription (channels, cb) {
this.channels = channels;
this.cb = cb;
}

// Call this method to unsubscribe a subscription
Subscription.prototype.unsubscribe = function () {
var cb = this.cb;
this.channels.forEach(function (channel) {
Expand All @@ -24,6 +30,8 @@ Subscription.prototype.unsubscribe = function () {
})
}

// Subscribe to an array of channels (TODO make this an array or string)
// listenerCB will be fired when an event is published to one of the channels.
exports.subscribe = function (channels, listenerCB) {
if(typeof listenerCB != "function") {
throw new Error("subscribe needs a callback parameter");
Expand All @@ -38,6 +46,7 @@ exports.subscribe = function (channels, listenerCB) {
return new Subscription(channels, listenerCB);
}

// Publish an event of data to a channel
exports.publish = function (channel, data) {
var subs = Subscribers[channel];
if(subs) {
Expand Down

0 comments on commit d56de65

Please sign in to comment.