HoloMap.Runtime (HoloMap v0.4.0)

Copy Markdown View Source

The Elixir seam onto the browser runtime.

This is the only module in HoloMap that touches JavaScript. Everything else (components, specifications, event normalisation) is ordinary Elixir that can be unit tested, which is the point: Hologram's JS interop is a no-op on the server and cannot be covered by ExUnit, so the less code lives behind it, the more of the library is actually verifiable.

The functions here are thin on purpose. The reconciliation logic they reach lives in runtime.mjs; see the Architecture guide for how the two halves fit together.

Interop runs in actions only

Hologram executes JS interop exclusively inside action handlers. Calling these functions from init/2, init/3 or a command silently does nothing. On the server they are compiled to no-ops. HoloMap.Map therefore boots through put_action/2 rather than directly from init/3.

Summary

Functions

Returns the list of JS imports declared with js_import/2 in the module.

Creates the MapLibre instance for cid and starts watching its definitions.

Calls method on the underlying maplibregl.Map with args.

Tears the map down and releases its WebGL context.

Documents the JavaScript-side escape hatch. Not callable from Elixir.

Sends a request that HoloMap.API built to the browser runtime, which dispatches the answer to the request's reply action.

Functions

__js_imports__()

@spec __js_imports__() :: [map()]

Returns the list of JS imports declared with js_import/2 in the module.

boot(cid)

@spec boot(String.t()) :: :ok

Creates the MapLibre instance for cid and starts watching its definitions.

Idempotent. Calling it for a map that is already running reconciles that map instead of building a second one, and calling it after a client-side navigation, where the same cid is rendered onto a fresh DOM node, disposes the stale instance first.

The map is built once its container and its own map definition are in the page, which it waits for a frame at a time. If they have not appeared after about a second (60 frames), it gives up with a console warning.

call(cid, method, args \\ [])

@spec call(String.t(), String.t(), list()) :: :ok

Calls method on the underlying maplibregl.Map with args.

Calls made before the map has booted, or before its style finishes loading, are queued and replayed in order once it has, so a camera move issued from a mount action is not lost. Calls for a map that has not booted within five seconds are dropped with a console warning. Calls for a map MapLibre could not build are dropped at once, with a console warning that names MapLibre's error (see "A map MapLibre cannot build" in HoloMap.Map).

This is the general escape hatch. Prefer the named wrappers in HoloMap.API, which document their arguments and normalise option keys; reach for this only for a MapLibre method those do not cover yet.

HoloMap.Runtime.call(cid, "setRenderWorldCopies", [false])

The method and its arguments cross into JavaScript as one JSON string, encoded with HoloMap.JSON, as requests and component specifications do. So an atom among the arguments arrives as a string, whatever its name, rather than being resolved by Hologram's interop as the JavaScript global it may name (:top as window.top). A tuple arrives as a list, a keyword list as a list of [key, value] pairs, and nil as null, as they did through the interop. A term JSON cannot carry, such as a struct (a Hologram.JS.NativeValue among them), a function or a pid, raises ArgumentError here, before anything is sent: hand a JavaScript object to MapLibre from your own JavaScript instead, through the instance global_api/0 describes.

destroy(cid)

@spec destroy(String.t()) :: :ok

Tears the map down and releases its WebGL context.

Normally unnecessary: the runtime observes its own container and disposes the map when Hologram removes it from the page. Use this when a map should stop consuming a context while its container stays mounted. Calls made for the map afterwards are dropped with a console warning until it boots again.

global_api()

@spec global_api() :: :ok

Documents the JavaScript-side escape hatch. Not callable from Elixir.

A MapLibre plugin (a draw control, a geocoder, a deck.gl overlay) needs the raw maplibregl.Map object, and Hologram's interop cannot hand one to a third-party script. Every booted map is therefore published on window:

window.HoloMap.map("explorer")   // the maplibregl.Map, or null
window.HoloMap.cids()            // every live map's cid
window.HoloMap.destroy("explorer")

Reach for it from your own JavaScript when a plugin needs the instance. From Elixir, use HoloMap.API and call/3 instead. They queue correctly against the style's readiness, which a raw handle does not.

request(cid, request)

@spec request(String.t(), map()) :: :ok

Sends a request that HoloMap.API built to the browser runtime, which dispatches the answer to the request's reply action.

Call HoloMap.API.request/4 or one of its typed wrappers instead: they check the request and document the answer. request is the map they build, with string keys:

  • "method" - the MapLibre method to call.
  • "args" - its arguments, as a list.
  • "reply" - the reply handler, as HoloMap.Event.normalize/1 returns it.
  • "source" - optional; a source id, to call the method on that source rather than on the map.
  • "limit" and "geometry" - for a method that finds features, the most features the answer carries and whether they keep their geometry.

The request crosses into JavaScript as one JSON string, encoded with HoloMap.JSON, as component specifications do. So an atom in the arguments or the reply's params arrives as a string, rather than being resolved by Hologram's interop as the JavaScript global it may name, and a term JSON cannot carry, such as a tuple, raises ArgumentError here.