silk.machine.store.

Stores: a context and one function that changes it for each event.

When to use#

Use a store for state that has no modes: a counter, a form, a cart. Implement Reducer for the context, start the store with Stores.begin and send it events with Stores.send. Reach for a machine when what an event does depends on a state.

Details#

A store is a machine with one state, Only, so a Store is a Snapshot. It is written to JSON and read back like any snapshot, and Actors and Durable run it and perform the effects its reducer queues.

Import as Stores with import silk.machine.store { Stores }.

Public declarations: 4.

Only#

silk
pub union Only<C>

The one state of a store whose context is C.

Active#

silk
Active

Implementation Schema for Only<C>#

silk
impl<C> Schema for Only<C>

Reducer#

silk
pub interface Reducer<E, X>

A context that changes itself for each event of type E and can queue effects of type X.

Operation reduce#

silk
effect fn reduce(self: &mut Self, event: &E, enq: &mut Enqueue<E, X>) -> ()
    ! OutOfMemoryError
    ? &mut Allocator

Applies event to the context. enq takes effects to perform and events to process next.

Implementation Machine<E, C, X> for Only<C>#

silk
impl<E, C: Reducer<E, X>, X> Machine<E, C, X> for Only<C>

Operation chart#

silk
static fn chart() -> Chart<Only<C>, E, C, X>

Operation handle#

silk
effect fn handle(state: &Only<C>, context: &mut C, event: &E, enq: &mut Enqueue<E, X>) -> Next<
    Only<C>
  >
    ! OutOfMemoryError
    ? &mut Allocator

Store#

silk
pub type Store<E, C, X> = Snapshot<Only<C>, E, C, X>

A store of events E, context C and effects X. The context is store.context.

Stores#

silk
pub struct Stores

Implementation Stores#

silk
impl Stores

Associated function begin#

silk
pub effect fn begin<E, C: Reducer<E, X>, X>(context: C) -> Store<E, C, X>
    ! OutOfMemoryError | TransitionLoopError
    ? &mut Allocator

Returns a store that starts with context.

Associated function send#

silk
pub effect fn send<E, C: Reducer<E, X>, X>(store: &mut Store<E, C, X>, event: E) -> ()
    ! OutOfMemoryError | TransitionLoopError
    ? &mut Allocator

Applies event to the store, then every event the reducer raised. Effects the reducer queued wait in store.pending. Use Actors.send in its place to perform them.

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