Boam.JS
(boam v0.1.2)
Copy Markdown
Helpers for generating JavaScript shim code around the beam.call(...) bridge.
The main entrypoint is export_prelude/1, which takes a nested export tree and
returns JavaScript source that installs functions on globalThis.
This is useful in two modes:
- pass the generated string through
prelude:when you want manual control over dispatching - let
Boam.Runtimedo the same work automatically withexpose:
Examples
Generate a manual prelude:
prelude =
Boam.JS.export_prelude(%{
console: %{
log: true,
error: {:dispatch, "logger.error"}
}
})That produces JavaScript equivalent to:
if (globalThis["console"] == null) { globalThis["console"] = {}; }
globalThis["console"]["log"] = (...args) => beam.call("console.log", ...args);
globalThis["console"]["error"] = (...args) => beam.call("logger.error", ...args);
Summary
Functions
Builds a JavaScript prelude that exposes nested shim functions on globalThis.
Types
@type export_tree() :: %{optional(path_key()) => export_tree() | leaf()} | keyword(export_tree() | leaf())
@type leaf() :: true | Boam.Dispatcher.handler() | {:dispatch, atom() | String.t()} | {:dispatch, atom() | String.t(), Boam.Dispatcher.handler()}
Leaf spec for a generated JavaScript function.
truederives the dispatch name from the nested path- a
Dispatcher.handler/0also derives the dispatch name from the path {:dispatch, name}generates a shim without attaching a handler{:dispatch, name, handler}uses a custom dispatch name and handler
Functions
@spec export_prelude(export_tree()) :: String.t()
Builds a JavaScript prelude that exposes nested shim functions on globalThis.
Leaf dispatch names default to the dot-joined path. For example:
Boam.JS.export_prelude(%{
console: %{
log: true
}
})defines globalThis.console.log = (...args) => beam.call("console.log", ...args).