Permalink
Browse files

Don't publish subscribe events for subscriptions that already exist.

  • Loading branch information...
1 parent 91e6ce1 commit a3fd080d1dcbcca75a7fbc0b9a6938cdae18373d @jcoglan jcoglan committed Sep 21, 2011
@@ -50,13 +50,13 @@ Faye.Engine.Memory = Faye.Class(Faye.Engine.Base, {
var clients = this._clients, channels = this._channels;
clients[clientId] = clients[clientId] || new Faye.Set();
- clients[clientId].add(channel);
+ var trigger = clients[clientId].add(channel);
channels[channel] = channels[channel] || new Faye.Set();
channels[channel].add(clientId);
this.debug('Subscribed client ? to channel ?', clientId, channel);
- this.trigger('subscribe', clientId, channel);
+ if (trigger) this.trigger('subscribe', clientId, channel);
if (callback) callback.call(scope, true);
},
@@ -100,10 +100,11 @@ Faye.Engine.Redis = Faye.Class(Faye.Engine.Base, {
subscribe: function(clientId, channel, callback, scope) {
var self = this;
- this._redis.sadd(this._ns + '/clients/' + clientId + '/channels', channel);
+ this._redis.sadd(this._ns + '/clients/' + clientId + '/channels', channel, function(error, added) {
+ if (added === 1) self.trigger('subscribe', clientId, channel);
+ });
this._redis.sadd(this._ns + '/channels' + channel, clientId, function() {
self.debug('Subscribed client ? to channel ?', clientId, channel);
- self.trigger('subscribe', clientId, channel);
if (callback) callback.call(scope);
});
},
@@ -48,14 +48,14 @@ def ping(client_id)
def subscribe(client_id, channel, &callback)
@clients[client_id] ||= Set.new
- @clients[client_id].add(channel)
+ should_trigger = @clients[client_id].add?(channel)
@channels[channel] ||= Set.new
@channels[channel].add(client_id)
debug 'Subscribed client ? to channel ?', client_id, channel
+ trigger(:subscribe, client_id, channel) if should_trigger
callback.call(true) if callback
- trigger(:subscribe, client_id, channel)
end
def unsubscribe(client_id, channel, &callback)
@@ -70,8 +70,8 @@ def unsubscribe(client_id, channel, &callback)
end
debug 'Unsubscribed client ? from channel ?', client_id, channel
- callback.call(true) if callback
trigger(:unsubscribe, client_id, channel) if should_trigger
+ callback.call(true) if callback
end
def publish(message)
@@ -101,11 +101,12 @@ def ping(client_id)
def subscribe(client_id, channel, &callback)
init
- @redis.sadd(@ns + "/clients/#{client_id}/channels", channel)
+ @redis.sadd(@ns + "/clients/#{client_id}/channels", channel) do |added|
+ trigger(:subscribe, client_id, channel) if added == 1
+ end
@redis.sadd(@ns + "/channels#{channel}", client_id) do
debug 'Subscribed client ? to channel ?', client_id, channel
callback.call if callback
- trigger(:subscribe, client_id, channel)
end
end
@@ -217,6 +217,15 @@ JS.ENV.EngineSpec = JS.Test.describe("Pub/sub engines", function() { with(this)
expect_event("alice", "subscribe", ["/messages/foo"])
subscribe("alice", "/messages/foo")
}})
+
+ describe("when the client is subscribed to the channel", function() { with(this) {
+ before(function() { this.subscribe("alice", "/messages/foo") })
+
+ it("does not publish an event", function() { with(this) {
+ expect_no_event("alice", "subscribe", ["/messages/foo"])
+ subscribe("alice", "/messages/foo")
+ }})
+ }})
}})
@@ -210,6 +210,15 @@ def create_engine
expect_event :alice, :subscribe, ["/messages/foo"]
subscribe :alice, "/messages/foo"
end
+
+ describe "when the client is subscribed to the channel" do
+ before { subscribe :alice, "/messages/foo" }
+
+ it "does not publish an event" do
+ expect_no_event :alice, :subscribe, ["/messages/foo"]
+ subscribe :alice, "/messages/foo"
+ end
+ end
end
describe :unsubscribe do

0 comments on commit a3fd080

Please sign in to comment.