Module: ReactOnRailsPro::AsyncRendering

Extended by:
ActiveSupport::Concern
Defined in:
lib/react_on_rails_pro/concerns/async_rendering.rb,
sig/react_on_rails_pro/concerns/async_rendering.rbs

Overview

AsyncRendering enables concurrent rendering of multiple React components. When enabled, async_react_component calls will execute their HTTP requests in parallel instead of sequentially.

Examples:

Enable for all actions

class ProductsController < ApplicationController
  include ReactOnRailsPro::AsyncRendering
  enable_async_react_rendering
end

Enable for specific actions only

class ProductsController < ApplicationController
  include ReactOnRailsPro::AsyncRendering
  enable_async_react_rendering only: [:show, :index]
end

Enable for all except specific actions

class ProductsController < ApplicationController
  include ReactOnRailsPro::AsyncRendering
  enable_async_react_rendering except: [:create, :update]
end

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#check_for_unresolved_async_componentsvoid

This method returns an undefined value.



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/react_on_rails_pro/concerns/async_rendering.rb', line 73

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

#wrap_in_async_react_context { ... } ⇒ Object

Yields:

Yield Returns:

  • (Object)

Returns:

  • (Object)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/react_on_rails_pro/concerns/async_rendering.rb', line 56

def wrap_in_async_react_context
  require "async"
  require "async/barrier"
  parent_context = ReactOnRailsPro::OpenTelemetry.capture_context

  Sync do
    ReactOnRailsPro::OpenTelemetry.with_context(parent_context) 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
end