Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

private_pub.js subscription callback and unsubcribe function. #60

Merged
merged 3 commits into from
Feb 22, 2013
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion app/assets/javascripts/private_pub.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ function buildPrivatePub(doc) {
fayeClient: null,
fayeCallbacks: [],
subscriptions: {},
subscriptionObjects: {},
subscriptionCallbacks: {},

faye: function(callback) {
Expand Down Expand Up @@ -49,7 +50,11 @@ function buildPrivatePub(doc) {
}
self.subscriptions[options.channel] = options;
self.faye(function(faye) {
faye.subscribe(options.channel, self.handleResponse);
var sub = faye.subscribe(options.channel, self.handleResponse);
self.subscriptionObjects[options.channel] = sub;
if (options.subscription) {
options.subscription(sub);
}
});
},

Expand All @@ -62,6 +67,26 @@ function buildPrivatePub(doc) {
}
},

subscription: function(channel) {
return self.subscriptionObjects[channel];
},

unsubscribeAll: function() {
for (var i in self.subscriptionObjects) {
if ( self.subscriptionObjects.hasOwnProperty(i) ) {
self.unsubscribe(i);
}
}
},

unsubscribe: function(channel) {
var sub = self.subscription(channel);
if (sub) {
sub.cancel();
delete self.subscriptionObjects[channel];
}
},

subscribe: function(channel, callback) {
self.subscriptionCallbacks[channel] = callback;
}
Expand Down
57 changes: 57 additions & 0 deletions spec/javascripts/private_pub_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,63 @@ describe("PrivatePub", function() {
expect(pub.subscriptions.somechannel).toEqual(options);
});

it("takes a callback for subscription object when signing", function(){
var faye = {subscribe: function(){ return "subscription"; }};
spyOn(pub, 'faye').andCallFake(function(callback) {
callback(faye);
});
var options = { server: "server", channel: "somechannel" };
options.subscription = jasmine.createSpy();
pub.sign(options);
expect(options.subscription).toHaveBeenCalledWith("subscription");
});

it("returns the subscription object for a subscribed channel", function(){
var faye = {subscribe: function(){ return "subscription"; }};
spyOn(pub, 'faye').andCallFake(function(callback) {
callback(faye);
});
var options = { server: "server", channel: "somechannel" };
pub.sign(options);
expect(pub.subscription("somechannel")).toEqual("subscription")
});

it("unsubscribes a channel by name", function(){
var sub = { cancel: jasmine.createSpy() };
var faye = {subscribe: function(){ return sub; }};
spyOn(pub, 'faye').andCallFake(function(callback) {
callback(faye);
});
var options = { server: "server", channel: "somechannel" };
pub.sign(options);
expect(pub.subscription("somechannel")).toEqual(sub);
pub.unsubscribe("somechannel");
expect(sub.cancel).toHaveBeenCalled();
expect(pub.subscription("somechannel")).toBeFalsy();
});

it("unsubscribes all channels", function(){
var created = 0;
var sub = function() {
created ++;
var sub = { cancel: function(){ created --; } };
return sub;
};
var faye = { subscribe: function(){ return sub(); }};
spyOn(pub, 'faye').andCallFake(function(callback) {
callback(faye);
});
pub.sign({server: "server", channel: "firstchannel"});
pub.sign({server: "server", channel: "secondchannel"});
expect(created).toEqual(2);
expect(pub.subscription("firstchannel")).toBeTruthy();
expect(pub.subscription("secondchannel")).toBeTruthy();
pub.unsubscribeAll()
expect(created).toEqual(0);
expect(pub.subscription("firstchannel")).toBeFalsy();
expect(pub.subscription("secondchannel")).toBeFalsy();
});

it("triggers faye callback function immediately when fayeClient is available", function() {
var called = false;
pub.fayeClient = "faye";
Expand Down