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

The contained cell (spec/02): the process that holds one `:contained` component's value,
drives its transitions as inputs arrive, proves its render, and hands the page what to
draw. Copied from visualize's `Visualize.Board.Cell` (the host part of
`Visualize.Visual`) and adapted: generic, it knows a component only through
`Macaw.Component`'s dispatch functions and its `render/2`, and its coalescing has moved to
the sender (spec/02 §3). visualize's board, subscribers, views, tokens and click
resolution are not carried over (spec/08).

**The process** (spec/02 §1) is a `GenServer`, `restart: :temporary`, started by the page
under `<live>.Cells`. Its first act is its heap limit (spec/02 §6), its second a monitor
on the page, whose `:DOWN` stops it with `:normal`; it is not linked to the page. It does
its work after `init/1` returns, its first batch sent by the page as every later one is,
once the page monitors it: starting a cell never waits on the component, and a cell that
stops on its first batch is seen stopping with its reason, never `:noproc`.

**The order** (spec/02 §2) is the one `Macaw.Surface.render/3` builds a contained value
in: `init/2` once per process, then within each batch every delivery in slot name order,
every bound value in name order (the selection handed to the cell last, as `render/3`
hands it), then the size. An input equal to the one held is not applied: a delivery by
the table's identity (`Macaw.Data.Table.equal?/2`) and exactly (`===`) for the rest, as
`Macaw.Data.Derived.Engine` judges a delivery; a bound value or a size by `===`.

**The batch protocol** (spec/02 §3):

    page → cell   {:macaw_live, :batch, seq, batch}
    cell → page   {:macaw_live, :proved, instance, seq, result}

`result` is `{:value, assigns}` (the value changed: the seven assigns proved),
`{:same, assigns}` (only the render assigns changed: the five proved, without the value)
or `:unchanged`. A transition or render that raises answers nothing: the cell dies, and
the page reads its `:DOWN` (spec/02 §5).

**Ticks** (spec/02 §2.1): a component with `tick_interval/1` and `tick/2` answers each
result with its interval beside it (`{:value, assigns, interval}`, `{:same, assigns,
interval}`, `{:unchanged, interval}`), and a batch's `tick: ms` steps it after the batch's
other inputs. Such a value is also given the easing clock, `clock/2` at the batch's `ms`,
before the batch's inputs (macaw_component spec/02 §7.4). A hook's requests (`request:`)
are answered after the size and before the tick, their answers sent to the page as
`{:macaw_live, :answers, instance, seq, [{name, answer}]}` before the result.

**The proof** (spec/02 §4): after each batch that changed anything the cell renders
`module.render(kind, assigns)` with the seven assigns and writes it out to iodata, so the
page renders only assigns that rendered whole.

**Drawing segments** (spec/05 §9.1): when the value moved and its component exports
`segments/1`, the cell reads its drawing with `Macaw.Live.Stream.segments/2` and hands it
beside the seven assigns as `segments:`, its binaries crossing by reference; a drawing of
no allowed shape stops the cell with `{:segments, faults}`.

# `batch`

```elixir
@type batch() :: %{
  :deliveries =&gt; %{required(String.t()) =&gt; Macaw.Data.Delivery.t()},
  :bound =&gt; %{required(String.t() | :selection) =&gt; term()},
  :size =&gt; {number(), number()} | nil,
  :assigns =&gt; map(),
  optional(:tick) =&gt; integer() | nil,
  optional(:request) =&gt; [{String.t(), term()}],
  optional(:ms) =&gt; integer() | nil
}
```

The inputs one batch carries (spec/02 §3): the latest delivery per slot, the latest value
per bound name (`:selection` for the selection handed to the cell), the size, the render
assigns (`context`, `settings`, `declarations`, `drawn`), and `ms`, the page's monotonic
milliseconds when it sent the batch.

# `held`

```elixir
@type held() :: %{
  deliveries: %{required(String.t()) =&gt; Macaw.Data.Delivery.t()},
  bound: %{required(String.t() | :selection) =&gt; term()},
  size: {number(), number()} | nil
}
```

The inputs a cell holds, the last of each it applied.

# `result`

```elixir
@type result() ::
  {:value, map()}
  | {:same, map()}
  | :unchanged
  | {:value, map(), pos_integer() | nil}
  | {:same, map(), pos_integer() | nil}
  | {:unchanged, pos_integer() | nil}
```

What a cell answers a batch with (spec/02 §3), and for a value that ticks the same with
its tick interval beside it (§2.1).

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `limit`

```elixir
@spec limit([non_neg_integer() | nil], pos_integer(), pos_integer()) :: pos_integer()
```

The heap limit of a cell whose slots ask `budgets` rows (spec/02 §6), in words:
`min(max_heap, 1,000,000 + 4,096 × rows)`, `rows` the sum of the budgets, each clamped
to `ceiling` and a `nil` budget (every row) read at the ceiling. A pure cell's proof is
limited by the same derivation over its slots (spec/01 §3.3).

    iex> Macaw.Live.Cell.limit([2_500], 5_000, 25_000_000)
    11_240_000
    iex> Macaw.Live.Cell.limit([nil, nil], 5_000, 25_000_000)
    25_000_000

---

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