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
20 changes: 20 additions & 0 deletions app/javascript/controllers/messages_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,13 @@ export default class extends Controller {
if (upToDate) {
event.detail.render = async (streamElement) => {
const didScroll = await this.#scrollManager.autoscroll(false, async () => {
const messageIds = this.#messageIdsIn(streamElement)

await render(streamElement)
await nextEventLoopTick()

this.#positionLastMessage()
if (this.#paginator.upToDate) this.#positionMessages(messageIds)
this.#playSoundForLastMessage()
this.#paginator.trimExcessMessages(true)
})
Expand Down Expand Up @@ -183,6 +186,23 @@ export default class extends Controller {
}
}

#messageIdsIn(streamElement) {
return Array.from(streamElement.templateContent.querySelectorAll(".message"), (message) => message.id).filter(Boolean)
}

// The refresh after a dropped connection brings what was missed and, again, what came in meanwhile, which Turbo moves
// to the end with the rest: a message that arrived live since would stay above them all. Only while the list is at
// its end, as one that's catching up pages on from its last message.
#positionMessages(ids) {
for (const id of ids) {
const message = document.getElementById(id)

if (message?.parentElement === this.messagesTarget) {
this.#followingMessage(message)?.before(message)
Comment on lines +200 to +201

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, and reproduced: with a missed message, two sent while disconnected, and a live one put in the list before the refresh renders, the page ended with [live, missed, sent, sent]. Addressed in 0514347: after a stream renders, every message it brought is put back in its place by sort value, which merges the missed and repeated ones into chronological order. The pagination boundary holds because this happens only while the list is at its end; on a merge with #369, doing it regardless fails its test of a refresh that misses more than a page, since paging then starts after the live message. The live-arrival test now includes a missed message, and fails on the previous commit and on main.

Written by Claude Code (AI assistant) on behalf of @namespaceMarcello, who directs this work.

}
}
}

#playSoundForLastMessage() {
const soundTarget = this.#lastMessage.querySelector(".sound")

Expand Down
10 changes: 9 additions & 1 deletion app/javascript/controllers/refresh_room_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default class extends Controller {
static values = { loadedAt: Number, url: String, }

#lastLoadedAt
#connected = false
#offlineTimer = null
#hiddenAt = null

Expand All @@ -30,8 +31,12 @@ export default class extends Controller {
this.channel?.unsubscribe()
}

// Only a message that arrives while connected moves where the next refresh starts. One that arrives while
// disconnected, like the answer to a message sent then, can be newer than others missed meanwhile.
messageTargetConnected(target) {
this.#lastLoadedAt = Math.max(this.#lastLoadedAt, target.dataset.messageUpdatedAt || 0)
if (this.#connected) {
this.#lastLoadedAt = Math.max(this.#lastLoadedAt, target.dataset.messageUpdatedAt || 0)
}
}

visibilityChanged() {
Expand All @@ -53,6 +58,7 @@ export default class extends Controller {
}

#channelConnected() {
this.#connected = true
this.#refresh("connection")

clearTimeout(this.#offlineTimer)
Expand All @@ -63,6 +69,8 @@ export default class extends Controller {
this.#offlineTimer = setTimeout(() => {
this.dispatch("offline", { target: window })
}, OFFLINE_AFTER_DISCONNECTED_TIMEOUT)

this.#connected = false
}

#refresh(reason) {
Expand Down
96 changes: 96 additions & 0 deletions test/system/catching_up_after_a_dropped_connection_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
require "application_system_test_case"

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

using_session("Kevin") do
sign_in "kevin@37signals.com"
join_room rooms(:designers)
end
end

test "a message sent while the connection is down doesn't hide the ones missed before it" do
drop_connection
using_session("Kevin") { send_message "Missed while away" }
assert_no_message_text "Missed while away"

send_message "Sent while away"
restore_connection

assert_message_text "Missed while away", wait: 10
assert_equal [ "Missed while away", "Sent while away" ], texts_from_while_away
end

test "messages missed or sent while the connection is down stay before one that arrives live as it comes back" do
drop_connection
using_session("Kevin") { send_message "Missed while away" }
send_message "Sent while away"
send_message "Sent again while away"
mark_message "Sent again while away"

# As if it arrived live once the connection was back, between the refresh's query and its answer.
execute_script <<~JS, dom_id(rooms(:designers), :messages), 1.minute.from_now.to_fs(:epoch)
document.getElementById(arguments[0]).insertAdjacentHTML("beforeend",
`<div class="message message--formatted" id="message_999999" data-message-id="999999" data-sort-value="${arguments[1]}"><div class="lexxy-content">Live while away</div></div>`)
JS
restore_connection

# The refresh brings the newest one again whatever happens, so its mark goes once the refresh is in.
assert_no_selector ".message[data-marked]", wait: 10
assert_equal [ "Missed while away", "Sent while away", "Sent again while away", "Live while away" ], texts_from_while_away
end

test "messages missed while the connection is down show once it's back, and the ones shown before stay as they are" do
using_session("Kevin") do
send_message "Seen live"
# The refresh asks from the newest message's millisecond, so that one comes again whatever happens.
send_message "Seen live, the newest"
end
mark_message "Seen live"

drop_connection
using_session("Kevin") { send_message "Missed while away" }
assert_no_message_text "Missed while away"

restore_connection

assert_message_text "Missed while away", wait: 10
assert_equal [ "Missed while away" ], texts_from_while_away
assert_selector ".message[data-marked]", text: "Seen live", count: 1
end

private
# The server drops the connection, as it does when the user signs out elsewhere, and Chrome keeps it from coming
# back until restore_connection, while the rest of the page still reaches the server.
def drop_connection
page.driver.browser.execute_cdp "Network.enable"
page.driver.browser.execute_cdp "Network.setBlockedURLs", urls: [ "*/cable*" ]
users(:jz).reset_remote_connections
assert_no_selector "turbo-cable-stream-source[connected]", visible: false
end

def restore_connection
page.driver.browser.execute_cdp "Network.setBlockedURLs", urls: []
# Action Cable tries again on its own schedule, several seconds apart.
assert_selector "turbo-cable-stream-source[connected]", count: 3, visible: false, wait: 30
end

# A refresh that brings the message again replaces its element, and the mark with it.
def mark_message(text)
assert_message_text text
execute_script <<~JS, text
Array.from(document.querySelectorAll(".message")).find((message) => message.textContent.includes(arguments[0])).dataset.marked = "true"
JS
assert_selector ".message[data-marked]", text: text
end

def texts_from_while_away
all(".message .lexxy-content", text: "while away").map(&:text)
end

def assert_no_message_text(text)
assert_no_selector ".message__body", text: text
end
end
Loading