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

A cell's render, proved in a process of its own before the page renders it (spec/01
§3.3). Adapted from visualize's drawing process (`Visualize.Components.Chart`, D-206,
D-216): generic, its outcome a value, with no message text.

**No component's render runs first in the page's process.** A render is a pure function
of its assigns (macaw_component spec/02 §7), so assigns that rendered whole in one
process render in another; the page renders exactly the assigns that were proved, and a
component's raise, loop or runaway heap takes down only its proof.

**The markup proved is the markup placed** (spec/01 §3.3): a proof answers what it
rendered, `t:placed/0`, and the page places it without rendering the component again.
A proof is `Macaw.Data.Bounded.run/2` of `render/3`, under a timeout and a reduction
cap, its process's heap limited with `kill: true`. A failed proof answers
`{:crashed, reason}`:

  * `{:raised, module}`, the exception's module, never its message, which may hold data
    (`:throw` for a throw and `:exit` for an exit, which have none);
  * `:killed`, its heap limit;
  * `:hung`, past its timeout;
  * `{:reductions, spent}`, past its reduction cap.

# `placed`

```elixir
@type placed() ::
  {:safe, binary()} | Macaw.Live.Keyed.t() | Phoenix.LiveView.Rendered.t()
```

What a proof rendered, as the page places it (`render/3`): a safe value written out to
one binary, a keyed render (`Macaw.Live.Keyed`, such as macaw's built-in table answers)
each part written out to a binary of its own, or the `%Phoenix.LiveView.Rendered{}` a HEEx render
answered, proved whole.

# `reason`

```elixir
@type reason() ::
  {:raised, module() | :throw | :exit}
  | :killed
  | :hung
  | {:reductions, pos_integer()}
```

Why a proof failed.

# `contained`

```elixir
@spec contained(module(), atom(), map(), keyword()) ::
  {:ok, term(), placed()} | {:crashed, reason()}
```

A contained cell's value built and its render proved before the socket connects (spec/01
§3.3), where no cell process exists: `{:ok, value, placed}` (`render/3`'s), or
`{:crashed, reason}`.
One bounded process runs the value's whole path as `Macaw.Surface.render/3` builds it,
`init/2`, every delivery in slot order, every bound value in name order (the handed
selection last), then `size/2`, and renders it with the seven assigns. It never ticks.

`inputs`: `settings:` (the cell's validated settings, each bound parameter at its
default), `context:`, `deliveries:` (`%{slot => delivery}`), `bound:` (`%{name =>
value}`, `:selection` for the handed selection), `size:` and `assigns:` (the render
assigns `context`, `settings`, `declarations`, `drawn` and `prepared`). `opts` are `pure/4`'s.

# `pure`

```elixir
@spec pure(module(), atom(), map(), keyword()) ::
  {:ok, placed()} | {:crashed, reason()}
```

A pure cell's render proved (spec/01 §3.3): `{:ok, placed}`, what it rendered as
`render/3` answers it, or `{:crashed, reason}`. `opts`: `timeout:` (ms, `1_000`),
`reductions:` (`50_000_000`) and `heap:` (the process's heap limit in words, none by
default).

# `render`

```elixir
@spec render(module(), atom(), map(), keyword()) :: placed()
```

A component's render, whole, as the page places it (spec/01 §3.3). It is the proof's
work, run in the proof's process; whatever it raises fails the proof.

  * **A safe value** is written out (`Phoenix.HTML.Safe.to_iodata/1`) to one binary,
    `{:safe, binary}`, which the page places whole: the proof's answer and the
    component's update copy its reference, never the markup.
  * **A keyed render** (`Macaw.Live.Keyed`, the shape macaw's built-in table answers,
    macaw#434) is answered as the struct, its head, gaps and tail and each row's markup written out
    to a binary of its own, so the page places the rows by key (a one-row change sends
    that row) and copies each part's reference, never its markup.
  * **A `%Phoenix.LiveView.Rendered{}`** (a HEEx render) is written out whole, which
    proves it, and answered as it is, so LiveView's change tracking is kept: placed, it
    evaluates in the page only what that tracking finds changed (a keyed comprehension
    its changed entries, an entry whose variables equal those it last sent not at all),
    the same assigns' expressions, which rendered whole here.

`opts`: `rows: {live, viewer}` writes a keyed render's rows through the supervisor
`live`'s render memo (`Macaw.Live.Renders.written/3`), each row it holds taken as held.
`held: {kept, memo}` hands the render the rows its page holds as macaw_component's
`held` assign (spec/01 §3.4, macaw#465): `kept`, the rows the page kept beside the cell
(`Macaw.Live.Keyed.kept/2`) or `nil`, merged into, when `memo` is `{live, scope,
viewer}`, the rows the memo holds under that scope (`Macaw.Live.Renders.held/3`), read
here, in the proof's process. A render that isn't keyed ignores them.

---

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