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

A surface's derived sources run once per host (spec/03 §3.1, dco-tek/macaw/macaw#108 "A
derived source is a managed source"): a process holding one `Macaw.Data.Derived.Engine`
for every page that mounts a surface with the same derived sources over the same inputs.

- **Keyed** (`key/4`) by the digest of the engine's definitions (its derived sources,
  the sources they may read, its functions, debounce and bounds) and the key of each
  input's source manager (`Macaw.Data.Manager.key/4`), so a viewer-dependent input
  makes the engine its viewer's. Started under `<live>.Owners` by the first page that
  finds none (`ensure/3`), registered in the supervisor's `Registry` with its table,
  and lingering `linger:` (5,000 ms) after the last page leaves, as a manager does.
- **Its inputs are managed:** it subscribes each source its engine reads, whole, to that
  source's manager as a reader of its own, and reads each from the manager's table
  (`Macaw.Data.Manager.Reader.read/3`), so an input read by pages and by the engine is
  one holding and one finish.
- **A writer only, its work in turns.** A manager's message that an input changed arms
  a read `debounce:` later, so changes within it are one pulse; the read, the engine's
  `push/4` and its due pulse (`run/2`) are one turn of its own under `<live>.Tasks`,
  given a copy of the engine and answering the engine, bounded by `timeout:`, a turn
  asked meanwhile run once after. A pulse due later is armed for when it is due.
- **Its outputs are sources:** each output's last good delivery and status is
  published in its table, and read by its output's manager through
  `Macaw.Live.Derived.Source`, an entry that names this process's key and the output's
  path, so its digest never moves. An output whose delivery or status moved is
  announced on its name's topic (spec/03 §7), once; an unchanged answer announces
  nothing, so it marks no reader.

A page holding a surface with derived sources joins it at mount (`join/2`) and leaves at
unmount, and is watched while it holds it.

# `key`

```elixir
@type key() :: {:derived, String.t(), [{String.t(), Macaw.Data.Manager.key()}]}
```

A derived process's key: `{:derived, digest, inputs}`, the digest of its engine's
definitions and each input's manager key by source name.

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `ensure`

```elixir
@spec ensure(atom(), key(), keyword()) :: {:ok, pid()} | {:error, term()}
```

The process of `key` under `live`, started when none runs: `{:ok, pid}`, the one started
first when two race, or `{:error, reason}`. `opts`: `engine:`, `inputs:` (by source
name, `{manager key, the manager's ensure options}`), `viewer:`, the first page's
`clock:`, `timeout:` and `debounce:`, and `linger:`.

# `info`

```elixir
@spec info(pid(), timeout()) :: {:ok, map()} | {:error, term()}
```

The process's present state: its key, engine (`Engine.runs/1`, `Engine.status/1` and
`Engine.faults/1` read it), inputs' managers, pages, turn running and whether a read or
a pulse is armed. `{:ok, info}`, or `{:error, reason}`.

# `join`

```elixir
@spec join(pid(), pid()) :: :ok
```

`page` joined: watched while it holds the surface. `:ok`, never waiting.

# `key`

```elixir
@spec key(Macaw.Data.Derived.Engine.t(), [{String.t(), Macaw.Data.Manager.key()}]) ::
  key()
```

The key of the process running `engine` over `inputs`, each source name it reads with
its manager's key.

# `leave`

```elixir
@spec leave(pid(), pid()) :: :ok
```

`page` left. `:ok`, never waiting.

# `lookup`

```elixir
@spec lookup(atom(), key()) :: {:ok, pid(), :ets.tid()} | :error
```

The process of `key` and its table: `{:ok, pid, table}`, or `:error`.

# `output`

```elixir
@spec output(atom(), key(), [String.t()]) ::
  {:ok, Macaw.Data.Delivery.t() | nil, term()} | :loading
```

The output at `path` as the process of `key` last published it: `{:ok, delivery,
status}`, its last good delivery (`nil` for none) and the derived source's status, or
`:loading` before it published one or with none running. Never calls the process.

---

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