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:
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#
pub struct NoContextThe context of a machine that keeps none.
NoEffect#
pub union NoEffectThe effects of a machine that queues none. No transition makes one.
Nothing#
NothingImplementation Schema for NoContext#
impl Schema for NoContextImplementation Schema for NoEffect#
impl Schema for NoEffectSimpleChart#
pub type SimpleChart<S, E> = Chart<S, E, NoContext, NoEffect>The chart of a simple machine of states S and events E.
SimpleSnapshot#
pub type SimpleSnapshot<S, E> = Snapshot<S, E, NoContext, NoEffect>The snapshot of a simple machine of states S and events E.
SimpleMachine#
pub type SimpleMachine<E> = Machine<E, NoContext, NoEffect>A machine with states and events only. The implementing union is the states.
Simple#
pub struct SimpleImplementation Simple#
impl SimpleAssociated function chart#
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#
pub effect fn begin<S: SimpleMachine<E>, E>(now: u64) -> SimpleSnapshot<S, E>
! OutOfMemoryError | TransitionLoopError
? &mut AllocatorReturns 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#
pub effect fn send<S: SimpleMachine<E>, E>(snapshot: &mut SimpleSnapshot<S, E>, event: E) -> bool
! OutOfMemoryError | TransitionLoopError
? &mut AllocatorProcesses event and returns whether a state handled it.
Associated function advance#
pub effect fn advance<S: SimpleMachine<E>, E>(
snapshot: &mut SimpleSnapshot<S, E>,
now: u64,
) -> bool
! OutOfMemoryError | TransitionLoopError
? &mut AllocatorMoves the machine's clock to now and takes the delayed transitions that are due.
Returns whether one was taken.
Associated function play#
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 SystemClockPlays 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.