Skuld.AsyncCoroutine.AsyncPageMachine (skuld_concurrency v0.35.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.

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.

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.