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

Commit 9524f14

Browse files
committed
the client autodisconnects if streams become done during resend
1 parent a10b9ad commit 9524f14

File tree

2 files changed

+61
-32
lines changed

2 files changed

+61
-32
lines changed

streamr-client.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -339,10 +339,11 @@ StreamrClient.prototype.unsubscribe = function(sub) {
339339
if (this.subsByStream[sub.streamId].length === 1 && this.connected && !this.disconnecting && sub.isSubscribed()) {
340340
this._requestUnsubscribe(sub.streamId)
341341
}
342-
// Else the sub can be cleaned off immediately
342+
// Else the sub can be cleaned off' immediately
343343
else {
344344
this._removeSubscription(sub)
345345
sub.trigger('unsubscribed')
346+
this._checkAutoDisconnect()
346347
}
347348
}
348349

@@ -454,11 +455,7 @@ StreamrClient.prototype.connect = function(reconnect) {
454455
sub.trigger('unsubscribed')
455456
})
456457

457-
// Disconnect if no longer subscribed to any channels
458-
if (Object.keys(_this.subsByStream).length===0 && _this.options.autoDisconnect) {
459-
console.log("Disconnecting due to no longer being subscribed to any channels")
460-
_this.disconnect()
461-
}
458+
_this._checkAutoDisconnect()
462459
})
463460

464461
// Route resending state messages to corresponding Subscriptions
@@ -529,6 +526,14 @@ StreamrClient.prototype.disconnect = function() {
529526
this.socket.disconnect()
530527
}
531528

529+
StreamrClient.prototype._checkAutoDisconnect = function() {
530+
// Disconnect if no longer subscribed to any channels
531+
if (Object.keys(this.subsByStream).length===0 && this.options.autoDisconnect) {
532+
console.log("Disconnecting due to no longer being subscribed to any channels")
533+
this.disconnect()
534+
}
535+
}
536+
532537
StreamrClient.prototype._resendAndSubscribe = function(sub) {
533538
var _this = this
534539

test/test.streamr-client.js

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -791,32 +791,6 @@ describe('StreamrClient', function() {
791791
})
792792
})
793793
})
794-
})
795-
796-
describe("disconnect", function() {
797-
798-
it('should disconnect the socket', function(done) {
799-
client.connect()
800-
client.socket.disconnect = done
801-
802-
client.socket.once('connect', function() {
803-
client.disconnect()
804-
})
805-
})
806-
807-
it('should report that it is not connected and not connecting after disconnecting', function(done) {
808-
client.connect()
809-
810-
client.socket.once('connect', function() {
811-
client.disconnect()
812-
})
813-
814-
client.socket.once('disconnect', function() {
815-
assert(!client.isConnected())
816-
assert(!client.connecting)
817-
done()
818-
})
819-
})
820794

821795
it('should disconnect when no longer subscribed to any streams', function(done) {
822796
client.options.autoDisconnect = true
@@ -839,6 +813,30 @@ describe('StreamrClient', function() {
839813
})
840814
})
841815

816+
it('should disconnect if all subscriptions are done during resend', function(done) {
817+
client.options.autoDisconnect = true
818+
819+
var sub1 = client.subscribe("stream1", function(message) {}, {resend_all: true})
820+
client.connect()
821+
822+
client.socket.on('resend', function(request) {
823+
async(function() {
824+
client.socket.emit('resending', {
825+
channel: request.channel,
826+
sub: request.sub,
827+
from: 0,
828+
to: 0
829+
})
830+
client.socket.emit('ui', byeMsg(request.channel,0))
831+
client.socket.emit('resent', {channel: request.channel, sub: request.sub, from:0, to:0})
832+
})
833+
})
834+
835+
client.socket.on('disconnect', function() {
836+
done()
837+
})
838+
})
839+
842840
it('should not disconnect if autoDisconnect is set to false', function(done) {
843841
client.options.autoDisconnect = false
844842

@@ -858,6 +856,32 @@ describe('StreamrClient', function() {
858856
throw "Should not have disconnected!"
859857
})
860858
})
859+
})
860+
861+
describe("disconnect", function() {
862+
863+
it('should disconnect the socket', function(done) {
864+
client.connect()
865+
client.socket.disconnect = done
866+
867+
client.socket.once('connect', function() {
868+
client.disconnect()
869+
})
870+
})
871+
872+
it('should report that it is not connected and not connecting after disconnecting', function(done) {
873+
client.connect()
874+
875+
client.socket.once('connect', function() {
876+
client.disconnect()
877+
})
878+
879+
client.socket.once('disconnect', function() {
880+
assert(!client.isConnected())
881+
assert(!client.connecting)
882+
done()
883+
})
884+
})
861885

862886
it('should reset subscriptions when calling disconnect()', function(done) {
863887
client.subscribe("stream1", function(message) {})

0 commit comments

Comments
 (0)