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

The host's navigation behavior (spec/01 §9). **The library never navigates:** a
`navigate` action's step (`Macaw.Page.steps/2`, macaw spec/06 §5) goes to the behavior
named by the mount's `navigation:`, `{module, arg}`, after the event's state changes are
applied and their renders sent, so a pick that selects and then navigates shows the
selection on the page it leaves.

The default, `Macaw.Live.Navigation.Message`, sends the page
`{:macaw_live, :navigate, target, params}`, which the runtime's `:handle_info` hook
passes on to the host's own `handle_info/2`. A step a persistent region's cell takes is
sent to the page the region sits on, so the host reads every step in one place.

A behavior's faults leave the page where it is; they are reported in `Macaw.Live.info/1`
as the `navigation` of the surface whose cell took the step, the last step's faults
until one succeeds, `Macaw.Page.steps/2`'s faults among them.

**Live or a full load** (macaw spec/06 §5, macaw#130): a navigation is live when the
target page's plan, the union of its components' declarations
(`%Macaw.Page.Validated{}`'s `plan`), equals the page's, and a full load when they
differ, since what a page may observe, load and proxy is fixed when its document loads.
`follow/3` answers which, with the target's URL, so a host follows a step with one
clause:

    def handle_info({:macaw_live, :navigate, target, params}, socket) do
      case Macaw.Live.Navigation.follow(socket, target, params) do
        {:ok, %{live: true, href: href}} -> {:noreply, push_navigate(socket, to: href)}
        {:ok, %{live: false, href: href}} -> {:noreply, redirect(socket, to: href)}
        {:error, _faults} -> {:noreply, socket}
      end
    end

A persistent region survives a live navigation between pages that share it, its cells'
processes, values and the focus inside it kept (spec/04 §3, spec/05 §5).

# `navigate`

```elixir
@callback navigate(
  arg :: term(),
  Phoenix.LiveView.Socket.t(),
  target :: term(),
  params :: map()
) ::
  {:ok, Phoenix.LiveView.Socket.t()} | {:error, [Macaw.Base.Fault.t()]}
```

Performs one navigation step: the socket, or faults that leave the page where it is.

# `follow`

```elixir
@spec follow(Phoenix.LiveView.Socket.t(), term(), map()) ::
  {:ok, %{href: String.t(), live: boolean()}}
  | {:error, [Macaw.Base.Fault.t(), ...]}
```

How a host follows a step from the page `Macaw.Live.mount_page/3` mounted: `{:ok,
%{href: url, live: boolean}}`, `href` the target's URL at `params`
(`Macaw.Page.href/2`) and `live` whether the target's plan equals this page's, so a live
navigation can carry it; else a full load.

The page itself is always live. Another target's plan is its validated value's, as the
host validated it once with `Macaw.Live.validate_pages/1` and passed it to the mount's
`pages:`, so a navigation costs the params' check and a lookup, never a validation: its
cost is the same whatever the target page holds.

Its faults: a socket that mounts no page, `{[:page], :not_mounted}`; a target the page's
actions don't name, `{[:target], {:unknown_page, target}}`; params the target refuses,
`Macaw.Page.href/2`'s, at `[:params | path]`.

---

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