lightspeed/async

LiveView-style async assign/task compatibility model.

Types

Async runtime transition errors.

pub type AsyncError {
  UnknownTask(key: String)
  InvalidTransition(key: String, state: String, action: String)
}

Constructors

  • UnknownTask(key: String)
  • InvalidTransition(key: String, state: String, action: String)

State of one async assignment.

pub type AsyncState(value) {
  Idle
  PendingDisconnected
  Loading(ref: Int)
  Succeeded(value: value)
  Failed(reason: String)
  Cancelled(reason: String)
}

Constructors

  • Idle
  • PendingDisconnected
  • Loading(ref: Int)
  • Succeeded(value: value)
  • Failed(reason: String)
  • Cancelled(reason: String)

Runtime

opaque

Async runtime state.

pub opaque type Runtime(value)

Values

pub fn assign_async(
  runtime: Runtime(value),
  key: String,
) -> #(Runtime(value), AsyncState(value))

Start one async assignment.

  • when connected: enters Loading(ref)
  • when disconnected: enters PendingDisconnected
pub fn cancel(
  runtime: Runtime(value),
  key: String,
  reason: String,
) -> #(Runtime(value), Result(Nil, AsyncError))

Cancel one async assignment.

pub fn connect(runtime: Runtime(value)) -> Runtime(value)

Mark runtime connected and start pending async assignments.

pub fn connected(runtime: Runtime(value)) -> Bool

Connection status.

pub fn disconnect(runtime: Runtime(value)) -> Runtime(value)

Mark runtime disconnected.

pub fn error_to_string(error: AsyncError) -> String

Stable error string for assertions and logs.

pub fn fail(
  runtime: Runtime(value),
  key: String,
  reason: String,
) -> #(Runtime(value), Result(Nil, AsyncError))

Resolve one async assignment with a failure.

pub fn new(connected: Bool) -> Runtime(value)

Build a runtime.

pub fn state(
  runtime: Runtime(value),
  key: String,
) -> option.Option(AsyncState(value))

Lookup current state for one task key.

pub fn state_label(state: AsyncState(value)) -> String

Stable state label for logs and assertions.

pub fn succeed(
  runtime: Runtime(value),
  key: String,
  value: value,
) -> #(Runtime(value), Result(Nil, AsyncError))

Resolve one async assignment with a success value.

Search Document