Expand description
§xmachines-machine-wasi
Typed WASI 0.2 components for one machine each: the machine’s guards, actions, delays and invoked actors are Rust compiled into the component (no call into the host per guard), and its events are checked against the machine’s generated types at the boundary.
§When to use it
Use it when a machine and its implementations are known at build time and should ship as one component: the host imports nothing but WASI and only drives actors (and settles the invoked work the machine leaves to it). The generic component of xmachines-runtime-wasi loads any definition at run time and calls the host for every guard and action; this one is built per machine. The same machine builds a Node.js addon with xmachines-machine-node.
§Example
A cdylib crate built for wasm32-wasip2 implements TypedMachine of xmachines-machine and calls export_machine!:
use serde_json::Value;
use xmachines_machine::TypedMachine;
use xmachines_runtime::{Machine, Registry};
struct Toggle;
impl TypedMachine for Toggle {
type Context = Value;
type Event = Value;
const DEFINITION: &'static str = r#"{
"id": "toggle", "initial": "off",
"states": { "off": { "on": { "toggle": { "target": "on" } } }, "on": {} }
}"#;
const SCHEMAS: &'static str = "{}";
fn machine() -> Result<Machine<Value, Value>, String> {
Machine::from_json_str(Self::DEFINITION, &Registry::new()).map_err(|e| e.to_string())
}
}
xmachines_machine_wasi::export_machine!(Toggle);With codegen, Context, Event, DEFINITION, SCHEMAS and machine() come from the generated module, and the implementations are the generated trait’s (examples/checkout-wasi):
cargo build -p checkout-wasi --target wasm32-wasip2 --release # about 1.1 MBThe component-model exports are compiled only for wasm32; a native build type-checks the crate, as above.
§Contract
The world xmachines:machine/machine of the WIT package xmachines:machine@0.3.0 (wit/machine.wit) exports:
info: the machine’sid((machine)when the definition has none, the runtime’s id for such a machine and its root actor),version,definitionandschemas(the bundle’s JSON Schemas);actors: themachineandactorresources ofxmachines:runtime, themachineloaded without a definition (load(options), withmaxSteps,maxTimersandobserveSystemEmitted;start(options, now);restore(snapshot); each error the contract’s{ "code", "message" },WORKFLOW_INVALID_INPUTorWORKFLOW_INVALID_MACHINEof a load,WORKFLOW_MACHINE_FAILEDorWORKFLOW_INVALID_INPUTof a start,WORKFLOW_INVALID_SNAPSHOTof a restore): the component holds its one machine, compiled once per instance, and amachineresource adds its load options. Theactoris the same (apply, whose error is a rejection{ "rejected": { "message", "data"? } }or a fault{ "code", "message" };can, whether the root would select a transition for the event, an empty one among them;persist,take-logs,take-system-emitted,stop); an event input that is not an event of the machine refuses the chunk asWORKFLOW_INVALID_INPUT.
Custom actions are compiled in, but for the host actions the machine’s bundle names (hostActions): the registry xmachines-build generates leaves them to the host, as work of kind action it settles done.
Values cross as JSON text, since WIT has no maps, no recursive types and no unknown, which JSON Schemas of contexts and events use freely, and the events usually arrive as JSON anyway (Kafka records, HTTP bodies). The package’s own JSON Schemas (load options, info.schemas) are in wit/schemas/; the values its actor shares with xmachines:runtime use that package’s.
§Runs: the record protocol
export_machine! also exports xmachines:workflow/machine-runs@0.1.0: runs of the component’s machine on the record protocol, run.open(options) (no definition: the machine is compiled in), apply(chunk) with its records out, can, checkpoint, as the generic runtime’s runs. An event that is not an event of the machine is a fault, WORKFLOW_INVALID_INPUT.
§Pins
WASI 0.2 (wasm32-wasip2) and wit-bindgen 0.63.
§Related crates
xmachines-machine: the contract of a typed machine, and the actor API this crate exports.xmachines-wasi-host: Wasmtime hosts of these components (Machine,TypedMachine,MachineActor).xmachines-build: the generated module a component implements.
§License
MIT
Macros§
- export_
machine - Exports
info,actorsandxmachines:workflow/machine-runsfor aTypedMachine.
Structs§
- Actor
Resource - The
actorresource of aTypedMachine: aJsonActorbehind the resource’s calls. A component instance is single-threaded, and its implementations are Rust that cannot call back into it, so the cell is never borrowed twice. - Machine
Resource - The
machineresource of aTypedMachine: aJsonMachine, the machine compiled once per component instance with the load options of this resource. - RunResource
- The
runresource ofxmachines:workflow/machine-runsfor aTypedMachine: a run of its machine, compiled once per component instance, a chunk at a time, records out.