-
Notifications
You must be signed in to change notification settings - Fork 331
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 (#499)
* 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. * Upgrade terser and its rollup plugin to support private method's syntax * Build latest turbo version from the page-refreshes branch in hotwired/turbo * Update with last turbo build * Debounce jobs to broadcast page refresh signals A page refresh stream signals the need to reload the page. It's a global action that makes sense to aggregate when multiple signals are generated in a short period of time for a given streamable. This implementation is based on creating a thread-level debouncer associated to the set of streamables. The debouncer is implemented using concurrent-ruby's scheduled tasks. * Build latest turbo version from the page-refreshes branch in hotwired/turbo * Update with last changes * Commit js changes * Build with latest turbo master * Make wait noop if scheduled_task is nil This can happen when the Debouncer has finished its work and we clear it from the current thread. The next call to refresh_debouncer_for creates a new Debouncer, but it doesn't have a scheduled_task yet. We only use the wait method in tests, to ensure that the debounces has finished its work. But if the scheduled_task is nil, we know that the debouncer has already finished its work. * Build with latest turbo master * Build with latest turbo/main * Don't include JS changes in this branch * Use released Turbo version --------- Co-authored-by: Jorge Manrubia <jorge.manrubia@gmail.com> Co-authored-by: Jorge Manrubia <jorge@hey.com>
- Loading branch information
1 parent
e44b6a9
commit 7800f38
Showing
25 changed files
with
744 additions
and
129 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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
Oops, something went wrong.