1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/lib/assets/javascripts/cable/connection_monitor.js.coffee
David Heinemeier Hansson 7c6a7f28eb Rename SubscriptionManager/Subscriber -> Subscriptions
This matches the server-side setup and is more consistent.
2015-07-08 11:00:24 +02:00

70 lines
1.3 KiB
CoffeeScript

class Cable.ConnectionMonitor
identifier: Cable.PING_IDENTIFIER
pollInterval:
min: 2
max: 30
staleThreshold:
startedAt: 4
pingedAt: 8
constructor: (@consumer) ->
@consumer.subscriptions.add(this)
@start()
connected: ->
@reset()
@pingedAt = now()
received: ->
@pingedAt = now()
reset: ->
@reconnectAttempts = 0
start: ->
@reset()
delete @stoppedAt
@startedAt = now()
@poll()
stop: ->
@stoppedAt = now()
poll: ->
setTimeout =>
unless @stoppedAt
@reconnectIfStale()
@poll()
, @getInterval()
getInterval: ->
{min, max} = @pollInterval
interval = 4 * Math.log(@reconnectAttempts + 1)
clamp(interval, min, max) * 1000
reconnectIfStale: ->
if @connectionIsStale()
@reconnectAttempts += 1
@consumer.connection.reopen()
connectionIsStale: ->
if @pingedAt
secondsSince(@pingedAt) > @staleThreshold.pingedAt
else
secondsSince(@startedAt) > @staleThreshold.startedAt
toJSON: ->
interval = @getInterval()
connectionIsStale = @connectionIsStale()
{@startedAt, @stoppedAt, @pingedAt, @reconnectAttempts, connectionIsStale, interval}
now = ->
new Date().getTime()
secondsSince = (time) ->
(now() - time) / 1000
clamp = (number, min, max) ->
Math.max(min, Math.min(max, number))