libero/remote_data

Typed states for async data loading. Inspired by Elm’s RemoteData package.

Use instead of Bool flags + Option fields for data that loads asynchronously. The view pattern matches directly on the state - impossible to show stale data while loading or forget to handle errors.

to_remote is the bridge from a libero RPC response (a Dynamic value shipped from the server) to a RemoteData value the page stores in its model. It collapses three wire outcomes - domain success, domain failure, framework failure - into the two post-response states (Success or Failure). The NotAsked and Loading states are page-lifecycle concerns the page sets itself in init and update.

Types

pub type RemoteData(value, error) {
  NotAsked
  Loading
  Failure(error)
  Success(value)
}

Constructors

  • NotAsked
  • Loading
  • Failure(error)
  • Success(value)

Error type for formatted RPC failures. Distinguishes domain errors (formatted by the caller) from framework errors (formatted by libero).

pub type RpcFailure {
  DomainFailure(message: String)
  FrameworkFailure(message: String)
}

Constructors

  • DomainFailure(message: String)
  • FrameworkFailure(message: String)

Values

pub fn is_loading(data: RemoteData(a, e)) -> Bool

Check if the data is currently loading.

pub fn is_success(data: RemoteData(a, e)) -> Bool

Check if the data is loaded successfully.

pub fn map(
  data data: RemoteData(a, e),
  transform transform: fn(a) -> b,
) -> RemoteData(b, e)

Apply a function to the success value.

pub fn map_error(
  data data: RemoteData(a, e1),
  transform transform: fn(e1) -> e2,
) -> RemoteData(a, e2)

Apply a function to the error value.

pub fn to_option(data: RemoteData(a, e)) -> option.Option(a)

Convert to Option - Some for Success, None for everything else.

pub fn to_remote(
  raw raw: dynamic.Dynamic,
  format_domain format_domain: fn(domain) -> String,
) -> RemoteData(payload, RpcFailure)

Convert a libero RPC response (Dynamic) into a RemoteData value.

The server-side dispatch ships the full MsgFromServer envelope so the wire response is Result(MsgFromServer.Variant(Result(payload, domain_err)), RpcError(app_err)). This helper peels the MsgFromServer wrapper and collapses the result into the two post-response states.

The format_domain callback formats domain errors (which the page owns and pattern-matches on its specific error type). Framework errors (internal, unknown function, malformed request, server AppError) are formatted by libero’s default formatter.

pub fn to_result(
  raw raw: dynamic.Dynamic,
  format_domain format_domain: fn(domain) -> String,
) -> Result(payload, RpcFailure)

Adapter for action responses that the page wants as a flat Result rather than RemoteData. Useful when the response only feeds a flash message or triggers a redirect - the page never needs to render NotAsked/Loading states for the action result itself.

pub fn unwrap(
  data data: RemoteData(a, e),
  default default: a,
) -> a

Extract the success value, or return a default.

Search Document