A lightweight JS execution context on a shared runtime thread.
Unlike a full runtime, a context does not spawn a dedicated OS thread.
Many contexts share a single JSRuntime thread managed by a
QuickBEAM.ContextPool. This makes each context ~58 KB (bare) to
~429 KB (full browser APIs) vs ~2 MB+ for a full runtime — ideal for
per-connection state in Phoenix LiveView.
Example
{:ok, pool} = QuickBEAM.ContextPool.start_link()
{:ok, ctx} = QuickBEAM.Context.start_link(pool: pool)
{:ok, 3} = QuickBEAM.Context.eval(ctx, "1 + 2")
QuickBEAM.Context.stop(ctx)With LiveView
def mount(_params, _session, socket) do
{:ok, ctx} = QuickBEAM.Context.start_link(
pool: MyApp.JSPool,
handlers: %{"db.query" => &MyApp.query/1}
)
{:ok, assign(socket, js: ctx)}
end
def handle_event("click", params, socket) do
{:ok, html} = QuickBEAM.Context.call(socket.assigns.js, "handleClick", [params])
{:noreply, push_event(socket, "update", %{html: html})}
endstart_link/1 links the context to the calling process, so it
automatically terminates (and cleans up its JS context) when the
LiveView process exits. No explicit terminate callback needed.
Summary
Functions
Returns a specification to start this module under a supervisor.
Types
@type t() :: %QuickBEAM.Context{ context_id: pos_integer(), handlers: map(), next_worker_id: pos_integer(), pending: map(), pool: GenServer.server() | nil, pool_resource: reference(), websockets: map(), workers: map() }
Functions
@spec call(GenServer.server(), String.t(), list(), keyword()) :: QuickBEAM.js_result()
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec dom_find(GenServer.server(), String.t()) :: {:ok, tuple() | nil}
@spec dom_find_all(GenServer.server(), String.t()) :: {:ok, list()}
@spec dom_html(GenServer.server()) :: {:ok, String.t()}
@spec dom_text(GenServer.server(), String.t()) :: {:ok, String.t()}
@spec eval(GenServer.server(), String.t(), keyword()) :: {:ok, term()} | {:error, String.t()}
@spec get_global(GenServer.server(), String.t()) :: {:ok, term()}
@spec memory_usage(GenServer.server()) :: {:ok, map()}
@spec reset(GenServer.server()) :: :ok | {:error, String.t()}
@spec send_message(GenServer.server(), term()) :: :ok
@spec set_global(GenServer.server(), String.t(), term()) :: :ok
@spec start_link(keyword()) :: GenServer.on_start()
@spec stop(GenServer.server()) :: :ok