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

The owner–mirror protocol (spec/04 §1): how a node of a page's DAG held in another
process reaches the processes that read it. One protocol serves every crossing edge: a
`:shared` owner (spec/04 §4), the visit's and the page's scopes (spec/04 §5), and the page and its
persistent regions once macaw has them (spec/04 §3). They differ only in where the owner
lives and how it is found.

**An owner holds a node; a mirror in each reading process holds its last value.**

  * **The owner's side is this struct:** its key, its current values and a sequence,
    increased by one at each change it publishes. `publish/2` answers the publication,
    `{:macaw_live, :edge, key, seq, values}` with `values` only the names that changed,
    or `nil` when nothing did: an unchanged result is never published, so propagation
    stops at the owner.
  * **A mirror joins** with `join/3`, answered `{:ok, seq, values}`, every current value
    (`current/1`), and holds them in a `Macaw.Live.Edge.Mirror`, which applies the next
    publication it expects, ignores a repeat and finds a gap, which it answers by joining
    again and taking the answer whole.
  * **An owner that ends** is a `:DOWN` the mirror reads: it keeps its values and joins
    the restarted or next owner, taking its answer whole.
  * **Values are judged where they land:** a state value is put into the reader's
    `Macaw.State` with `Macaw.State.put/2` (`Macaw.Live.Edge.Mirror.land/3`), judged by
    the reader's own declaration; a value it refuses is dropped and reported.

**A restarted owner's sequence starts past every sequence its predecessor published:**
`new/2` starts it at the system's microseconds. A late publication of the old owner is
then a repeat, and the new owner's first is a gap the mirror rejoins at, so a mirror
never applies one owner's change over another's values. The protocol is modeled in
extla (`Macaw.Live.Specs.Edge`) and checked over every ordering it reaches.

The protocol's calls (`join/3`, and the owners' own) are messages with a deadline and
the owner monitored, so a call to an owner that ended or hangs answers an error value,
never an exit in the caller.

# `publication`

```elixir
@type publication() ::
  {:macaw_live, :edge, term(), integer(), %{required(term()) =&gt; term()}}
```

A publication, as it travels to every mirror.

# `t`

```elixir
@type t() :: %Macaw.Live.Edge{
  key: term(),
  seq: integer(),
  values: %{required(term()) =&gt; term()}
}
```

An owner's side of an edge: its key, its sequence and its current values.

# `current`

```elixir
@spec current(t()) :: {:ok, integer(), map()}
```

A joiner's answer: `{:ok, seq, values}`, every current value.

# `join`

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

Joins the edge `key` of the owner process `owner` for the process `pid`: `{:ok, seq,
values}`, every current value, or `{:error, reason}`: `{:down, reason}` for an owner that
ended, `:timeout` for one that didn't answer within 5,000 ms, and the owner's own refusal
of a key it doesn't hold. The owner monitors the joiner when it sends to it directly.

# `new`

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

An owner's side of an edge under `key`, holding `values`, its sequence started past any
its predecessor could have published: the system's microseconds, or `seq:` for an owner
that knows its predecessor's last sequence (a host persisting it, a test).

    iex> edge = Macaw.Live.Edge.new("wall", %{"pick" => nil})
    iex> {:ok, _seq, values} = Macaw.Live.Edge.current(edge)
    iex> values
    %{"pick" => nil}

# `publish`

```elixir
@spec publish(t(), map()) :: {t(), publication() | nil}
```

The owner's values after a change, and its publication: `{edge, publication}` with only
the names whose values changed, under the next sequence, or `{edge, nil}`, the edge as it
was, when none did.

    iex> edge = Macaw.Live.Edge.new("wall", %{"pick" => nil, "zoom" => 1})
    iex> {edge, {:macaw_live, :edge, "wall", seq, changed}} = Macaw.Live.Edge.publish(edge, %{"pick" => "a", "zoom" => 1})
    iex> {changed, seq == edge.seq}
    {%{"pick" => "a"}, true}
    iex> Macaw.Live.Edge.publish(edge, %{"pick" => "a"})
    {edge, nil}

---

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