Skip to content
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: 25 additions & 2 deletions assets/js/phoenix.js
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,7 @@ export class Socket {
this.flushSendBuffer()
this.reconnectTimer.reset()
this.resetHeartbeat()
this.resetChannelTimers()
this.stateChangeCallbacks.open.forEach( callback => callback() )
}

Expand Down Expand Up @@ -986,6 +987,15 @@ export class Socket {
}
})
}

/**
* @private
*/
resetChannelTimers() {
this.channels.forEach(channel => {
channel.rejoinTimer.restart()
})
}
}


Expand Down Expand Up @@ -1346,18 +1356,31 @@ class Timer {

reset(){
this.tries = 0
clearTimeout(this.timer)
this.clearTimer()
}

restart(){
const processing = this.timer !== null
this.reset()
if (processing){
this.scheduleTimeout()
}
}

/**
* Cancels any previous scheduleTimeout and schedules callback
*/
scheduleTimeout(){
clearTimeout(this.timer)
this.clearTimer()

this.timer = setTimeout(() => {
this.tries = this.tries + 1
this.callback()
}, this.timerCalc(this.tries + 1))
}

clearTimer() {
clearTimeout(this.timer)
this.timer = null
}
}
19 changes: 19 additions & 0 deletions assets/test/socket_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,25 @@ describe("onConnOpen", () => {
assert.ok(spy.calledOnce)
})

it("resets all channel timers and schedules a timeout if the timer was in progress", () => {
const channel = socket.channel("topic", {})
const channel2 = socket.channel("topic2", {})

channel.rejoinTimer.tries = 1
channel2.rejoinTimer.tries = 2
channel2.rejoinTimer.scheduleTimeout()

assert.equal(channel.rejoinTimer.timer, null)
assert.notEqual(channel2.rejoinTimer.timer, null)

socket.onConnOpen()

assert.equal(channel.rejoinTimer.tries, 0)
assert.equal(channel2.rejoinTimer.tries, 0)
assert.equal(channel.rejoinTimer.timer, null)
assert.notEqual(channel2.rejoinTimer.timer, null)
})

it("triggers onOpen callback", () => {
const spy = sinon.spy()

Expand Down