Skip to content
Open
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
25 changes: 24 additions & 1 deletion app/javascript/controllers/refresh_room_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { cable } from "@hotwired/turbo-rails"
import { pageIsTurboPreview } from "helpers/turbo_helpers"

const OFFLINE_AFTER_DISCONNECTED_TIMEOUT = 5_000
const RECONNECT_AFTER_DISCONNECTED_TIMEOUT = 1_000
const RECONNECT_AFTER_DISCONNECTED_JITTER = 1_000
const REFRESH_AFTER_HIDDEN_TIMEOUT = 60_000

export default class extends Controller {
Expand All @@ -12,6 +14,7 @@ export default class extends Controller {

#lastLoadedAt
#offlineTimer = null
#reconnectTimer = null
#hiddenAt = null

async connect() {
Expand All @@ -21,12 +24,13 @@ export default class extends Controller {

this.channel = await cable.subscribeTo({ channel: "HeartbeatChannel" }, {
connected: this.#channelConnected.bind(this),
disconnected: this.#channelDisconnected.bind(this)
disconnected: this.#channelDropped.bind(this)
})
}
}

disconnect() {
clearTimeout(this.#reconnectTimer)
this.channel?.unsubscribe()
}

Expand Down Expand Up @@ -56,15 +60,34 @@ export default class extends Controller {
this.#refresh("connection")

clearTimeout(this.#offlineTimer)
clearTimeout(this.#reconnectTimer)
this.dispatch("online", { target: window })
}

#channelDisconnected() {
clearTimeout(this.#offlineTimer)
this.#offlineTimer = setTimeout(() => {
this.dispatch("offline", { target: window })
}, OFFLINE_AFTER_DISCONNECTED_TIMEOUT)
}

#channelDropped({ willAttemptReconnect } = {}) {
this.#channelDisconnected()
if (willAttemptReconnect) this.#reconnectSoon()
}

// Action Cable leaves a dropped connection alone for at least six seconds, so that the clients
// of a server that went away don't all come back at once. But the server is often still there:
// it was replaced behind a proxy, or it closed the connection itself, as it does to the members
// of a room that's deleted. So try once, sooner, each client at a moment of its own. If that
// fails, Action Cable carries on as it always has.
#reconnectSoon() {
clearTimeout(this.#reconnectTimer)
this.#reconnectTimer = setTimeout(() => {
this.channel.consumer.connect()
}, RECONNECT_AFTER_DISCONNECTED_TIMEOUT + Math.random() * RECONNECT_AFTER_DISCONNECTED_JITTER)
}

#refresh(reason) {
get(this.urlValue, { query: { since: this.#lastLoadedAt, reason: reason }, responseKind: "turbo-stream" })
}
Expand Down
43 changes: 43 additions & 0 deletions test/system/reconnecting_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require "application_system_test_case"

class ReconnectingTest < ApplicationSystemTestCase
setup do
sign_in "jz@37signals.com"
join_room rooms(:designers)
end

test "a connection the server drops is back before the room goes offline" do
users(:jz).reset_remote_connections
assert_cable_disconnected

# The room goes offline, and the composer with it, five seconds after the connection is lost.
assert_selector "turbo-cable-stream-source[connected]", count: 3, visible: false, wait: 4
end

test "a connection the server closes for good stays closed" do
ActionCable.server.remote_connections.where(current_user: users(:jz)).disconnect reconnect: false
assert_cable_disconnected

sleep 2.5 # Past the moment a dropped connection is tried again.
assert_cable_disconnected
end

test "a room that loses its connection twice before getting it back isn't left offline" do
page.execute_script(<<~JS)
const element = document.querySelector("[data-controller~='refresh-room']")
const { channel } = Stimulus.getControllerForElementAndIdentifier(element, "refresh-room")

channel.disconnected({ willAttemptReconnect: false })
channel.disconnected({ willAttemptReconnect: false })
channel.connected()
JS

sleep 5.5 # Past the moment a lost connection takes the room offline.
assert_no_selector "fieldset[data-composer-target='fields'][disabled]"
end

private
def assert_cable_disconnected
assert_no_selector "turbo-cable-stream-source[connected]", visible: false
end
end
Loading