Add singleton subscriptions
Sometime you need to have only one subscription to a channel, regardless the number of times you call subscribe. It may be useful when you subscribe in an AJAX callback.
$(".user a.follow").live("click", function(event){
$.post(this.href, function(data, status, xhr){
var channel = "users/" + data.user.id + "/feed";
juggernaut.singleSubscribe(channel, function(feedEntry){
console.log("Got new feed entry: " + feedEntry);
}
});
});
Even if the user click many times to follow an user, each new messages of this users's feed will be pushed to the client only once.
+1 on including this.
When does it make sense to have multiple subscriptions to a single channel? I think this should be the default behavior, not a special path.
Or is the use case adding multiple subscriptions to a single channel, each with a different handler for the data?
I think the identity of the handler should be sufficient, and in this case you shouldn't be subscribing with anonymous handlers.
For example, I think it's reasonable for Juggernaut to detect and thwart duplicating messages to a handler added like this:
function followHandler(feedEntry) {
console.log("Got new feed entry: " + feedEntry);
}
$(".user a.follow").live("click", function(event){
$.post(this.href, function(data, status, xhr){
var channel = "users/" + data.user.id + "/feed";
juggernaut.subscribe(channel, gotFeedEntry);
});
});
However, when passing an anonymous handler like in the initial example, it is perfectly reasonable to add each "new" handler.
I can patch to use this behavior, @duncanbeevers. It's a good idea : subscribe only if it's a new callback. I would be glad to have @maccman's opinion on this.
+1 on @duncanbeevers's comment that this should be the default behavior. If @maccman wants to keep the current subscription model, +1 on this PR.