# `Macaw.Live.Assets`
[🔗](https://github.com/dcoai/macaw_live/blob/v0.1.0/lib/macaw/live/assets.ex#L1)

What a page loads from its own origin (spec/06 §2, §3): the generated stylesheets the
runtime registers at mount and the links to them. Copied from visualize's
`Visualize.Sheets` and adapted: the store is a behavior over an argument
(`Macaw.Live.Assets.Store`), its default table the supervisor's
(`Macaw.Live.Assets.Store.Memory`), and a sheet's path is
`Macaw.Component.Stylesheet.path/1`'s, `/assets/<hex>.css`, `hex` the SHA-256 of its bytes.

**The runtime registers every stylesheet a mounted cell declares** when it mounts,
before the cell renders (`put_stylesheet/2`), with the surface's layout sheet and its
theme's, so a page under `style-src 'self'` finds each one. `Macaw.Live.Assets.Plug`
serves them, and `<Macaw.Live.stylesheets macaw={@macaw} />` links them.

# `file`

```elixir
@type file() :: %{
  app: String.t(),
  path: String.t(),
  version: String.t(),
  type: String.t(),
  bytes: binary()
}
```

A declared file as the assets plug serves it.

# `sheets`

```elixir
@type sheets() :: %{layout: String.t(), theme: String.t(), cells: [String.t()]}
```

One mounted surface's sheets, each a path: its layout's, its theme's, its cells'.

# `index`

```elixir
@spec index(Macaw.Registry.t()) ::
  {:ok, %{required({String.t(), String.t()}) =&gt; file()}}
  | {:error, [Macaw.Base.Fault.t(), ...]}
```

The index of static files the assets plug serves (spec/06 §3): every `hooks:` and
`assets:` path each component of the registry's enabled packages declares, each read
once and digested, by `{app, path}`. `{:ok, index}`, or every fault at once: a
package's own at `[package]`, and `url/2`'s for a file at `[:files, component, path]`.
Nothing outside a declaration is in it, and macaw_component refused `..` in a declared
path at compile time.

# `links`

```elixir
@spec links(map()) :: Phoenix.LiveView.Rendered.t()
```

The page's stylesheet links (spec/06 §2.3), as `Macaw.Live.stylesheets/1`: one
`<link rel="stylesheet" href="…">` per path of `stylesheets/1`. The host places it once
in its layout; it changes only when a surface is mounted or unmounted.

    <Macaw.Live.stylesheets macaw={@macaw} />

## Attributes

* `macaw` (`Macaw.Live.Mounted`) (required) - the `@macaw` assign mount/3 set.

# `put_route`

```elixir
@spec put_route(atom(), String.t(), Macaw.Component.Proxy.route()) ::
  {:ok, String.t()} | {:error, [Macaw.Base.Fault.t(), ...]}
```

Registers a component's proxy route in the asset store of the supervisor `live`
(spec/06 §1.2), under its component, its name and its digest
(`Macaw.Component.Proxy.digest/1`): `{:ok, digest}`. The runtime registers every route a
mounted cell declares before the cell renders a path to it; a request can only look one
up. Registering one already held is a no-op.

Faults: `{[:live], {:not_running, live}}`, `{[:component], {:type, :string}}`, a route's
own (`{[:route | path], reason}` as macaw_component words them), and `{[:assets],
{:store, reason}}`.

# `put_stylesheet`

```elixir
@spec put_stylesheet(atom(), String.t()) ::
  {:ok, String.t()} | {:error, [Macaw.Base.Fault.t(), ...]}
```

Registers a generated stylesheet in the asset store of the supervisor `live`
(spec/06 §2.1): `{:ok, path}`, its content path `/assets/<hex>.css`
(`Macaw.Component.Stylesheet.path/1`). Registering one already held is a no-op.

Faults: `{[:live], {:not_running, live}}`; `{[:css], :not_a_string}`, and `{[:css],
:origin}` for an `@import` or `url(` that isn't same-origin
(`Macaw.Component.Stylesheet.fault/1`); `{[:assets], {:store, reason}}` for a store that
answered an error.

    {:ok, "/assets/" <> _} = Macaw.Live.Assets.put_stylesheet(MyApp.Macaw, ".a{opacity:1}")

# `route`

```elixir
@spec route(atom(), String.t(), String.t(), String.t()) ::
  {:ok, Macaw.Component.Proxy.route()} | :error
```

A registered proxy route by its component, name and digest: `{:ok, route}`, or `:error`
for one not registered, a supervisor not running or anything else.

# `stylesheet`

```elixir
@spec stylesheet(atom(), String.t()) :: {:ok, String.t()} | :error
```

The registered stylesheet of a hex name, the 64 lowercase hex digits of its path:
`{:ok, css}`, or `:error` for a name not registered, a supervisor not running or
anything else.

# `stylesheets`

```elixir
@spec stylesheets(Macaw.Live.Mounted.t()) :: [String.t()]
```

The stylesheets a page links (spec/06 §2.3), each path once, in this order:
macaw_component's drawn-state notices (`Macaw.Component.Drawn.stylesheet_url/0`), each
mounted surface's layout sheet in mount order, their themes', then every generated sheet
of every mounted cell, surface by surface in reading order.

# `url`

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

The static URL of a file a component declares (spec/06 §3), by any module of its package
and its path relative to the package's application directory:
`{:ok, "/macaw/static/<app>/<path>?v=<hex>"}`, `hex` the first 16 hex digits of the file's
SHA-256, so a changed file is a new URL and the old one stays cacheable forever.

The file is the application directory's, or, for a package not built into one (a
project's own components, run from its checkout), its source root's: the nearest
directory above the module's source holding a `mix.exs`, where macaw_component checked
it at compile time.

Faults: `{[], {:no_application, module}}` for a module in no application, `{[],
{:unreadable, path}}` for a file neither directory holds, `{[:module], {:type, :atom}}`
and `{[:path], {:type, :string}}`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
