Macaw.Live.Visit (macaw_live v0.1.0)

Copy Markdown View Source

The holder of a visit's :session state across live navigation (spec/04 §5): one per transport, the LiveView socket every page of a browser tab's visit shares. New.

  • It is registered by its transport's pid in the supervisor's Registry, as {:visit, transport_pid}, and started under <live>.Owners by the first page of the transport that mounts a surface declaring :session state (ensure/3); racing pages take the one started first. It ends with the transport, which it monitors.
  • Its instance (Macaw.State.new/2's instances: %{session: …}) is a token it makes at its start, so no two visits' values meet under one identity.
  • Pages join its edge with their surfaces' states (join/4), each registered (Macaw.Live.Scopes.register/3) and put the visit's values before its first render. A live navigation ends the old page and joins the new one to the same visit, so a :session value written on one page is read on the next.
  • A page's event is applied by the page to the scopes it holds and sent here (apply/3, a cast); the visit applies it with scopes: [:session] in arrival order, puts the change into every other surface's state it holds, and publishes it to every joined page directly, {:macaw_live, :edge, {:session, transport_pid}, seq, values}. No page applies a :session write itself.
  • Restored at its start and written after each change through the host's persistence (persistence:, [module: module, carrier: carrier]): a surface's :session values are restored from the carrier when the surface first joins (Macaw.Live.Scopes.restore/4), and every change writes that surface's :session values into it (Macaw.Live.Scopes.persist/4). The carrier store/3 answers is the one the next write takes, and info/1 gives it.
  • A visitor's changes to a source's rows are held here (macaw spec/13 §3, #94): each surface's overlay of each source its add and remove actions change, at :session scope under {:overlay, source} (Macaw.Live.Scopes.held/2). An add, a remove and a reset: :all are applied here as a :session write is, and the overlay published to every joined page, which applies it to its reads; so a visit's changes survive its live navigations and end with it. An overlay its host's policy holds held: :session is written and restored through the host's persistence too; one held :visit, the default, only lives here.
  • Faults of an event (a value its declaration refuses, a row its policy refuses) change nothing and are reported in info/1.
  • A crashed visit is restarted by Owners and restores from the carrier it was started with; its pages' mirrors rejoin and take its values whole.

Summary

Types

A visit's edge key: {:session, transport_pid}.

Functions

A page's event sent to the visit, which applies it with scopes: [:session] to the surface's state in arrival order: :ok, never waiting. legal is the legal values the page read for it, as Macaw.Live.Scopes.forward/5 takes them.

Returns a specification to start this module under a supervisor.

The visit of transport under the supervisor live, started when none runs: {:ok, pid}, the one started first when two pages race, or {:error, reason}. opts: persistence:, nil or [module: module, carrier: carrier].

The visit's present state: its key, instance, sequence, :session values by name (each overlay it holds among them, under {:overlay, source}), the pages joined, the surfaces registered, the faults of the events it refused in arrival order and its persistence's carrier. {:ok, info}, or {:error, reason}.

A page pid joins the visit for its surface's state: {:ok, %{id: id, seq: seq, values: values}}, the visit's instance and every current :session value by name, or {:error, reason} ({:down, reason}, :timeout, as Macaw.Live.Edge.join/3 answers). The visit registers the state, restoring its :session values from the carrier the first time the surface joins, and monitors the page.

The edge key of a transport's visit.

A page leaves the visit: :ok, never waiting.

The visit of transport under the supervisor live, or nil when none runs.

Types

key()

@type key() :: {:session, pid()}

A visit's edge key: {:session, transport_pid}.

Functions

apply(visit, surface, event, legal \\ %{})

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

A page's event sent to the visit, which applies it with scopes: [:session] to the surface's state in arrival order: :ok, never waiting. legal is the legal values the page read for it, as Macaw.Live.Scopes.forward/5 takes them.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

ensure(live, transport, opts \\ [])

@spec ensure(atom(), pid(), keyword()) :: {:ok, pid()} | {:error, term()}

The visit of transport under the supervisor live, started when none runs: {:ok, pid}, the one started first when two pages race, or {:error, reason}. opts: persistence:, nil or [module: module, carrier: carrier].

info(visit)

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

The visit's present state: its key, instance, sequence, :session values by name (each overlay it holds among them, under {:overlay, source}), the pages joined, the surfaces registered, the faults of the events it refused in arrival order and its persistence's carrier. {:ok, info}, or {:error, reason}.

join(visit, surface, state, pid)

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

A page pid joins the visit for its surface's state: {:ok, %{id: id, seq: seq, values: values}}, the visit's instance and every current :session value by name, or {:error, reason} ({:down, reason}, :timeout, as Macaw.Live.Edge.join/3 answers). The visit registers the state, restoring its :session values from the carrier the first time the surface joins, and monitors the page.

key(transport)

@spec key(pid()) :: key()

The edge key of a transport's visit.

leave(visit, pid)

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

A page leaves the visit: :ok, never waiting.

whereis(live, transport)

@spec whereis(atom(), pid()) :: pid() | nil

The visit of transport under the supervisor live, or nil when none runs.