-
Notifications
You must be signed in to change notification settings - Fork 330
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for page refreshes and broadcasting
This PR is the Rails companion for the Turbo changes to add page refreshes. ```ruby turbo_refreshes_with scroll method: :morph, scroll: :preserve ``` This adds new Active Record helpers to broadcast page refreshes from models: ```ruby class Board broadcast_refreshes end ``` This works great in hierarchical structures, where child record touch parent records automatically to invalidate cache: ```ruby class Column belongs_to :board, touch: true # +Board+ will trigger a page refresh on column changes end ``` You can also specify the streamable declaratively: ```ruby class Column belongs_to :board broadcast_refreshes_to :board end ``` There are also instance-level companion methods to broadcast page refreshes: - `broadcast_refresh_later` - `broadcast_refresh_later_to(*streamables)` This PR introduces a new mechanism to suppress broadcasting of turbo treams for arbitrary blocks of code: ```ruby Recording.suppressing_turbo_broadcasts do ... end ``` When broadcasting page refreshes, the system will automatically debounce multiple calls in a row to only broadcast the last one. This is meant for scenarios where you process records in mass. Because of the nature of such signals, it makes no sense to broadcast them repeatedly and individually.
- Loading branch information
1 parent
1809799
commit 4cb02fc
Showing
18 changed files
with
449 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module Turbo::RequestIdTracking | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
around_action :turbo_tracking_request_id | ||
end | ||
|
||
private | ||
def turbo_tracking_request_id(&block) | ||
Turbo.with_request_id(request.headers["X-Turbo-Request-Id"], &block) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Turbo::Streams::BroadcastStreamJob < ActiveJob::Base | ||
discard_on ActiveJob::DeserializationError | ||
|
||
def perform(stream, content:) | ||
Turbo::StreamsChannel.broadcast_stream_to(stream, content: content) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require "test_helper" | ||
require "action_cable" | ||
|
||
class Turbo::CurrentRequestIdTest < ActiveSupport::TestCase | ||
test "sets the current request id for a block of code" do | ||
assert_nil Turbo.current_request_id | ||
|
||
result = Turbo.with_request_id("123") do | ||
assert_equal "123", Turbo.current_request_id | ||
:the_result | ||
end | ||
|
||
assert_equal :the_result, result | ||
assert_nil Turbo.current_request_id | ||
end | ||
|
||
test "raised errors will raise and clear the current request id" do | ||
assert_nil Turbo.current_request_id | ||
|
||
assert_raise "Some error" do | ||
Turbo.with_request_id("123") do | ||
raise "Some error" | ||
end | ||
end | ||
|
||
assert_nil Turbo.current_request_id | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class RequestIdsController < ApplicationController | ||
def show | ||
render json: { turbo_frame_request_id: Turbo.current_request_id } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<% turbo_exempts_page_from_cache %> | ||
<% turbo_page_requires_reload %> | ||
<%= turbo_refreshes_with method: :morph, scroll: :preserve %> | ||
|
||
<p>Not in the cache!</p> |
Oops, something went wrong.