Macaw.Live.Hooks (macaw_live v0.1.0)

Copy Markdown View Source

The generated hooks module (spec/05 §2): one ES module that imports every enabled package's static hooks and the live module's, so a host registers them with its LiveSocket in one import and changes nothing else. Adapted from visualize's Visualize.Hooks (js_code/0, install!/0): the text is generated from the registry, never a bundle of hook sources, and each hook file is imported by its static URL as its default export (macaw_component spec/01 §1.8).

import { hooks as macaw, dom } from "/macaw/hooks.js";
const liveSocket = new LiveSocket("/live", Socket, { hooks: { ...macaw, ...myHooks }, dom, params });

Macaw.Live.Assets.Plug builds the text once at start and serves it at url/0, with the live module it imports, priv/static/macaw_live/live.js, at live_url/0 (spec/05 §1, spec/06 §3); each hook file's URL is Macaw.Live.Assets.url/2's.

Summary

Functions

The live module's path in macaw_live's application directory (spec/05 §1).

Where the live module is served (spec/05 §1).

The generated module's text for a registry (spec/05 §2), {:ok, text}.

As module/1, the text alone, for a host's boot: raises an ArgumentError naming every fault.

Where the generated module is served (spec/06 §3).

Functions

live_path()

@spec live_path() :: String.t()

The live module's path in macaw_live's application directory (spec/05 §1).

live_url()

@spec live_url() :: String.t()

Where the live module is served (spec/05 §1).

module(registry)

@spec module(Macaw.Registry.t()) ::
  {:ok, String.t()} | {:error, [Macaw.Base.Fault.t(), ...]}

The generated module's text for a registry (spec/05 §2), {:ok, text}.

It imports the live module's hooks and dom from live_url/0, then each hook of the registry's packages (Macaw.Registry.hooks/1) by its static URL, /macaw/static/<app>/<path>?v=<hex>, hex the first 16 hex digits of the file's SHA-256 (spec/06 §3), and exports hooks, MacawSurface first and every other in name order, and dom. One registry gives the same text, byte for byte.

Its faults are Macaw.Registry.hooks/1's, among them {[], {:duplicate_hook, name, [component, component]}} for two hooks of one name; {[], {:reserved_hook, "MacawSurface"}} for a hook taking the live module's name; {[:hooks, name], {:unreadable, path}} for a declared file the package's directory doesn't hold and {[:hooks, name], {:no_application, package}} for a package in no application; and {[], {:type, :registry}} for anything but a registry.

module!(registry)

@spec module!(Macaw.Registry.t()) :: String.t()

As module/1, the text alone, for a host's boot: raises an ArgumentError naming every fault.

url()

@spec url() :: String.t()

Where the generated module is served (spec/06 §3).