aboutsummaryrefslogtreecommitdiffstats
path: root/actioncable/app/javascript/action_cable/connection_monitor.js
diff options
context:
space:
mode:
authorRichard Macklin <richard.github@nrm.com>2018-01-12 16:49:09 -0800
committerRichard Macklin <richard.github@nrm.com>2018-11-02 08:40:59 -0700
commit0eb6b86e9606cace49afba0b35ec18916c73646e (patch)
tree2d038d66bccb439d683b89a93388e31f967ebd6e /actioncable/app/javascript/action_cable/connection_monitor.js
parent403c001c56e3980e624da2cb1e1e98d667499d40 (diff)
downloadrails-0eb6b86e9606cace49afba0b35ec18916c73646e.tar.gz
rails-0eb6b86e9606cace49afba0b35ec18916c73646e.tar.bz2
rails-0eb6b86e9606cace49afba0b35ec18916c73646e.zip
Refactor decaffeinate output to more natural/idiomatic javascript
- Remove unnecessary Array.from usages from subscriptions.js These were all Arrays before, so Array.from is a no-op - Remove unnecessary IIFEs from subscriptions.js - Manually decaffeinate sample ActionCable code in comments Here the coffeescript -> ES2015 conversion was done by hand rather than using decaffeinate, because these code samples were simple enough. - Refactor ActionCable.Subscription to avoid initClass - Refactor ActionCable.Subscription to use ES2015 default parameters - Refactor ActionCable.ConnectionMonitor to avoid initClass - Refactor ActionCable.ConnectionMonitor to use shorter variations of null checks - Remove unnecessary code created because of implicit returns in ConnectionMonitor This removes the `return` statements that were returning the value of console.log and those from private methods whose return value was not being used. - Refactor ActionCable.Connection to avoid initClass - Refactor Connection#isProtocolSupported and #isState This addresses these three decaffeinate cleanup suggestions: - DS101: Remove unnecessary use of Array.from - DS104: Avoid inline assignments - DS204: Change includes calls to have a more natural evaluation order It also removes the use of Array.prototype.includes, which means we don't have to worry about providing a polyfill or requiring that end users provide one. - Refactor ActionCable.Connection to use ES2015 default parameters - Refactor ActionCable.Connection to use shorter variations of null checks - Remove return statements that return the value of console.log() in ActionCable.Connection - Simplify complex destructure assignment in connection.js decaffeinate had inserted ``` adjustedLength = Math.max(protocols.length, 1) ``` to be safe, but we know that there has to always be at least one protocol, so we don't have to worry about protocols.length being 0 here. - Refactor Connection#getState The decaffeinate translation of this method was not very clear, so we've rewritten it to be more natural. - Simplify destructure assignment in connection.js - Remove unnecessary use of Array.from from action_cable.js.erb - Refactor ActionCable#createConsumer and #getConfig This addresses these two decaffeinate cleanup suggestions: - DS104: Avoid inline assignments - DS207: Consider shorter variations of null checks - Remove unnecessary code created because of implicit returns in action_cable.js.erb This removes the `return` statements that were returning the value of console.log and those from methods that just set and unset the `debugging` flag. - Remove decaffeinate suggestion about avoiding top-level this In this case, the top-level `this` is intentional, so it's okay to ignore this suggestion. - Remove decaffeinate suggestions about removing unnecessary returns I did remove some of the return statements in previous commits, where it seemed appropriate. However, the rest of these should probably remain because the return values have been exposed through the public API. If we want to break that contract, we can do so, but I think it should be done deliberately as part of a breaking-API change (separate from this coffeescript -> ES2015 conversion) - Remove unused `unsupportedProtocol` variable from connection.js Leaving this would cause eslint to fail - Refactor Subscriptions methods to avoid `for` ... `of` syntax Babel transpiles `for` ... `of` syntax to use `Symbol.iterator`, which would require a polyfill in applications that support older browsers. The `for` ... `of` syntax was produced by running `decaffeinate`, but in these instances a simpler `map` should be sufficient and avoid any `Symbol` issues.
Diffstat (limited to 'actioncable/app/javascript/action_cable/connection_monitor.js')
-rw-r--r--actioncable/app/javascript/action_cable/connection_monitor.js79
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
+
})()