Skuld.Coroutine.PageMachine (skuld_concurrency v0.35.0)

View Source

Synchronous page-machine for effectful state machines.

Wraps Skuld.Coroutine, returning tagged tuples instead of raw sum types. Use this when you want in-process testing without the overhead of a separate BEAM process. For cross-process use (e.g. LiveView), see Skuld.AsyncCoroutine.AsyncPageMachine.

Example

alias Skuld.Coroutine.PageMachine

# Start — returns raw Coroutine sum type
fiber = PageMachine.run(MyApp.CheckoutFlow.flow(cart))
{:yield, :shipping} = PageMachine.dispatch(fiber)

# Resume
fiber = PageMachine.run(fiber, {:ok, %{address: "123 Main"}})
{:yield, :payment} = PageMachine.dispatch(fiber)

# Complete
fiber = PageMachine.run(fiber, {:ok, %{card: "4242"}})
{:complete, {:ok, order}} = PageMachine.dispatch(fiber)

Return values (from dispatch/1)

  • {:yield, value} — computation yielded, waiting for input
  • {:complete, result} — computation finished successfully
  • {:error, error} — computation threw or errored
  • {:cancel, reason} — computation was cancelled

Summary

Functions

Cancel a running computation. Returns the raw Coroutine sum type.

Convert a raw Coroutine sum type to a tagged tuple for pattern matching.

Start a computation. Returns the raw Coroutine sum type.

Resume a yielded computation with a value. Returns the raw Coroutine sum type.

Functions

cancel(fiber, reason \\ :cancelled)

Cancel a running computation. Returns the raw Coroutine sum type.

dispatch(arg1)

Convert a raw Coroutine sum type to a tagged tuple for pattern matching.

Returns {:yield, value}, {:complete, result}, {:error, error}, or {:cancel, reason}.

run(computation)

Start a computation. Returns the raw Coroutine sum type.

Use dispatch/1 to convert to a tagged tuple when you're ready to pattern-match on the outcome.

run(fiber, value)

Resume a yielded computation with a value. Returns the raw Coroutine sum type.