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

The owner of one surface instance's `:shared` state (spec/04 §4), across every viewer's
process: a wall display's selection, set by anyone watching it, seen by everyone. New:
visualize's shared board state lived in `Visualize.Board.Server`, not carried over.

  * **Its key** (`key/2`) is the surface's name, the env's `shared_key:` (the host's
    instance, such as a tenant, `nil` by default) and the digest of the validated
    surface, so two versions of a surface never share an owner.
  * **It is registered through `owners:`** and started under `<live>.Owners`,
    `:transient`, by the first page that mounts the instance (`ensure/3`); racing pages
    take the one started first. It holds a `Macaw.State` of the validated surface,
    starting at its initial values, and uses only its `:shared` names.
  * **Viewers join its edge** at mount (`Macaw.Live.Edge.join/3`) and put its values into
    their own state before their first render. It publishes through the notification
    behavior on its topic (`topic/2`), `{:macaw_live, :edge, key, seq, values}`, the
    names that changed.
  * **A viewer's event** is applied by the viewer to its other scopes and sent here
    (`apply/2`, a cast). The owner **applies events in arrival order** with `scopes:
    [:shared]` and publishes what changed; every viewer, the sender among them, takes the
    change from its mirror. No viewer applies a `:shared` write itself, so every viewer
    sees the owner's order.
  * **Faults** of an event (a value its declaration refuses) change nothing and are
    reported in `info/1`.
  * **It lives** while a viewer is joined and `linger:` (60,000 ms) after the last
    leaves, so a wall display that reloads finds its state.
  * **A crashed owner** is restarted by `Owners` at its initial values; its viewers'
    mirrors rejoin and take them whole.

# `key`

```elixir
@type key() :: {String.t(), term(), String.t()}
```

A `:shared` owner's key: the surface's name, the host's `shared_key:` and its digest.

# `apply`

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

A viewer's event sent to the owner, which applies it with `scopes: [:shared]` in arrival
order: `:ok`, never waiting. `legal` is the current legal values of the `values_from`
states it writes, by name, as the viewer read them (`Macaw.Surface.legal/2`), and the
owner judges those writes against them (`Macaw.State.apply/3`'s `legal:`): `%{}` for an
event that writes none, and a `values_from` write sent without its values is refused.

# `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 owner of `key` under the supervisor `live`, started when none runs: `{:ok, pid}`,
the one started first when two pages race, or `{:error, reason}`. `opts`: `validated:`
(the validated surface), `host:` (the host's values for its initials), `clock:`, and
`linger:` (60,000 ms).

# `info`

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

The owner's present state: its key, sequence, `:shared` values, the viewers joined and
the faults of the events it refused, in arrival order. `{:ok, info}`, or `{:error,
reason}`.

# `key`

```elixir
@spec key(Macaw.Surface.Validated.t(), term()) :: key()
```

The key of a surface instance's owner: its name, the host's `shared_key:` and the
digest of the validated surface.

# `leave`

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

A viewer leaves the owner: `:ok`, never waiting.

# `names`

```elixir
@spec names(Macaw.State.t()) :: [String.t()]
```

The names of a state's `:shared` sources, in name order: `[]` for a surface with none,
which has no owner.

# `topic`

```elixir
@spec topic(atom(), key()) :: String.t()
```

The topic an owner publishes on, `"macaw:<live>:shared:<digest>"` (spec/03 §7).

# `whereis`

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

The owner of `key` under the supervisor `live`, or `nil` when none runs.

---

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