diff options
Diffstat (limited to 'actioncable/app/javascript/action_cable/connection_monitor.js')
-rw-r--r-- | actioncable/app/javascript/action_cable/connection_monitor.js | 79 |
1 files changed, 34 insertions, 45 deletions
diff --git a/actioncable/app/javascript/action_cable/connection_monitor.js b/actioncable/app/javascript/action_cable/connection_monitor.js index c8d2c62cc8..4d2db5b4ae 100644 --- a/actioncable/app/javascript/action_cable/connection_monitor.js +++ b/actioncable/app/javascript/action_cable/connection_monitor.js @@ -1,33 +1,13 @@ -/* - * decaffeinate suggestions: - * DS102: Remove unnecessary code created because of implicit returns - * DS206: Consider reworking classes to avoid initClass - * DS207: Consider shorter variations of null checks - * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md - */ // Responsible for ensuring the cable connection is in good health by validating the heartbeat pings sent from the server, and attempting // revival reconnections if things go astray. Internal class, not intended for direct user manipulation. -(function() { - let now = undefined - let secondsSince = undefined - let clamp = undefined - const Cls = (ActionCable.ConnectionMonitor = class ConnectionMonitor { - static initClass() { - this.pollInterval = { - min: 3, - max: 30 - } - - this.staleThreshold = 6 - - now = () => new Date().getTime() - - secondsSince = time => (now() - time) / 1000 - - clamp = (number, min, max) => Math.max(min, Math.min(max, number)) - // Server::Connections::BEAT_INTERVAL * 2 (missed two pings) - } +ActionCable.ConnectionMonitor = (function() { + const now = () => new Date().getTime() + + const secondsSince = time => (now() - time) / 1000 + const clamp = (number, min, max) => Math.max(min, Math.min(max, number)) + + class ConnectionMonitor { constructor(connection) { this.visibilityDidChange = this.visibilityDidChange.bind(this) this.connection = connection @@ -40,7 +20,7 @@ delete this.stoppedAt this.startPolling() document.addEventListener("visibilitychange", this.visibilityDidChange) - return ActionCable.log(`ConnectionMonitor started. pollInterval = ${this.getPollInterval()} ms`) + ActionCable.log(`ConnectionMonitor started. pollInterval = ${this.getPollInterval()} ms`) } } @@ -49,45 +29,45 @@ this.stoppedAt = now() this.stopPolling() document.removeEventListener("visibilitychange", this.visibilityDidChange) - return ActionCable.log("ConnectionMonitor stopped") + ActionCable.log("ConnectionMonitor stopped") } } isRunning() { - return (this.startedAt != null) && (this.stoppedAt == null) + return this.startedAt && !this.stoppedAt } recordPing() { - return this.pingedAt = now() + this.pingedAt = now() } recordConnect() { this.reconnectAttempts = 0 this.recordPing() delete this.disconnectedAt - return ActionCable.log("ConnectionMonitor recorded connect") + ActionCable.log("ConnectionMonitor recorded connect") } recordDisconnect() { this.disconnectedAt = now() - return ActionCable.log("ConnectionMonitor recorded disconnect") + ActionCable.log("ConnectionMonitor recorded disconnect") } // Private startPolling() { this.stopPolling() - return this.poll() + this.poll() } stopPolling() { - return clearTimeout(this.pollTimeout) + clearTimeout(this.pollTimeout) } poll() { - return this.pollTimeout = setTimeout(() => { + this.pollTimeout = setTimeout(() => { this.reconnectIfStale() - return this.poll() + this.poll() } , this.getPollInterval()) } @@ -103,16 +83,16 @@ ActionCable.log(`ConnectionMonitor detected stale connection. reconnectAttempts = ${this.reconnectAttempts}, pollInterval = ${this.getPollInterval()} ms, time disconnected = ${secondsSince(this.disconnectedAt)} s, stale threshold = ${this.constructor.staleThreshold} s`) this.reconnectAttempts++ if (this.disconnectedRecently()) { - return ActionCable.log("ConnectionMonitor skipping reopening recent disconnect") + ActionCable.log("ConnectionMonitor skipping reopening recent disconnect") } else { ActionCable.log("ConnectionMonitor reopening") - return this.connection.reopen() + this.connection.reopen() } } } connectionIsStale() { - return secondsSince(this.pingedAt != null ? this.pingedAt : this.startedAt) > this.constructor.staleThreshold + return secondsSince(this.pingedAt ? this.pingedAt : this.startedAt) > this.constructor.staleThreshold } disconnectedRecently() { @@ -121,16 +101,25 @@ visibilityDidChange() { if (document.visibilityState === "visible") { - return setTimeout(() => { + setTimeout(() => { if (this.connectionIsStale() || !this.connection.isOpen()) { ActionCable.log(`ConnectionMonitor reopening stale connection on visibilitychange. visbilityState = ${document.visibilityState}`) - return this.connection.reopen() + this.connection.reopen() } } , 200) } } - }) - Cls.initClass() - return Cls + + } + + ConnectionMonitor.pollInterval = { + min: 3, + max: 30 + } + + ConnectionMonitor.staleThreshold = 6 // Server::Connections::BEAT_INTERVAL * 2 (missed two pings) + + return ConnectionMonitor + })() |