# `Macaw.Surface.Reader`
[🔗](https://github.com/dcoai/macaw/blob/v0.6.0/lib/macaw/surface/reader.ex#L1)

A source manager's reader (spec/06 §3.5, spec/07 §2.2, #443): how a host lets the paths
outside a live page, `Macaw.Surface.fetch/2` (and so `render/2`, `show/2` and a
container's surfaces), `Macaw.Surface.legal/2` and `Macaw.Page.check/3`, read a source's
data from the manager that holds it rather than from its adapter.

A host gives one as `manager: {module, arg}` in each env, `module` implementing this
behavior and `arg` the term each callback is given first (the host's managers, the
viewer's, a registry name: macaw never reads it). Without `manager:` (or with `nil`), each
path calls the host's sources as it always has.

  * `c:window/3` answers a reader's window of a source: the delivery the source itself
    would answer the request, before finishing. `fetch/2` asks it once per pooled request
    (one per source and request equal but for the budget, at the largest budget) and
    packs, finishes and reduces what it answers as it does a fetch's, so the cells draw
    exactly as they would from the source.
  * `c:distinct/4` answers a field's distinct present values as a `MapSet`, each spelled
    as `spelled/1` spells it (a category's by its spelling): the legal values of a
    `values_from` param or state. `legal/2` and `check/3` ask it once per `{source,
    field}` per call, and only for a field whose schema declares no `values`, which are
    answered from the schema with no read.

Each callback runs in the caller's process, never bounded by `timeout:`: a manager's
reader reads what its manager has published and never waits on an adapter, so bounding
its own wait is the reader's, answering `:loading` (a window not held in time) or an
error. An error is a value: a window's `{:error, reason}` draws its cells `down`, as a
fetch's error does, and a distinct's is the check's `{:unavailable, reason}`.

# `t`

```elixir
@type t() :: {module(), term()}
```

A host's reader as an env gives it: its module and the term each callback is given.

# `distinct`

```elixir
@callback distinct(
  arg :: term(),
  source :: String.t(),
  field :: String.t(),
  Macaw.Data.Source.Request.t()
) ::
  {:ok, MapSet.t()} | {:error, term()}
```

The distinct present values of `field` of the source named `source`, as `request` reads
it (its viewer and now): `{:ok, set}`, a `MapSet` of the values, each spelled as
`spelled/1` spells it, `nil` never among them; or `{:error, reason}`, a check that can't
be made, `{:unavailable, reason}`.

# `window`

```elixir
@callback window(
  arg :: term(),
  source :: String.t(),
  request :: Macaw.Data.Source.Request.t()
) ::
  {:ok, Macaw.Data.Delivery.t()} | :loading | {:error, term()}
```

A window of the source named `source`, as `request` asks it (its params, window, box,
`as_of`, order, budget, viewer and now): `{:ok, delivery}`, the delivery the source would
answer; `:loading`, a window the manager doesn't hold within its own bound, drawn
`loading`; or `{:error, reason}`, drawn `down`.

# `spelled`

```elixir
@spec spelled(term()) :: term()
```

A value as a legal check compares it, the spelling `c:distinct/4`'s values are in: an
atom naming a category by its string, any other value as it is at the edge.

    iex> Macaw.Surface.Reader.spelled(:api)
    "api"
    iex> Macaw.Surface.Reader.spelled(3)
    3

---

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