silk.machine.child.
Child machines: a machine that a parent starts, sends events to and stops.
When to use#
Put a Child in the parent's context for each machine the parent owns. Start it in the
parent's enter, stop it in exit to tie it to a state as xstate's invoke does, or start
and stop it in handle to tie it to the parent as spawn does.
Details#
A child is a Snapshot inside the parent's context, so the parent's snapshot holds its
children and they are saved and restored with it. A child takes its steps inside the parent's
step, in the order the parent calls it, so the whole tree stays deterministic.
A child queues effects of its own type. Child.drain moves them into the parent's queue,
wrapped in a variant of the parent's effect type, and the parent's performer hands them to the
child's performer. Replies.forward wraps the events that performer sends back, so they
reach the parent, which passes them to the child with Child.send.
A child's delayed transitions use the parent's clock. Start the child with enq.clock,
return Child.nextDeadline from the parent's Machine.deadline, and call
Child.advance and Child.drain in the parent's Machine.tick. The parent's snapshot
then reports the child's deadline as its own, and an actor wakes up for it.
Import as Child with import silk.machine.child { Child }.
Public declarations: 2.
Child#
pub struct Child<S, E, C, X>A child machine of states S, events E, context C and effects X. It is absent until
it is started and after it is stopped.
Field snapshot#
pub snapshot: Option<Snapshot<S, E, C, X>>Implementation Schema for Child<S, E, C, X>#
impl<S: Schema, E: Schema, C: Schema, X: Schema> Schema for Child<S, E, C, X>Implementation Child<S, E, C, X>#
impl<S: Machine<E, C, X>, E, C, X> Child<S, E, C, X>Associated function absent#
pub fn absent() -> Child<S, E, C, X>Returns a child that is not started.
Method start#
pub effect fn start(self: &mut Self, context: C, now: u64) -> ()
! OutOfMemoryError
? &mut AllocatorStarts the child with context at the wall clock time now, in nanoseconds. A child that
was running is replaced.
Method stop#
pub fn stop(self: &mut Self) -> ()Stops the child and drops its state.
Method isActive#
pub fn isActive(self: &Self) -> boolReturns whether the child is started and can still take events.
Method isDone#
pub fn isDone(self: &Self) -> boolReturns whether the child reached a final state.
Method send#
pub effect fn send(self: &mut Self, event: E) -> bool
! OutOfMemoryError
? &mut AllocatorSends event to the child and returns whether one of its states handled it. A child that
is absent or finished handles nothing. A child stuck in eventless transitions is marked
Status.Failed.
Method advance#
pub effect fn advance(self: &mut Self, now: u64) -> bool
! OutOfMemoryError
? &mut AllocatorMoves the child's clock to now and takes its delayed transitions that are due. Returns
whether one was taken.
Method nextDeadline#
pub fn nextDeadline(self: &Self) -> Option<u64>Returns the deadline of the child's timer that fires first, in nanoseconds of wall clock
time. Return it from the parent's Machine.deadline, and call Child.advance in the
parent's Machine.tick, so the parent's actor wakes up for the child's delayed transitions.
Method drain#
pub effect fn drain<PE, PX>(
self: &mut Self,
enq: &mut Enqueue<PE, PX>,
wrap: fn<'static>(X) -> PX,
) -> ()
! OutOfMemoryError
? &mut AllocatorMoves the effects the child queued into the parent's queue, each wrapped by wrap.
Replies#
pub struct RepliesImplementation Replies#
impl RepliesAssociated function make#
pub fn make<E>() -> Reply<E>Returns an empty reply for a child's performer to fill.
Associated function forward#
pub effect fn forward<E, PE>(from: Reply<E>, to: &mut Reply<PE>, wrap: fn<'static>(E) -> PE) -> ()
! OutOfMemoryError
? &mut AllocatorMoves the events a child's performer sent into the parent's reply, each wrapped by wrap.
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.