Skuld.AsyncCoroutine.AsyncPageMachine (skuld_concurrency v0.38.0)

View Source

Generates handle_info/2 clauses that dispatch AsyncCoroutine messages into callback functions, eliminating LiveView boilerplate.

No Phoenix dependency — just code generation. Use in any LiveView module that bridges an effectful page flow via AsyncCoroutine.

Usage

use Skuld.AsyncCoroutine.AsyncPageMachine,
  tag: :checkout,
  on_yield: &handle_yield/2,
  on_complete: &handle_complete/2

Options

  • :tag (required) — the async coroutine tag atom
  • :on_yield (required) — (value, socket) -> term() Called when the computation yields via ExternalSuspend
  • :on_complete(result, socket) -> term() Called when the computation completes
  • :on_error(error, socket) -> term() Called on {:error, reason} results
  • :on_cancel(reason, socket) -> term() Called when the computation is cancelled
  • :on_throw(error, socket) -> term() Called when the computation throws

If on_error is not given but on_complete is, {:error, reason} falls through to on_complete.

Summary

Functions

Cancel a running page machine. Delegates to AsyncCoroutine.cancel/1.

Generate a handle_event/3 clause that pipes a Phoenix event into the AsyncPageMachine as a Yield resume value. Multiple def_pipe_event calls produce multiple handle_event/3 clauses — one per event name.

Generate a handle_event/3 clause with params pattern matching and a value-transformation block.

Start a page machine in a separate process. Delegates to AsyncCoroutine.run/3.

Resume a yielded page machine with a value. Delegates to AsyncCoroutine.run/3.

Functions

cancel(runner)

Cancel a running page machine. Delegates to AsyncCoroutine.cancel/1.

def_pipe_event(event, assign_key)

(macro)

Generate a handle_event/3 clause that pipes a Phoenix event into the AsyncPageMachine as a Yield resume value. Multiple def_pipe_event calls produce multiple handle_event/3 clauses — one per event name.

Auto-imported when using use AsyncPageMachine.

Without pattern matching

def_pipe_event "submit_payment", :runner

Generates:

def handle_event("submit_payment", params, socket) do
  AsyncPageMachine.run(socket.assigns[:runner], {:ok, params})
  {:noreply, socket}
end

With pattern matching and transformation

def_pipe_event "submit_shipping", :runner, %{"address" => addr} do
  {:ok, %{address: addr}}
end

Generates:

def handle_event("submit_shipping", %{"address" => addr}, socket) do
  AsyncPageMachine.run(socket.assigns[:runner], {:ok, %{address: addr}})
  {:noreply, socket}
end

def_pipe_event(event, assign_key, pattern, list)

(macro)

Generate a handle_event/3 clause with params pattern matching and a value-transformation block.

run(computation, tag)

Start a page machine in a separate process. Delegates to AsyncCoroutine.run/3.

The computation will have Throw.with_handler/1 and Yield.with_handler/1 added automatically. Add other handlers before calling run.

Returns {:ok, runner} where the runner can be used with run/3 to resume the flow with user input.

run(computation, tag, opts)

Resume a yielded page machine with a value. Delegates to AsyncCoroutine.run/3.