-
-
Notifications
You must be signed in to change notification settings - Fork 638
Add async_react_component and cached_async_react_component helpers #2139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5d78a83
Add async_react_component and cached_async_react_component helpers (#…
AbanoubGhadban 8aaf212
Add async components demo with concurrent rendering example
AbanoubGhadban d03434d
Add RBS type signatures and CHANGELOG entry for async component helpers
AbanoubGhadban 38d0dfd
Fix ESLint no-promise-executor-return error in DelayedComponent
AbanoubGhadban a3baa6c
Remove component_name from AsyncValue class
AbanoubGhadban File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,38 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module ReactOnRailsPro | ||
| # AsyncValue wraps an Async task to provide a simple interface for | ||
| # retrieving the result of an async react_component render. | ||
| # | ||
| # @example | ||
| # async_value = async_react_component("MyComponent", props: { name: "World" }) | ||
| # # ... do other work ... | ||
| # html = async_value.value # blocks until result is ready | ||
| # | ||
| class AsyncValue | ||
| attr_reader :component_name | ||
|
|
||
| def initialize(component_name:, task:) | ||
| @component_name = component_name | ||
| @task = task | ||
| end | ||
|
|
||
| # Blocks until result is ready, returns HTML string. | ||
| # If the async task raised an exception, it will be re-raised here. | ||
| def value | ||
| @task.wait | ||
| end | ||
|
|
||
| def resolved? | ||
| @task.finished? | ||
| end | ||
|
|
||
| def to_s | ||
| value.to_s | ||
| end | ||
|
|
||
| def html_safe | ||
| value.html_safe | ||
| end | ||
| end | ||
| end | ||
68 changes: 68 additions & 0 deletions
68
react_on_rails_pro/lib/react_on_rails_pro/concerns/async_rendering.rb
This file contains hidden or 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,68 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module ReactOnRailsPro | ||
| # AsyncRendering enables concurrent rendering of multiple React components. | ||
| # When enabled, async_react_component calls will execute their HTTP requests | ||
| # in parallel instead of sequentially. | ||
| # | ||
| # @example Enable for all actions | ||
| # class ProductsController < ApplicationController | ||
| # include ReactOnRailsPro::AsyncRendering | ||
| # enable_async_react_rendering | ||
| # end | ||
| # | ||
| # @example Enable for specific actions only | ||
| # class ProductsController < ApplicationController | ||
| # include ReactOnRailsPro::AsyncRendering | ||
| # enable_async_react_rendering only: [:show, :index] | ||
| # end | ||
| # | ||
| # @example Enable for all except specific actions | ||
| # class ProductsController < ApplicationController | ||
| # include ReactOnRailsPro::AsyncRendering | ||
| # enable_async_react_rendering except: [:create, :update] | ||
| # end | ||
| # | ||
| module AsyncRendering | ||
| extend ActiveSupport::Concern | ||
|
|
||
| class_methods do | ||
| # Enables async React component rendering for controller actions. | ||
| # Accepts standard Rails filter options like :only and :except. | ||
| # | ||
| # @param options [Hash] Options passed to around_action (e.g., only:, except:) | ||
| def enable_async_react_rendering(**options) | ||
| around_action :wrap_in_async_react_context, **options | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def wrap_in_async_react_context | ||
| require "async" | ||
| require "async/barrier" | ||
|
|
||
| Sync do | ||
| @react_on_rails_async_barrier = Async::Barrier.new | ||
| yield | ||
| check_for_unresolved_async_components | ||
| ensure | ||
| @react_on_rails_async_barrier&.stop | ||
| @react_on_rails_async_barrier = nil | ||
| end | ||
| end | ||
|
|
||
| def check_for_unresolved_async_components | ||
| return if @react_on_rails_async_barrier.nil? | ||
|
|
||
| pending_tasks = @react_on_rails_async_barrier.size | ||
| return if pending_tasks.zero? | ||
|
|
||
| Rails.logger.error( | ||
| "[React on Rails Pro] #{pending_tasks} async component(s) were started but never resolved. " \ | ||
| "Make sure to call .value on all AsyncValue objects returned by async_react_component " \ | ||
| "or cached_async_react_component. Unresolved tasks will be stopped." | ||
| ) | ||
| end | ||
| end | ||
| end |
27 changes: 27 additions & 0 deletions
27
react_on_rails_pro/lib/react_on_rails_pro/immediate_async_value.rb
This file contains hidden or 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,27 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module ReactOnRailsPro | ||
| # ImmediateAsyncValue is returned when a cached_async_react_component call | ||
| # has a cache hit. It provides the same interface as AsyncValue but returns | ||
AbanoubGhadban marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # the cached value immediately without any async operations. | ||
| # | ||
| class ImmediateAsyncValue | ||
| def initialize(value) | ||
| @value = value | ||
| end | ||
|
|
||
| attr_reader :value | ||
|
|
||
| def resolved? | ||
| true | ||
| end | ||
|
|
||
| def to_s | ||
| @value.to_s | ||
| end | ||
|
|
||
| def html_safe | ||
| @value.html_safe | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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,18 @@ | ||
| module ReactOnRailsPro | ||
| class AsyncValue | ||
| attr_reader component_name: String | ||
|
|
||
| @task: untyped | ||
| @component_name: String | ||
|
|
||
| def initialize: (component_name: String, task: untyped) -> void | ||
|
|
||
| def value: () -> untyped | ||
|
|
||
| def resolved?: () -> bool | ||
|
|
||
| def to_s: () -> String | ||
|
|
||
| def html_safe: () -> untyped | ||
| end | ||
| end |
15 changes: 15 additions & 0 deletions
15
react_on_rails_pro/sig/react_on_rails_pro/concerns/async_rendering.rbs
This file contains hidden or 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,15 @@ | ||
| module ReactOnRailsPro | ||
| module AsyncRendering | ||
| module ClassMethods | ||
| def enable_async_react_rendering: (**untyped options) -> void | ||
| end | ||
|
|
||
| @react_on_rails_async_barrier: untyped | ||
|
|
||
| private | ||
|
|
||
| def wrap_in_async_react_context: () { () -> untyped } -> untyped | ||
|
|
||
| def check_for_unresolved_async_components: () -> void | ||
| end | ||
| end |
15 changes: 15 additions & 0 deletions
15
react_on_rails_pro/sig/react_on_rails_pro/immediate_async_value.rbs
This file contains hidden or 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,15 @@ | ||
| module ReactOnRailsPro | ||
| class ImmediateAsyncValue | ||
| attr_reader value: untyped | ||
|
|
||
| @value: untyped | ||
|
|
||
| def initialize: (untyped value) -> void | ||
|
|
||
| def resolved?: () -> bool | ||
|
|
||
| def to_s: () -> String | ||
|
|
||
| def html_safe: () -> untyped | ||
| end | ||
| end |
This file contains hidden or 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
34 changes: 34 additions & 0 deletions
34
react_on_rails_pro/spec/dummy/app/views/pages/pro/async_components_demo.html.erb
This file contains hidden or 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,34 @@ | ||
| <h1>Async React Components Demo</h1> | ||
| <p> | ||
| This page renders 10 React components, each with a 1-second delay. | ||
| <br> | ||
| <strong>Sequential rendering:</strong> ~10 seconds | ||
| <br> | ||
| <strong>Concurrent rendering (async_react_component):</strong> ~1 second | ||
| </p> | ||
|
|
||
| <% start_time = Time.now %> | ||
|
|
||
| <% | ||
| # Start all 10 async renders immediately (non-blocking) | ||
| components = 10.times.map do |i| | ||
| async_react_component( | ||
| "DelayedComponent", | ||
| props: { index: i + 1, delayMs: 1000 }, | ||
| prerender: true | ||
| ) | ||
| end | ||
| %> | ||
|
|
||
| <div id="components-container"> | ||
| <% components.each do |component| %> | ||
| <%= component.value %> | ||
| <% end %> | ||
| </div> | ||
|
|
||
| <% elapsed = Time.now - start_time %> | ||
| <p> | ||
| <strong>Total render time:</strong> <%= (elapsed * 1000).round %>ms | ||
| <br> | ||
| <em>If this is close to 1 second instead of 10 seconds, concurrent rendering is working!</em> | ||
| </p> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.