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

The page's pulse (spec/03 §3): a change reaches the cells in ranked pulses, as the core's
page DAG carries it ([macaw spec/03 §6](https://github.com/dcoai/macaw/blob/main/spec/03-state.md#state-dag)).
Adapted from visualize's `Visualize.Board.Server` pulse timer: the marks waiting, when the
pulse is due, and its stages in rank order, over the core's values.

**What marks a pulse, and when it is due:**

| Mark | Due |
|---|---|
| a visitor's or the host's event that changed state | at once |
| a source manager's change to a reader's window, a sliding window's tick | `debounce:` ms after the first such mark, `0` at once |
| a source manager's answer to a reader waiting for it | at once |

A pulse takes every mark waiting when it runs, whatever made it due, so marks within a
debounce, from any number of sources, are one pulse.

**The stages, in rank order,** each node at most once:

1. **State** (rank 0): the changed names, from an event; `Macaw.State.affected/2` names
   the slots to ask and the cells whose bound settings changed.
2. **Requests:** each reader whose window or reads moved moves its subscription in its
   source's manager (`Macaw.Data.Manager`), and waits for its answer.
3. **Deliveries:** when a manager answers, or tells a reader its window changed, each
   reader's window is read from what the manager published and compared with the
   delivery its cell holds: an equal one marks nothing.
4. **Derived sources** run once per host, beside the page (`Macaw.Live.Derived`): their
   outputs are sources with managers of their own, read as stage 3 reads any source,
   and an unchanged answer is announced to no manager, so it marks no reader.
5. **Publish:** each node another process reads, on its edge (spec/04).
6. **Cells** (last): every cell with a changed setting or delivery, once, with all of
   them, when none of its readers still waits for its manager: a cell whose slots were
   asked renders with its settings and its data together, never its new settings over
   its old rows. A cell whose proved assigns equal its component's sends nothing.

**State changes only at events:** no stage after the first writes state, so a pulse can
never mark a state and no loop is possible.

Its value, in the page's `pulse`: the marks waiting, the monotonic ms the pulse is due
at, and its timer.

# `mark`

```elixir
@type mark() :: {:read, String.t()} | :slide | term()
```

A data mark: a reader whose window its manager changed (`{:read, name}`), or the page's
sliding readers to move (`:slide`).

# `t`

```elixir
@type t() :: %Macaw.Live.Pulse{
  due: integer() | nil,
  marks: %{required(mark()) =&gt; true},
  timer: reference() | nil,
  token: reference() | nil
}
```

# `mark`

```elixir
@spec mark(t(), mark(), integer(), non_neg_integer()) ::
  {t(), :now | :armed | {:arm, integer()}}
```

A pulse with `mark` waiting at the monotonic `ms`, `debounce` the page's: `{pulse, :now}`
when it is due at once, `{pulse, {:arm, ms}}` when the first mark makes it due after the
debounce, and `{pulse, :armed}` when a pulse is due already.

    iex> {pulse, {:arm, 250}} = Macaw.Live.Pulse.mark(%Macaw.Live.Pulse{}, {:read, "a"}, 0, 250)
    iex> {pulse, :armed} = Macaw.Live.Pulse.mark(pulse, {:read, "b"}, 100, 250)
    iex> Map.keys(pulse.marks) |> Enum.sort()
    [{:read, "a"}, {:read, "b"}]
    iex> elem(Macaw.Live.Pulse.mark(%Macaw.Live.Pulse{}, {:read, "a"}, 0, 0), 1)
    :now

# `take`

```elixir
@spec take(t()) :: {[mark()], t()}
```

The marks a pulse takes when it runs, and the pulse emptied: `{marks, pulse}`, every mark
waiting whatever made it due.

    iex> {pulse, _} = Macaw.Live.Pulse.mark(%Macaw.Live.Pulse{}, {:read, "a"}, 0, 250)
    iex> {marks, pulse} = Macaw.Live.Pulse.take(pulse)
    iex> {marks, pulse.due}
    {[{:read, "a"}], nil}

---

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