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.Runtime do the same work automatically with expose:

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

Types

Leaf spec for a generated JavaScript function.

Functions

Builds a JavaScript prelude that exposes nested shim functions on globalThis.

Types

export_tree()

@type export_tree() ::
  %{optional(path_key()) => export_tree() | leaf()}
  | keyword(export_tree() | leaf())

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.

  • true derives the dispatch name from the nested path
  • a Dispatcher.handler/0 also 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

path_key()

@type path_key() :: atom() | String.t()

Functions

export_prelude(tree)

@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).