socketcluster-client
Advanced tools
Comparing version 1.0.6 to 1.0.7
{ | ||
"name": "socketcluster-client", | ||
"description": "Client side of SocketCluster", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"homepage": "http://socketcluster.io", | ||
@@ -6,0 +6,0 @@ "contributors": [ |
@@ -12,5 +12,8 @@ var Emitter = require('emitter'); | ||
this.STATE_SUBSCRIBED = 'subscribed'; | ||
this.STATE_PENDING = 'pending'; | ||
this.STATE_UNSUBSCRIBED = 'unsubscribed'; | ||
this.name = name; | ||
this.subscribing = false; | ||
this.subscribed = false; | ||
this.state = this.STATE_UNSUBSCRIBED; | ||
this.socket = socket; | ||
@@ -17,0 +20,0 @@ }; |
139
scsocket.js
@@ -305,2 +305,15 @@ /** | ||
this.connecting = false; | ||
var channel, newState; | ||
for (var channelName in this._channels) { | ||
channel = this._channels[channelName]; | ||
if (channel.state == channel.STATE_SUBSCRIBED || | ||
channel.state == channel.STATE_PENDING) { | ||
newState = channel.STATE_PENDING; | ||
} else { | ||
newState = channel.STATE_UNSUBSCRIBED; | ||
} | ||
this._triggerChannelUnsubscribe(channel, newState); | ||
} | ||
if (!this._connectAttempts) { | ||
@@ -348,7 +361,5 @@ this._tryReconnect(); | ||
if (channel) { | ||
channel.subscribed = false; | ||
Emitter.prototype.emit.call(this, e.event, kickData.message, channelName); | ||
channel.emit(e.event, kickData.message, channelName); | ||
channel.emit('dropOut', kickData.message, channelName); | ||
Emitter.prototype.emit.call(this, 'dropOut', kickData.message, channelName); | ||
this._triggerChannelUnsubscribe(channel); | ||
} | ||
@@ -552,28 +563,12 @@ } else { | ||
} | ||
if (!channel.subscribed && !channel.subscribing) { | ||
channel.subscribing = true; | ||
if (channel.state == channel.STATE_UNSUBSCRIBED) { | ||
channel.state = channel.STATE_PENDING; | ||
this.emit('subscribe', channelName, function (err) { | ||
channel.subscribing = false; | ||
channel.subscribed = !err; | ||
if (err) { | ||
channel.emit('subscribeFail', err, channelName); | ||
Emitter.prototype.emit.call(self, 'subscribeFail', err, channelName); | ||
channel.emit('dropOut', err, channelName); | ||
Emitter.prototype.emit.call(self, 'dropOut', err, channelName); | ||
self._triggerChannelSubscribeFail(err, channel); | ||
} else { | ||
channel.emit('subscribe', channelName); | ||
Emitter.prototype.emit.call(self, 'subscribe', channelName); | ||
self._triggerChannelSubscribe(channel); | ||
} | ||
}); | ||
} else { | ||
// In this case, the client thinks it's already subscribed - We emit a precautionary | ||
// subscribe event in case the client has been kicked out of a channel and is not aware of it. | ||
this.emit('subscribe', channelName, function (err) { | ||
channel.subscribed = !err; | ||
if (err) { | ||
channel.emit('dropOut', err, channelName); | ||
Emitter.prototype.emit.call(self, 'dropOut', err, channelName); | ||
} | ||
}); | ||
} | ||
@@ -585,10 +580,9 @@ | ||
SCSocket.prototype.unsubscribe = function (channelName) { | ||
var self = this; | ||
var channel = this._channels[channelName]; | ||
if (channel) { | ||
if (channel.subscribed || channel.subscribing) { | ||
channel.subscribing = false; | ||
channel.subscribed = false; | ||
if (channel.state != channel.STATE_UNSUBSCRIBED) { | ||
this._triggerChannelUnsubscribe(channel); | ||
@@ -599,6 +593,3 @@ // The only case in which unsubscribe can fail is if the connection is closed or dies. | ||
this.emit('unsubscribe', channelName, function (err) { | ||
channel.emit('unsubscribe', channelName); | ||
Emitter.prototype.emit.call(self, 'unsubscribe', channelName); | ||
}); | ||
this.emit('unsubscribe', channelName); | ||
} | ||
@@ -628,8 +619,16 @@ } | ||
var subs = []; | ||
var channelObject; | ||
for (var channel in this._channels) { | ||
channelObject = this._channels[channel]; | ||
if (channelObject && (channelObject.subscribed || includePending)) { | ||
subs.push(channel); | ||
var channel, includeChannel; | ||
for (var channelName in this._channels) { | ||
channel = this._channels[channelName]; | ||
if (includePending) { | ||
includeChannel = channel && (channel.state == channel.STATE_SUBSCRIBED || | ||
channel.state == channel.STATE_PENDING); | ||
} else { | ||
includeChannel = channel && channel.state == channel.STATE_SUBSCRIBED; | ||
} | ||
if (includeChannel) { | ||
subs.push(channelName); | ||
} | ||
} | ||
@@ -642,13 +641,47 @@ return subs; | ||
if (includePending) { | ||
return !!channel; | ||
return !!channel && (channel.state == channel.STATE_SUBSCRIBED || | ||
channel.state == channel.STATE_PENDING); | ||
} | ||
return !!channel && channel.subscribed; | ||
return !!channel && channel.state == channel.STATE_SUBSCRIBED; | ||
}; | ||
SCSocket.prototype._triggerChannelSubscribe = function (channel) { | ||
var channelName = channel.name; | ||
channel.state = channel.STATE_SUBSCRIBED; | ||
channel.emit('subscribe', channelName); | ||
Emitter.prototype.emit.call(this, 'subscribe', channelName); | ||
}; | ||
SCSocket.prototype._triggerChannelSubscribeFail = function (err, channel) { | ||
var channelName = channel.name; | ||
channel.state = channel.STATE_UNSUBSCRIBED; | ||
channel.emit('subscribeFail', err, channelName); | ||
Emitter.prototype.emit.call(this, 'subscribeFail', err, channelName); | ||
}; | ||
SCSocket.prototype._triggerChannelUnsubscribe = function (channel, newState) { | ||
var channelName = channel.name; | ||
var oldState = channel.state; | ||
if (newState) { | ||
channel.state = newState; | ||
} else { | ||
channel.state = channel.STATE_UNSUBSCRIBED; | ||
} | ||
if (oldState == channel.STATE_SUBSCRIBED) { | ||
channel.emit('unsubscribe', channelName); | ||
Emitter.prototype.emit.call(this, 'unsubscribe', channelName); | ||
} | ||
}; | ||
SCSocket.prototype._resubscribe = function (callback) { | ||
var self = this; | ||
var events = []; | ||
for (var event in this._channels) { | ||
events.push(event); | ||
var channels = []; | ||
for (var channelName in this._channels) { | ||
channels.push(channelName); | ||
} | ||
@@ -658,4 +691,10 @@ var error; | ||
var ackHandler = function (err) { | ||
var ackHandler = function (err, channel) { | ||
ackCount++; | ||
if (err) { | ||
self._triggerChannelSubscribeFail(err, channel); | ||
} else { | ||
self._triggerChannelSubscribe(channel); | ||
} | ||
if (!error) { | ||
@@ -665,3 +704,3 @@ if (err) { | ||
callback && callback(err); | ||
} else if (ackCount >= events.length) { | ||
} else if (ackCount >= channels.length) { | ||
callback && callback(); | ||
@@ -671,4 +710,10 @@ } | ||
}; | ||
for (var i in events) { | ||
this.emit('subscribe', events[i], ackHandler); | ||
for (var i in this._channels) { | ||
(function (channel) { | ||
if (channel.state == channel.STATE_PENDING) { | ||
self.emit('subscribe', channel.name, function (err) { | ||
ackHandler(err, channel); | ||
}); | ||
} | ||
})(this._channels[i]); | ||
} | ||
@@ -675,0 +720,0 @@ }; |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
232953
5431