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

The server's side of the client (spec/05 §4, §5, §7): the announcements and focus moves
the runtime pushes to the live module, and the runtime's own client events it reads.
Adapted from visualize's `Visualize.Hooks.Announcer` (`attrs/1`): the announcer's queue
is macaw_component's client module's, and a request is built here as the client builds
it, by `Macaw.Component.Announcement.new/2`.

`Macaw.Live.announce/3` and `Macaw.Live.focus/2` are this module's, as the host calls
them.

# `announce`

```elixir
@spec announce(Phoenix.LiveView.Socket.t(), String.t(), keyword() | map()) ::
  {:ok, Phoenix.LiveView.Socket.t()} | {:error, [Macaw.Base.Fault.t(), ...]}
```

An announcement requested by the server (spec/05 §4): built by
`Macaw.Component.Announcement.new/2` against the declarations of the cell `instance`
names, and pushed as `"mw:announce"`, `%{instance, name, text, politeness}`, which the
live module dispatches on the instance's element, so the client checks the declaration
again. The instance `"mw-live"` is the runtime's own element, declaring `page` and
`error`, for a host's error summary.

`{:ok, socket}`, or `{:error, faults}` with nothing pushed: `Announcement.new/2`'s
(`{[:name], {:undeclared, name}}`, `{[:text], :empty}`, `{[:politeness], {:politeness,
value}}`, `{[key], :unknown_key}`), `{[:instance], {:unknown, instance}}` for an instance
the page doesn't hold, `{[:instance], {:type, :string}}`, `{[:attrs], {:type,
:keyword}}` for attributes that are neither a keyword list nor a map, and `{[:socket],
{:type, :socket}}` for anything but a socket the runtime mounted.

# `focus`

```elixir
@spec focus(Phoenix.LiveView.Socket.t(), String.t()) ::
  {:ok, Phoenix.LiveView.Socket.t()} | {:error, [Macaw.Base.Fault.t(), ...]}
```

Moves focus to the element of id `id` (spec/05 §5): pushes `"mw:focus"`, `%{id}`, and
the live module focuses that element, a host's error summary or first invalid field; an
id with no element focuses nothing. `{:ok, socket}`, or `{:error, faults}`:
`{[:id], {:type, :string}}` for an id that is no non-empty string, and `{[:socket],
{:type, :socket}}` for anything but a socket the runtime mounted.

# `live_id`

```elixir
@spec live_id() :: String.t()
```

The id of the live module's own announcing element, `"mw-live"` (spec/05 §1).

# `request`

```elixir
@spec request(Phoenix.LiveView.Socket.t(), term()) ::
  {:ok, {String.t(), String.t(), term()}} | :error
```

The request `"mw-request"` carries (spec/05 §7), read as untrusted input: `{:ok,
{instance, name, payload}}` for an instance the page holds whose component declares the
name in `requests`, with a payload of at most 8 KiB JSON-encoded, the event wire's bound;
else `:error`, and the request is dropped. It never raises.

The runtime's own name, `"mw:segments"`, is the stream's ask for segments again by key
(spec/05 §9.4), never a component's: `{:ok, {instance, "mw:segments", %{token: token,
keys: keys}}}` for an instance whose cell draws segments and a payload `{"token":
string, "keys": [string, …]}` within the same bound; else `:error`.

# `shortcut`

```elixir
@spec shortcut(term()) :: {:ok, {String.t(), String.t() | nil}} | :error
```

The viewer's shortcut change `"mw-shortcut"` carries (spec/05 §6, §7), read as untrusted
input: `{:ok, {id, key}}` for an id that is a string and a key that is a string or
absent (`nil`, off); else `:error`. Whether the registry holds the id and takes the key
is `Macaw.Live.Shortcuts.change/4`'s to judge. It never raises.

# `sizes`

```elixir
@spec sizes(Phoenix.LiveView.Socket.t(), term()) :: %{
  font: number() | nil,
  sizes: [{String.t(), {number(), number()}, number() | nil}]
}
```

The sizes `"mw-size"` carries (spec/05 §7), read as untrusted input against the page:
`%{font: px | nil, sizes: [{instance, {width, height}, ratio | nil}]}`, in the order
sent, each instance's last. Params of another shape read as `%{font: nil, sizes: []}`;
an entry of another shape, an instance the page doesn't hold, and a width or height that
isn't a number from 1 to 100,000 are dropped; a font outside 1 to 1,000 pixels reads as
`nil`; a ratio, the optional fourth element, that isn't a number above 0 and at most 16
is dropped and its entry kept. It never raises.

---

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