# `Macaw.Page.Pulse`
[🔗](https://github.com/dcoai/macaw/blob/v0.6.0/lib/macaw/page/pulse.ex#L1)

A page's ranked pulses (spec/03 §6.3): its surfaces' states and its derived sources'
engine as one value, run over the page's graph (`Macaw.Page.Graph`). It holds no
process and reads no clock: `now` is the owner's monotonic milliseconds, as
`Macaw.Data.Derived.Engine`'s is.

A change marks its readers: an event applied to a surface (`apply/4`), values an owner
publishes (`put/4`) and a feed's deliveries (`deliver/4`). The first mark makes a pulse
due `debounce` milliseconds later (`due/1`), and `run/2` runs it: every marked node in
rank order, each at most once, an export or a mirror whose value didn't change marking
nothing, so propagation stops there. The derived sources run in the same pulse through
the page's one engine (`Macaw.Data.Derived.Engine.run/2`), after the states and exports
they read, since nothing a derived source answers is read by a state or an export.

Computing is separate from rendering: a pulse takes no visibility, so a hidden surface's
mirrors and the derived sources it feeds compute as any others do. What changed is the
host's to render (`run/2`'s answer, `Macaw.State.affected/2`).

# `changed`

```elixir
@type changed() :: %{
  states: %{required(String.t()) =&gt; [String.t()]},
  derived: [String.t()],
  dropped: [Macaw.Base.Fault.t()]
}
```

What a pulse changed: each surface's changed state names, in name order (for
`Macaw.State.affected/2`), the derived sources whose answers changed, and each value a
mirror's declaration refused, by path from the page.

# `t`

```elixir
@type t() :: %Macaw.Page.Pulse{
  changed: term(),
  debounce: term(),
  deliveries: term(),
  derived: term(),
  derived_marked?: term(),
  dropped: term(),
  due: term(),
  engine: term(),
  graph: term(),
  inputs: term(),
  last: term(),
  marked: term(),
  outputs: term(),
  pending: term(),
  runs: term(),
  states: term(),
  types: term(),
  values: term()
}
```

# `apply`

```elixir
@spec apply(t(), String.t(), Macaw.Data.Event.t(), integer()) ::
  {:ok, t(), [String.t()]} | {:error, [Macaw.Base.Fault.t(), ...]}
```

An event applied to one surface's state with `Macaw.State.apply/2` at `now` (spec/03
§6.3): `{:ok, pulse, changed}`, `changed` the names whose values changed, each marked,
or the event's faults with nothing changed. A changed `:page` state other surfaces
share is put into each of them (spec/03 §2.1), found through the graph's index
(`Macaw.Page.Graph.sharing/2`), judged by each one's declaration, each change marked and
each value refused in the next run's `dropped`. A surface the page doesn't hold is
`{[:surface], {:unknown_surface, surface}}`.

# `deliver`

```elixir
@spec deliver(t(), map(), map(), integer()) :: t()
```

A feed's latest deliveries and schemas, each by its name, shown to the page's derived
sources at `now` (spec/03 §6.3): every derived source reading a feed whose delivery is
new is marked, as `Macaw.Data.Derived.Engine.push/4` marks it. A page with no derived
sources answers the pulse as it is.

# `due`

```elixir
@spec due(t()) :: integer() | nil
```

The monotonic millisecond the next pulse is due, or `nil` (spec/03 §6.3).

# `last`

```elixir
@spec last(t()) :: [Macaw.Page.Graph.node_id()]
```

The nodes the last pulse ran, in the order it ran them: the states and exports by rank,
then the derived sources by theirs (spec/03 §6.3).

# `new`

```elixir
@spec new(Macaw.Page.Validated.t(), term(), keyword()) ::
  {:ok, t()} | {:error, [Macaw.Base.Fault.t(), ...]}
```

The pulse of a validated page (spec/03 §6.3), `states` each surface's `%Macaw.State{}`
by its name, built with `Macaw.State.new/2`: `{:ok, pulse}` or `{:error, faults}`. A
new pulse has run once at `now`, as a page runs at mount: each mirror holds its
export's value, and each derived source has run over what it reads. `opts`:
`debounce:`, milliseconds from the first mark to the pulse, `250`; `now:`, the owner's
monotonic milliseconds, `0`.

Its faults, every one at once: `states` that aren't a map, `{[:states], {:type,
:map}}`; a surface of the page with no state, `{[:states, surface], :required}`; a value
that isn't a state of that surface, `{[:states, surface], {:type, :state}}`; a surface
the page doesn't hold, `{[:states, surface], {:unknown_surface, surface}}`; opts that
aren't a keyword list, `{[], {:type, :keyword}}`, an unknown one `{[key], :unknown_key}`,
a `debounce:` that isn't a non-negative integer `{[:debounce], {:type,
:non_neg_integer}}` and a `now:` that isn't an integer `{[:now], {:type, :integer}}`.

# `provenance`

```elixir
@spec provenance(t(), Macaw.Page.Graph.node_id() | Macaw.Page.Graph.cell()) ::
  {:ok, [map()]} | :error
```

Why a node or a cell holds what it holds, with values (spec/03 §6.5): `{:ok, edges}` as
`Macaw.Page.Graph.provenance/2` answers them, each with `value`, the value at its `from`
now: a state's or an export's value, a derived source's or a feed's last delivery, `nil`
for none. `:error` for what the graph doesn't hold.

# `put`

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

Values an owner publishes put into one surface's state with `Macaw.State.put/2` at `now`
(spec/03 §6.3, macaw_live 04 §1): `{:ok, pulse, changed, dropped}`, each changed name
marked, as an event's are. A surface the page doesn't hold is `{:error, [{[:surface],
{:unknown_surface, surface}}]}`.

# `run`

```elixir
@spec run(t(), integer()) :: {t(), changed()}
```

The due pulse run at `now` (spec/03 §6.3): `{pulse, changed}`. Every marked node runs in
rank order, at most once: a state marks the exports naming it; an export reads its
state and, when the value changed, marks the mirrors and gives the derived sources
reading it; a mirror takes its export's value with `Macaw.State.put/2` and, when it
changed, marks its own readers. An unchanged value marks nothing. Then the page's engine
runs its marked derived sources in its own rank order. A pulse not yet due answers the
pulse as it is, and nothing changed.

# `runs`

```elixir
@spec runs(t()) :: %{required(Macaw.Page.Graph.node_id()) =&gt; pos_integer()}
```

How many times each node has run, by node, a node never run left out.

# `sources`

```elixir
@spec sources(t(), map()) :: map()
```

The host's sources with each of the page's derived sources' current answer beside them,
as a surface renders and fetches them (spec/03 §6.1).

# `states`

```elixir
@spec states(t()) :: %{required(String.t()) =&gt; Macaw.State.t()}
```

Each surface's state by its name, as the pulses left it.

---

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