silk.machine.simple.

The short form of a machine that has only states and events.

When to use#

Implement SimpleMachine in place of Machine when a machine keeps no context and queues no effects: a toggle, a traffic light, the steps of a form. It names one type, the events, where Machine names three. Move to Machine when the machine needs data or effects.

Details#

SimpleMachine<E> is another name for Machine<E, NoContext, NoEffect>, so everything that takes a machine takes a simple one: snapshots, actors, definitions, the xstate config and paths. Simple has the calls that would otherwise repeat those two types, and Simple.chart starts the chart:

silk
import silk.machine.chart { Chart }
import silk.machine.simple { Simple, SimpleChart, SimpleMachine }

union Lamp {
  Off,
  On,
}

union LampEvent {
  Toggle,
}

impl SimpleMachine<LampEvent> for Lamp {
  static fn chart() -> SimpleChart<Lamp, LampEvent> {
    return Simple.chart<Lamp, LampEvent>()
      |> Chart.initial(Lamp.Off)
      |> Chart.on("Off", "Toggle", Lamp.On)
      |> Chart.on("On", "Toggle", Lamp.Off)
  }
}

pub fn main() -> i32 {
  return 0
}

Import as Simple with import silk.machine.simple { Simple }.

Public declarations: 6.

NoContext#

silk
pub struct NoContext

The context of a machine that keeps none.

NoEffect#

silk
pub union NoEffect

The effects of a machine that queues none. No transition makes one.

Nothing#

silk
Nothing

Implementation Schema for NoContext#

silk
impl Schema for NoContext

Implementation Schema for NoEffect#

silk
impl Schema for NoEffect

SimpleChart#

silk
pub type SimpleChart<S, E> = Chart<S, E, NoContext, NoEffect>

The chart of a simple machine of states S and events E.

SimpleSnapshot#

silk
pub type SimpleSnapshot<S, E> = Snapshot<S, E, NoContext, NoEffect>

The snapshot of a simple machine of states S and events E.

SimpleMachine#

silk
pub type SimpleMachine<E> = Machine<E, NoContext, NoEffect>

A machine with states and events only. The implementing union is the states.

Simple#

silk
pub struct Simple

Implementation Simple#

silk
impl Simple

Associated function chart#

silk
pub static fn chart<S, E>() -> Chart<S, E, NoContext, NoEffect>

Returns an empty chart of a simple machine, to build on with the Chart functions.

Associated function begin#

silk
pub effect fn begin<S: SimpleMachine<E>, E>(now: u64) -> SimpleSnapshot<S, E>
    ! OutOfMemoryError | TransitionLoopError
    ? &mut Allocator

Returns the first snapshot of the machine at the wall clock time now, in nanoseconds. A machine without delayed transitions can pass 0.

Associated function send#

silk
pub effect fn send<S: SimpleMachine<E>, E>(snapshot: &mut SimpleSnapshot<S, E>, event: E) -> bool
    ! OutOfMemoryError | TransitionLoopError
    ? &mut Allocator

Processes event and returns whether a state handled it.

Associated function advance#

silk
pub effect fn advance<S: SimpleMachine<E>, E>(
    snapshot: &mut SimpleSnapshot<S, E>,
    now: u64,
  ) -> bool
    ! OutOfMemoryError | TransitionLoopError
    ? &mut Allocator

Moves the machine's clock to now and takes the delayed transitions that are due. Returns whether one was taken.

Associated function play#

silk
pub effect fn play<'env, S: SimpleMachine<E>, E: 'static, G, ? Q>(
    snapshot: &mut SimpleSnapshot<S, E>,
    sender: &Sender<Signal<E>>,
    inbox: &Receiver<Signal<E>>,
    watch: effect fn<'env>(&SimpleSnapshot<S, E>) -> () ! G ? Q,
  ) -> ()
    ! G | OutOfMemoryError | TransitionLoopError | TaskIdExhaustedError
    ? Q | &mut Allocator | &mut Scheduler | &mut SystemClock

Plays the machine on the real clock: it takes the events that arrive on inbox and the delayed transitions when they are due, with no call to Simple.advance, until the machine reaches a final state or a Signal.Stop arrives.

Details#

watch gets the snapshot after each event and after each delayed transition, to draw or print the new state. sender must send to the channel of inbox: the machine uses it to wake itself up when a delay ends. Send Signal.Event { event } through a clone of it from any other fiber. This is Actors.drive for a machine without effects.

Category: State machines.

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.

View the source of this page