Class: Ruact::RenderPipeline

Inherits:
Object
  • Object
show all
Defined in:
lib/ruact/render_pipeline.rb

Overview

Internal — orchestrates the full server component render:

ERB source → (preprocessor) → evaluated HTML → (HtmlConverter) → ReactElement tree
                                                                 → (Flight::Renderer) → wire bytes

External code should use ‘Ruact::Controller#ruact_render` instead. `Ruact::RenderPipeline` (and the rest of `Ruact::Flight::*` / `Ruact::Internal::*`) are not part of the public API and may change between minor versions.

Single entry point: #render.

Constant Summary collapse

VALID_MODES =
%i[string stream].freeze

Instance Method Summary collapse

Constructor Details

#initialize(manifest, controller_path: nil, logger: nil) ⇒ RenderPipeline

Returns a new instance of RenderPipeline.



18
19
20
21
22
# File 'lib/ruact/render_pipeline.rb', line 18

def initialize(manifest, controller_path: nil, logger: nil)
  @manifest         = manifest
  @controller_path  = controller_path
  @logger           = logger
end

Instance Method Details

#render(input, mode: :string) ⇒ String, Enumerator

Render a server component tree to Flight wire format.

**Internal API.** External code should call ‘Ruact::Controller#ruact_render` instead. `Ruact::RenderPipeline` is not part of the public API and may change between minor versions without deprecation. Reach into it only when extending the gem itself.

Examples:

ERB source, buffered output

pipeline.render({ erb: "<NavBar />", binding: ctx.instance_eval { binding } }, mode: :string)
# => "0:[\"$\",\"div\",null,...]\n"

Pre-rendered HTML, streaming output

ctx = Ruact::RenderContext.new
pipeline.render({ html: "<!-- __RUACT_0__ -->", render_context: ctx }, mode: :stream)
# => #<Enumerator: ...>  (yields Flight rows lazily)

Parameters:

  • input (Hash)

    selects the input shape — exactly one of:

    • ‘{ erb: String, binding: Binding }` — render an ERB template within the given binding. Both keys required; `:erb` must be a `String`, `:binding` must be a `Binding`.

    • ‘{ html: String, render_context: Ruact::RenderContext }` — convert pre-rendered HTML (from ActionView) using a render context populated during ERB evaluation. Both keys required; `:html` must be a `String`, `:render_context` must be a `Ruact::RenderContext`.

    Extra keys beyond the two-key shapes above are rejected with ‘ArgumentError`.

  • mode (Symbol) (defaults to: :string)

    one of:

    • ‘:string` — returns the full serialized `String`. Deferred chunks are inlined eagerly (no Suspense delay). Suitable for buffered HTTP responses.

    • ‘:stream` — returns an `Enumerator` that yields Flight rows one at a time. Deferred chunks delay. Suitable for `ActionController::Live` streaming.

Returns:

  • (String, Enumerator)

    depending on ‘mode`.

Raises:

  • (ArgumentError)

    when:

    • ‘input` is not a `Hash`,

    • ‘input` mixes `:erb` and `:html` keys,

    • ‘input` omits both `:erb` and `:html`,

    • the required sibling key is missing or has the wrong type (‘:binding` must be a `Binding`; `:render_context` must be a `Ruact::RenderContext`),

    • ‘:erb` or `:html` is not a `String`,

    • ‘input` contains extra keys beyond the documented shapes,

    • ‘mode` is not in VALID_MODES.

    All ‘ArgumentError` messages name the offending input and reference `RenderPipeline#render` for the canonical contract.



64
65
66
67
68
# File 'lib/ruact/render_pipeline.rb', line 64

def render(input, mode: :string)
  validate_mode!(mode)
  enum = build_enum(input, streaming: mode == :stream)
  mode == :string ? enum.to_a.join : enum
end