drift/js/runtime

Wrap a drift stepper with an event loop on JavaScript.

Types

Errors that can happen when using call or call_forever.

pub type CallError {
  RuntimeStopped
  CallTimedOut
}

Constructors

  • RuntimeStopped

    The runtime stopped while a call was active.

  • CallTimedOut

    The call took longer than what was provided as the timeout value.

Holds the event loop for running a stepper.

pub opaque type Runtime(input)

The result of a runtime terminating.

pub type TerminalResult(a, e) {
  Terminated(a)
  Failed(e)
  RuntimeError(String)
}

Constructors

  • Terminated(a)

    The runtime terminated successfully, with drift.stop.

  • Failed(e)

    The runtime ran into a failure, either through dirft.stop_with_error, or an error occurring during output handling.

  • RuntimeError(String)

    The runtime ran into an error. This is probably a bug in drift_js!

Values

pub fn call(
  runtime: Runtime(i),
  waiting timeout: Int,
  sending make_request: fn(drift.Effect(a)) -> i,
) -> promise.Promise(Result(a, CallError))

Similar to process.call on Gleam on Erlang.

pub fn call_forever(
  runtime: Runtime(i),
  make_request: fn(drift.Effect(a)) -> i,
) -> promise.Promise(Result(a, CallError))

Similar to process.call_forever on Gleam on Erlang.

pub fn now() -> Int

Returns a monotonic timestamp in milliseconds. The reference point (value 0) is not defined.

pub fn send(runtime: Runtime(i), input: i) -> Nil

Sends an input to be handled by the runtime.

pub fn start(
  state: s,
  create_io: fn(Runtime(i)) -> io,
  handle_input: fn(drift.Context(i, o), s, i) -> drift.Step(
    s,
    i,
    o,
    e,
  ),
  handle_output: fn(drift.EffectContext(io), o, fn(i) -> Nil) -> Result(
    drift.EffectContext(io),
    e,
  ),
) -> #(promise.Promise(TerminalResult(s, e)), Runtime(i))

Starts a new runtime with the given state and IO handlers.

Search Document