Phoenix.ReactServer.Runtime behaviour (Phoenix.ReactServer NG v0.8.5)

Copy Markdown View Source

Dynamic supervisor for JavaScript runtime processes and file watching.

This module manages the lifecycle of JavaScript runtime processes (Bun/Deno) and coordinates file watching for hot reloading in development mode.

Features

  • Runtime Management: Supervises JavaScript runtime processes
  • Hot Reloading: Automatic component rebuilding in development
  • File Watching: Monitors component directory changes with throttling
  • Graceful Restart: Handles runtime failures and restarts
  • Development Mode: Enhanced developer experience with live reloading

Runtime Lifecycle

Development Mode

  1. Starts file watcher for component directory
  2. Monitors file changes with 3-second throttle
  3. Triggers automatic runtime restart on component changes
  4. Provides hot reloading for seamless development

Production Mode

  1. Starts optimized runtime processes
  2. No file watching (minimal overhead)
  3. Uses pre-bundled components for performance

Supported Runtimes

Configuration

Runtime behavior is controlled via application configuration:

config :phoenix_react_ng, Phoenix.ReactServer.Runtime.Bun,
  env: :dev,  # :dev or :prod
  port: 5225,
  component_base: "/path/to/components"

Performance Considerations

  • Development mode incurs file watching overhead
  • Throttling prevents excessive rebuilds during rapid changes
  • Runtime processes are isolated for stability
  • Automatic restart ensures reliability

Summary

Types

Component name

Rendered HTML output

Rendering method

File system path

Render response

Runtime configuration

Runtime start arguments

t()

Runtime state structure

Callbacks

Returns runtime configuration.

Renders a component using the specified method.

Starts the runtime with given arguments.

Starts a file watcher for the component directory.

Functions

Returns a specification to start this module under a supervisor.

Callback implementation for DynamicSupervisor.init/1.

Types

component()

@type component() :: String.t()

Component name

html()

@type html() :: String.t()

Rendered HTML output

method()

@type method() ::
  :render_to_readable_stream | :render_to_string | :render_to_static_markup

Rendering method

path()

@type path() :: Path.t()

File system path

render_response()

@type render_response() :: {:ok, html()} | {:error, term()}

Render response

runtime_config()

@type runtime_config() :: keyword()

Runtime configuration

start_args()

@type start_args() :: [component_base: path(), render_timeout: timeout()]

Runtime start arguments

t()

@type t() :: %Phoenix.ReactServer.Runtime{
  cd: Path.t(),
  component_base: Path.t(),
  render_timeout: timeout(),
  runtime_port: port(),
  server_js: Path.t()
}

Runtime state structure

Callbacks

config()

@callback config() :: runtime_config()

Returns runtime configuration.

get_rendered_component(method, component, map, t)

@callback get_rendered_component(method(), component(), map(), t()) :: render_response()

Renders a component using the specified method.

start(start_args)

@callback start(start_args()) :: port()

Starts the runtime with given arguments.

Returns a port that can be used to communicate with the runtime process.

start_file_watcher(path)

@callback start_file_watcher(path()) :: {:ok, pid()} | {:error, term()}

Starts a file watcher for the component directory.

Functions

child_spec(arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

init(init_arg)

Callback implementation for DynamicSupervisor.init/1.

start_file_watcher(args)

start_link(init_arg)

start_runtime(runtime, args)