Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions app/assets/javascripts/turbo.js
Original file line number Diff line number Diff line change
Expand Up @@ -5421,6 +5421,10 @@ function walk(obj) {

class TurboCableStreamSourceElement extends HTMLElement {
static observedAttributes=[ "channel", "signed-stream-name" ];
constructor() {
super();
this.addEventListener("turbo:before-morph-attribute", this.preserveConnectedAttribute);
}
async connectedCallback() {
connectStreamSource(this);
this.subscription = await subscribeTo(this.channel, {
Expand All @@ -5446,6 +5450,11 @@ class TurboCableStreamSourceElement extends HTMLElement {
});
return this.dispatchEvent(event);
}
preserveConnectedAttribute(event) {
if (event.target === this && event.detail.attributeName === "connected") {
event.preventDefault();
}
}
subscriptionConnected() {
this.setAttribute("connected", "");
}
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/turbo.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app/assets/javascripts/turbo.min.js.map

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions app/javascript/turbo/cable_stream_source_element.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import snakeize from "./snakeize"
class TurboCableStreamSourceElement extends HTMLElement {
static observedAttributes = ["channel", "signed-stream-name"]

constructor() {
super()
this.addEventListener("turbo:before-morph-attribute", this.preserveConnectedAttribute)
}

async connectedCallback() {
connectStreamSource(this)
this.subscription = await subscribeTo(this.channel, {
Expand Down Expand Up @@ -32,6 +37,12 @@ class TurboCableStreamSourceElement extends HTMLElement {
return this.dispatchEvent(event)
}

preserveConnectedAttribute(event) {
if (event.target === this && event.detail.attributeName === "connected") {
event.preventDefault()
}
}

subscriptionConnected() {
this.setAttribute("connected", "")
}
Expand Down
2 changes: 2 additions & 0 deletions test/dummy/app/views/messages/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<%= turbo_refreshes_with method: :morph %>

<h1>Message #<%= @message.id %></h1>

<%= turbo_stream_from @message, style: "display: none" %>
Expand Down
13 changes: 13 additions & 0 deletions test/system/broadcasts_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,19 @@ class BroadcastsTest < ApplicationSystemTestCase
assert_no_text original.content
end

test "the turbo-cable-stream-source stays connected through a morph refresh" do
message = Message.create!(content: "original content")

visit message_path(message)
assert_turbo_cable_stream_source message, connected: true

message.update!(content: "refreshed content")
message.broadcast_refresh

assert_text message.content
assert_turbo_cable_stream_source message, connected: true
end

private

def reconnect_cable_stream_source(from:, to:)
Expand Down
Loading