Tyrex.Inline (Tyrex v0.3.0)

Copy Markdown View Source

Process-local runtime management for the ~JS sigil.

Stores the current Tyrex runtime reference in the process dictionary so ~JS sigils don't need to specify a runtime on every call.

Usage

import Tyrex.Sigil

{:ok, pid} = Tyrex.start()
Tyrex.Inline.set_runtime(pid)

{:ok, 3} = ~JS"1 + 2"

Scoped Runtime

Tyrex.Inline.with_runtime(pid, fn ->
  {:ok, 3} = ~JS"1 + 2"
end)

Summary

Functions

Evaluate JavaScript code on the current process's runtime.

Get the current Tyrex runtime for this process, or nil if not set.

Set the Tyrex runtime for the current process.

Execute a function with a temporary runtime binding.

Functions

eval(code, opts \\ [])

@spec eval(binary(), Keyword.t()) :: {:ok, term()} | {:error, Tyrex.Error.t()}

Evaluate JavaScript code on the current process's runtime.

This is called by the ~JS sigil — you typically don't call it directly.

get_runtime()

@spec get_runtime() :: pid() | atom() | nil

Get the current Tyrex runtime for this process, or nil if not set.

set_runtime(pid_or_name)

@spec set_runtime(pid() | atom()) :: pid() | atom() | nil

Set the Tyrex runtime for the current process.

Accepts a PID or a registered name.

Tyrex.Inline.set_runtime(pid)
Tyrex.Inline.set_runtime(MyApp.JS)

with_runtime(pid_or_name, fun)

@spec with_runtime(pid() | atom(), (-> result)) :: result when result: var

Execute a function with a temporary runtime binding.

The previous runtime (if any) is restored after the function returns.

Tyrex.Inline.with_runtime(pid, fn ->
  {:ok, 3} = ~JS"1 + 2"
end)