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

Where each scope's values live (spec/04 §5), and the owner's side of a scope held for
several surfaces' states. macaw gives the pure functions (macaw spec/03 §2.1); this
module decides which process holds what, calling them and never reimplementing them.

| Scope | Held by | Across processes |
|---|---|---|
| `:local` | the browser; never on the server | — |
| `:surface` | the process that mounts the surface: the page, or a region | — |
| `:page` | the page | to regions, by edge, keyed `{:page, transport_pid}` |
| `:region` | the region (`Macaw.Live.Region`), or the page for a region it holds | to the page through the region's exports, by edge |
| `:session` | the visit's `Macaw.Live.Visit`, one per transport | to the page and regions, by edge |
| `:viewer` | the page, restored at mount and written after each change through the host's persistence | — |
| `:shared` | its `Macaw.Live.Shared` | to every viewer, by edge |

**A process applies an event to the scopes it holds** (`Macaw.State.apply/3` with
`scopes:`, `held/1`) and forwards a write of another's to its owner: `:shared` to its
owner, `:session` to the visit, `:page` from a region to the page (`forward/4`). The
owner's publication returns the change to every reader, the sender among them.

**An owner of a scope for several surfaces** (the visit's `:session`, the page's `:page`
for its regions) is this struct: an `Macaw.Live.Edge` of the scope's values by name, and
each reader surface's `Macaw.State`, registered when it joins (`register/3`). Two
surfaces declaring one name at one scope in one instance hold one value (macaw spec/03
§2.1): a write applied on one (`apply/3`, with `scopes: [scope]`) is put into every
other with `Macaw.State.put/2`, each judging it by its own declaration, and published
once.

**The page's `:page` owner finds those others in the page DAG** (`Macaw.Page.Graph`,
macaw spec/03 §6.2), whose `sharing/2` indexes each `:page` identity to the surfaces
declaring it (`sharers/3`): a write reaches its sharers at a cost independent of the
surfaces registered. A surface the graph doesn't hold (one mounted by
`Macaw.Live.mount/3` outside a page) is held aside, `unindexed`, and put every write;
an owner without a graph (a page of `mount/3`'s surfaces, the visit's `:session`) or a
write from a surface the graph doesn't hold puts it into every registered state.

# `t`

```elixir
@type t() :: %Macaw.Live.Scopes{
  edge: Macaw.Live.Edge.t(),
  graph: Macaw.Page.Graph.t() | nil,
  scope: atom(),
  states: %{required(String.t()) =&gt; Macaw.State.t()},
  unindexed: MapSet.t(String.t())
}
```

An owner of one scope for several surfaces: the scope, its edge (the values by name and
the sequence), the page DAG its `:page` writes are fanned out by (`nil` without one),
each registered surface's state, by surface name, and the registered surfaces the graph
doesn't hold.

# `adopt`

```elixir
@spec adopt(t(), Macaw.State.t()) ::
  {t(), Macaw.State.t(), Macaw.Live.Edge.publication() | nil}
```

A state the owner's process holds itself joining the owner's values, never kept by the
owner (the page's own surfaces, whose states the page holds): `{owner, state,
publication}`. The state is put the owner's values of the names it declares at the
scope; a name it declares that the owner holds no value of yet joins the owner's values
at the state's own, published to the readers already joined (`nil` when none is new).

# `apply`

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

A reader's event, forwarded to the owner, applied to the scope's states of the surface
it came from (`Macaw.State.apply/3` with `scopes: [scope]` and `legal:`, the legal values
of the `values_from` states it writes, `%{}` by default): `{:ok, owner, changed,
publication}`, `changed` the values that changed by name, put into every other
registered state and published (`nil` when nothing changed); or `{:error, faults}`, the
event's faults with nothing changed, `{[:surface], {:not_joined, surface}}` for a
surface never registered.

# `drop`

```elixir
@spec drop(t(), String.t()) :: t()
```

A surface's state no longer held by the owner.

# `forward`

```elixir
@spec forward(pid(), term(), String.t(), Macaw.Data.Event.t(), map()) :: :ok
```

A reader's event sent to the owner of a scope it doesn't hold, which applies it with
`scopes: [scope]` and publishes what changed: `:ok`, never waiting. A region forwards a
`:page` write to the page with the key `{:page, transport_pid}`; the page forwards a
`:session` write to the visit (`Macaw.Live.Visit.apply/4`). `legal` is the current legal
values of the `values_from` states the event writes, by name, as the sender read them
(`Macaw.Surface.legal/2`), which the owner judges those writes against (`apply/4`);
`%{}` by default, and a `values_from` write sent without its values is refused.

# `held`

```elixir
@spec held(:page | :region) :: [atom(), ...]
```

The scopes a process applies an event to itself (spec/04 §5): a page `:surface`,
`:page`, `:region` (a region in the page's process) and `:viewer`; a persistent region
`:surface`, `:region` and `:viewer`, its `:page` writes the page's. `:session` is the
visit's and `:shared` its owner's, so both forward their writes.

    iex> Macaw.Live.Scopes.held(:page)
    [:surface, :page, :region, :viewer]
    iex> Macaw.Live.Scopes.held(:region)
    [:surface, :region, :viewer]

# `held`

```elixir
@spec held(Macaw.State.t(), atom()) :: map()
```

What `state` holds at `scope`, by name: `Macaw.State.values/2`'s, and at `:session` the
visitor's overlay of each source its actions change too (macaw spec/13 §3.3), under its
name `{:overlay, source}`, a tuple no state's name can be. The visit holds them as it
holds the `:session` values (spec/04 §5): its edge carries them, and a page lands them.

    iex> surface =
    ...>   Macaw.Surface.build!("s",
    ...>     state: %{"menu" => %{type: :text, scope: :session}},
    ...>     cells: [{"macaw.reset", name: "again", kind: :control}]
    ...>   )
    iex> registry = Macaw.Registry.new!([Macaw.Builtin])
    iex> {:ok, validated} = Macaw.Surface.validate(surface, registry, %{})
    iex> {:ok, state} = Macaw.State.new(validated)
    iex> held = Macaw.Live.Scopes.held(state, :session)
    iex> {Map.fetch(held, "menu"), Enum.filter(Map.keys(held), &is_tuple/1)}
    {{:ok, nil}, []}

# `indexed?`

```elixir
@spec indexed?(Macaw.Page.Graph.t() | nil, String.t(), String.t()) :: boolean()
```

Whether the page DAG `graph` holds `surface`'s state of `name` (`nil` holds none).

# `join`

```elixir
@spec join(pid(), pid(), String.t(), Macaw.State.t(), pid()) ::
  {:ok, integer(), map()} | {:error, term()}
```

A reader in another process of the page's transport (a persistent region) joins the
page's `:page` edge for its surface's state: `{:ok, seq, values}`, the page's `:page`
values by name, or `{:error, reason}` as `Macaw.Live.Edge.join/3` answers. The page
registers the state, so a `:page` write forwarded from it is judged by its declaration,
monitors `pid` and sends it each publication directly.

# `land`

```elixir
@spec land(Macaw.State.t(), map()) ::
  {:ok, Macaw.State.t(), list(), [Macaw.Base.Fault.t()]}
```

`values` put into `state` (`Macaw.State.put/2`), each judged by its declaration, and an
overlay among them (`{:overlay, source}`, `held/2`) taken as its holder published it,
for a source the state changes: `{:ok, state, changed, dropped}`, `changed` the moved
overlays first and then the changed names, as `Macaw.State.apply/2` answers them. An
overlay of a source the state doesn't change is left out, as an undeclared name is.

# `leave`

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

A reader leaves an owner's edge: `:ok`, never waiting.

# `new`

```elixir
@spec new(atom(), term(), keyword()) :: t()
```

An owner of `scope` under the edge `key`, holding no surface yet. `opts` are
`Macaw.Live.Edge.new/3`'s (`seq:`) and `graph:`, the page DAG a `:page` owner fans its
writes out by (`nil`, the default, for none).

# `page`

```elixir
@spec page(atom(), pid()) :: pid() | nil
```

The page of a transport under the supervisor `live`: its pid, registered in the
supervisor's `Registry` as `{:page, transport_pid}`, or `nil`. A persistent region finds
its page this way and by nothing else (spec/04 §3).

# `persist`

```elixir
@spec persist(Macaw.State.t(), [atom()], module(), term()) :: term()
```

The carrier with the state's values of the given scopes written through a host's
persistence (`Macaw.State.persist/3`): every other scope's values left out, as
`Macaw.State.follow/3` returns them to their initial values, which persistence never
writes.

# `put`

```elixir
@spec put(t(), map(), String.t() | nil) :: {t(), Macaw.Live.Edge.publication() | nil}
```

Values of the scope changed at the owner, written on the surface `from` (a registered
reader's, or one the owner's process holds; `nil` for the owner's own): put into the
other registered states holding them, each judging them by its declaration, and
published: `{owner, publication}`, `nil` when none changed the owner's values.

A `:page` owner with a graph finds those states by its index (`sharers/3`), plus the
registered surfaces it doesn't hold, so the write costs the surfaces sharing it, never
a walk of every registered state; otherwise every registered state but `from`'s is put
the values it declares.

# `register`

```elixir
@spec register(t(), String.t(), Macaw.State.t()) ::
  {t(), Macaw.State.t(), Macaw.Live.Edge.publication() | nil}
```

A surface's state registered with the owner (a reader joining): `{owner, state,
publication}`, the state `adopt/2` answers, kept as the surface's. A surface registered
again replaces its state.

# `restore`

```elixir
@spec restore(Macaw.State.t(), [atom()], module(), term()) ::
  {Macaw.State.t(), [Macaw.Base.Fault.t()]}
```

The given scopes' values of a state restored from a host's persistence (spec/04 §5,
`Macaw.State.restore/3`): `{state, dropped}`, the state with the values the carrier
holds for those scopes' names, every other scope's values as they were, and the faults
of the values the carrier holds that the declaration refuses. A carrier is one scope's
(the visit's for `:session`, the viewer's for `:viewer`) or the page's, so a scope's
values are never taken from another's carrier. A `:session` restore takes the
visitor's overlays the carrier holds too, each row judged again (macaw spec/13 §3.5):
the changes a host's policy holds `:session`, which a URL never carries.

# `scoped`

```elixir
@spec scoped(Macaw.State.t(), atom(), map()) :: map()
```

The values of `values` that `state` declares at `scope`, so a reader puts only its own
names, never another surface's as unknown: at `:session`, its overlays among them
(`held/2`).

# `sharers`

```elixir
@spec sharers(Macaw.Page.Graph.t() | nil, String.t() | nil, [String.t()]) ::
  {:ok, %{required(String.t()) =&gt; [String.t(), ...]}} | :error
```

The other surfaces sharing each of `names` with `surface` at `:page`, read from the page
DAG's index (`Macaw.Page.Graph.sharing/2`, macaw spec/03 §2.1, §6.2): `{:ok, %{other =>
names}}`, each other surface to the names it shares, at a cost independent of the
page's surfaces; `:error` when there is no graph or it doesn't hold the surface's state
of every name (a surface mounted outside the page), so the caller finds them otherwise.

    iex> Macaw.Live.Scopes.sharers(nil, "a", ["zoom"])
    :error

---

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