WardrikWeb.Follow (wardrik_web v0.1.0-rc.1)

Copy Markdown

Keeps a LiveView up to date with a streamed operation.

When events arrive, it reloads the view. It reloads at most once every 250 ms.

def mount(_params, _session, socket),
  do: {:ok, socket |> load() |> WardrikWeb.Follow.start("events.list", %{})}

def handle_info({WardrikWeb.Follow, _message} = message, socket),
  do: {:noreply, WardrikWeb.Follow.handle(message, socket, &load/1)}
  • The stream starts only once the LiveView is connected.
  • The stream runs in a task linked to the LiveView. When one exits, the other exits too.
  • @live is true while the stream is open. When the stream fails or ends, @live is false and @stopped gives the reason.

Summary

Functions

Streams events for every agent the scope can see, using events.list.

Handles a message sent by the stream.

Starts streaming operation id with input, in the socket's scope.

Functions

agents(socket)

Streams events for every agent the scope can see, using events.list.

Agents created later are included the next time the view mounts.

handle(message, socket, reload, opts \\ [])

@spec handle(
  {module(), term()},
  Phoenix.LiveView.Socket.t(),
  (term() -> term()),
  keyword()
) ::
  Phoenix.LiveView.Socket.t()

Handles a message sent by the stream.

It calls reload with the socket after events arrive. If you pass :on_event, it calls it with each event.

start(socket, id, input)

Starts streaming operation id with input, in the socket's scope.