xmachines-runtime 6.0.0

XState v6 semantics in Rust: a port of XState's interpreter and actor runtime; not affiliated with Stately
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! The compiled form of a JSON machine definition: the structural [`Tree`]
//! shared with every host, plus the values the native host needs.

use std::sync::Arc;

use serde_json::{Map, Value};

use crate::core::{Nid, Tid, Tree};
use crate::event::{matches_descriptor, Ev};
use crate::registry::{ActionFn, ContextFn, DelayFn, ExprFn, Factory, GuardFn};
use crate::system::{EffectExec, Logic};
use crate::FixedMap;

/// Index of a state node, in document order.
pub type NodeId = Nid;

pub(crate) const ROOT: Nid = 0;

/// Per-node values.
pub(crate) struct NodeData {
    pub key: String,
    pub id: String,
    pub entry: Vec<Action>,
    pub exit: Vec<Action>,
    /// Delayed transitions' timers, in XState's order: `after` entries,
    /// the state `timeout`, then invoke timeouts.
    pub delayed: Vec<DelayedDef>,
    pub invokes: Vec<InvokeDef>,
    pub output: Option<Template>,
    pub tags: Vec<String>,
    /// The static targets of a compound state's `initial`.
    pub initial: Arc<[Nid]>,
    /// The `input` of an object-form `initial` (`{ target, input }`), for
    /// the states it enters (taken as it is: XState does not evaluate it).
    pub initial_input: Option<Value>,
    /// The default targets of a history state.
    pub history_target: Arc<[Nid]>,
    /// Whether the state declares `onError`, which recovers an actor from
    /// an execution error (XState's `getExecutionErrorEvent`).
    pub on_error: bool,
}

/// A timer a state schedules on entry and cancels on exit; its event is
/// caught by the state's delayed transitions.
pub(crate) struct DelayedDef {
    pub kind: DelayedKind,
    pub delay: DelaySpec,
}

pub(crate) enum DelayedKind {
    /// `after`: raises `{ type: "xstate.after", delay, stateId }`, where
    /// `delay` is a number for numeric keys and the key string otherwise.
    After { event_delay: Value },
    /// The state `timeout`: raises `{ type: "xstate.timeout.state", stateId }`.
    StateTimeout,
    /// An invoke `timeout`: raises `{ type: "xstate.timeout.actor", actorId, sessionId }`.
    InvokeTimeout { invoke_id: String },
}

/// One `invoke` of a state.
pub(crate) struct InvokeDef {
    pub id: String,
    pub logic: Arc<dyn Logic>,
    pub input: Option<Template>,
    pub registry_key: Option<String>,
    /// Whether the invoke has `onSnapshot` (the child then reports snapshots).
    pub sync_snapshot: bool,
}

/// A custom action reference: its name and params, and the bound Rust
/// implementation (if the registry provides one).
pub(crate) struct CustomAction {
    pub name: String,
    pub params: Option<Value>,
    pub exec: EffectExec,
}

/// A custom action or guard whose `params` hold expressions: its
/// implementation is bound to the params each time it runs.
pub(crate) struct Dynamic<T> {
    pub name: String,
    pub factory: Factory<T>,
    pub params: Template,
}

/// A JSON value with expressions in it (XState's resolvable values).
#[derive(Debug, Clone)]
pub(crate) enum Template {
    Static(Value),
    /// Index into `Machine::exprs` (or `Machine::context_exprs` in the root
    /// `context`).
    Expr(u32),
    Object(Vec<(String, Template)>),
    Array(Vec<Template>),
}

impl Template {
    /// The value, with `expr` computing each expression.
    pub fn eval(&self, expr: &mut dyn FnMut(u32) -> Value) -> Value {
        match self {
            Template::Static(v) => v.clone(),
            Template::Expr(i) => expr(*i),
            Template::Object(fields) => Value::Object(fields.iter().map(|(k, t)| (k.clone(), t.eval(expr))).collect()),
            Template::Array(items) => Value::Array(items.iter().map(|t| t.eval(expr)).collect()),
        }
    }
}

#[derive(Debug, Clone, Copy)]
pub(crate) enum DelaySpec {
    Fixed(u64),
    Named(u32),
    /// An expression (index into `Machine::exprs`): milliseconds or a
    /// duration string.
    Expr(u32),
}

pub(crate) struct TransitionData {
    /// `None` for a targetless transition.
    pub targets: Option<Arc<[Nid]>>,
    pub matches: Vec<(String, Value)>,
    pub guard: Option<Guard>,
    /// The transition's `context` patch.
    pub context: Option<Map<String, Value>>,
    /// Instead of `context`: a patch with expressions in it.
    pub context_expr: Option<Template>,
    pub actions: Vec<Action>,
    pub reenter: bool,
    /// For invoke completion transitions: the child's id, whose session must
    /// match the event's `sessionId` (XState's `_eventMatcher`).
    pub session: Option<String>,
    /// For completion transitions of an invoke with a `timeout`: the timer
    /// key the transition cancels after its own operations.
    pub cancel_timeout: Option<crate::system::TimerKey>,
    /// A transition function (an expression in the `transition` slot):
    /// its result gives the targets, context and `reenter`.
    pub function: Option<TransitionFn>,
    /// The `input` the transition gives the states it enters.
    pub input: Option<Template>,
    /// [`TransitionData::evaluates`], computed once after loading.
    pub selects: bool,
}

/// XState's error for rejection reasons that are not a nonempty array of
/// objects with a string `type`.
pub(crate) const REJECT_REASONS: &str =
    "reject(...) requires a nonempty array of rejection objects with a string type.";

/// A transition computed when it is selected, as XState's `to` functions
/// (expression indices are into `Machine::exprs`).
#[derive(Debug, Clone)]
pub(crate) enum TransitionFn {
    /// A transition expression, with `enq`.
    Expr(u32),
    /// A route function: its state is the target of every result (a falsy
    /// result blocks the route, `true` takes it as it is).
    Route { expr: u32, target: Arc<[Nid]> },
    /// A choice state's expression, which must give a target.
    ChoiceExpr { expr: u32, id: String },
    /// A choice state's branches: the first whose `when` passes.
    Choice { branches: Vec<ChoiceBranch>, path: String, id: String },
    /// A JSON rejecting transition (`{ reject: [...] }`): its reasons, as
    /// `machineFromJSON` compiles it to a transition function returning
    /// `reject(reasons)`.
    Reject(Template),
}

#[derive(Debug, Clone)]
pub(crate) struct ChoiceBranch {
    pub when: Option<Guard>,
    pub targets: Option<Arc<[Nid]>>,
    pub context: Option<Template>,
    pub input: Option<Template>,
    pub reenter: bool,
}

impl TransitionData {
    /// Whether XState compiles this transition to a transition function
    /// (`machineFromJSON` does so for any `context` or `actions`; invoke
    /// timeouts wrap completion transitions in one).
    pub fn is_dynamic(&self) -> bool {
        self.context.is_some()
            || self.context_expr.is_some()
            || !self.actions.is_empty()
            || self.cancel_timeout.is_some()
            || self.function.is_some()
    }

    /// Whether the transition runs expressions, so its result is computed
    /// when it is selected, as XState evaluates transition functions.
    pub fn evaluates(&self) -> bool {
        self.function.is_some() || self.context_expr.is_some() || self.actions.iter().any(Action::evaluates)
    }
}

#[derive(Debug, Clone)]
pub(crate) enum Guard {
    Custom(u32),
    Not(Box<Guard>),
    StateIn(Nid),
    /// A route's match: the event's `to` is this `#id`.
    RouteTo(String),
    /// Both, in order (a route's match, then its named guard).
    And(Box<Guard>, Box<Guard>),
    /// An inline expression, truthy as in JavaScript.
    Expr(u32),
    /// A custom guard with expressions in its params (index into
    /// `Machine::dynamic_guards`).
    Dynamic(u32),
    /// A named JSON guard with params its expressions read: the params
    /// (resolved in the outer scope), then the guard.
    Scoped(Box<Template>, Box<Guard>),
    /// `@xstate.stateIn` with expressions in its params (the state id is
    /// resolved when the guard runs).
    StateInExpr(Box<Template>),
}

#[derive(Debug, Clone)]
pub(crate) enum Action {
    /// Index into `Machine::raise_events`.
    Raise {
        event: u32,
        id: Option<String>,
        delay: Option<u64>,
    },
    Cancel(String),
    Log(Vec<Value>),
    Emit(Value),
    Assign(Map<String, Value>),
    Custom(u32),
    /// `@xstate.raise` of an event with expressions in it.
    RaiseExpr {
        event: Template,
        id: Option<String>,
        delay: Option<u64>,
    },
    EmitExpr(Template),
    AssignExpr(Template),
    /// Index into `Machine::dynamic_actions`.
    CustomExpr(u32),
    /// An expression as an action: its result's `context` is merged.
    Expr(u32),
    /// The start of a named JSON action whose params its expressions read:
    /// the params, resolved here, are their scope's until `EndParams`.
    Params(Template),
    EndParams,
}

impl Action {
    /// Whether the action writes the context (and so needs the context it
    /// starts from).
    pub fn assigns(&self) -> bool {
        matches!(self, Action::Assign(_) | Action::AssignExpr(_) | Action::Expr(_))
    }

    /// Whether the action evaluates expressions (which read the context).
    pub fn evaluates(&self) -> bool {
        matches!(
            self,
            Action::RaiseExpr { .. }
                | Action::EmitExpr(_)
                | Action::AssignExpr(_)
                | Action::CustomExpr(_)
                | Action::Expr(_)
                | Action::Params(_)
        )
    }
}

pub(crate) enum InitialContext<C> {
    Value(C),
    Template(Template),
}

/// The options of a machine definition (XState's machine `options`).
#[derive(Debug, Clone, Default)]
pub(crate) struct MachineOptions {
    /// The microstep bound for one macrostep (`options.maxIterations`):
    /// more is an infinite loop. Unbounded when the definition sets none.
    pub(crate) max_iterations: Option<usize>,
}

/// A compiled machine definition. Create actors from it with
/// [`crate::Actor::start`]; one machine can be shared by many actors.
pub struct Machine<C, E> {
    pub(crate) id: Option<String>,
    pub(crate) version: Option<String>,
    pub(crate) tree: Tree,
    pub(crate) nodes: Vec<NodeData>,
    pub(crate) transitions: Vec<TransitionData>,
    pub(crate) guards: Vec<GuardFn<C, E>>,
    pub(crate) actions: Vec<CustomAction>,
    pub(crate) actors: Vec<(String, Arc<dyn Logic>)>,
    pub(crate) delays: Vec<DelayFn<C, E>>,
    pub(crate) raise_events: Vec<Ev<E>>,
    pub(crate) exprs: Vec<ExprFn<C, E>>,
    pub(crate) context_exprs: Vec<ContextFn>,
    pub(crate) dynamic_actions: Vec<Dynamic<ActionFn>>,
    pub(crate) dynamic_guards: Vec<Dynamic<GuardFn<C, E>>>,
    /// The registry's custom actions, for `enq.action` in expressions.
    pub(crate) action_factories: FixedMap<String, Factory<ActionFn>>,
    /// The custom actions left to the host, which run no implementation and
    /// stay pending until the host says they are done.
    pub(crate) host_actions: crate::registry::HostActions,
    pub(crate) id_map: FixedMap<String, Nid>,
    pub(crate) internal_events: Vec<String>,
    /// The initial context: a value, or, when the root `context` has
    /// expressions, computed from the actor's input at start.
    pub(crate) initial_context: InitialContext<C>,
    pub(crate) output: Option<Template>,
    pub(crate) options: MachineOptions,
    /// Why starting fails although the definition loads: a root `initial`
    /// without child states, which XState accepts and fails on at start.
    pub(crate) start_error: Option<String>,
    /// Empty snapshot parts that new snapshots share until they first write
    /// them (copy-on-write), so an actor allocates none it does not use.
    pub(crate) empty: crate::interp::EmptyParts<E>,
}

/// The machine's `id`, `version` and size; its implementations are closures.
impl<C, E> std::fmt::Debug for Machine<C, E> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Machine")
            .field("id", &self.id)
            .field("version", &self.version)
            .field("states", &self.nodes.len())
            .field("transitions", &self.transitions.len())
            .finish_non_exhaustive()
    }
}

impl<C, E> Machine<C, E> {
    /// Whether the custom action `name` is left to the host.
    pub(crate) fn is_host_action(&self, name: &str) -> bool {
        self.host_actions.includes(name, self.action_factories.contains_key(name))
    }

    /// The machine's `id`, if the definition declares one.
    pub fn id(&self) -> Option<&str> {
        self.id.as_deref()
    }

    /// The definition's `version`, if any.
    pub fn version(&self) -> Option<&str> {
        self.version.as_deref()
    }

    /// The id of every state node, in document order.
    pub fn state_ids(&self) -> impl Iterator<Item = &str> {
        self.nodes.iter().map(|n| n.id.as_str())
    }

    pub(crate) fn node_by_id(&self, id: &str) -> Option<Nid> {
        self.id_map.get(id).copied()
    }

    /// XState's `resolveTarget` for one target of a transition from
    /// `source`: `#id`, `.child.path` below the source, or a path below the
    /// source's parent.
    pub(crate) fn resolve_target(&self, source: Nid, target: &str) -> Result<Nid, String> {
        if let Some(id) = target.strip_prefix('#') {
            return self.node_by_id(id).ok_or_else(|| format!("no state has id \"{id}\""));
        }
        let (mut current, path) = match target.strip_prefix('.') {
            Some(rest) => (source, rest),
            None => match self.tree.node(source).parent {
                Some(parent) => (parent, target),
                None => return Err(format!("invalid target \"{target}\" on the root; use \".child\" or \"#id\"")),
            },
        };
        for segment in crate::load::to_state_path(path) {
            if segment.is_empty() {
                break;
            }
            let node = self.tree.node(current);
            current = node
                .children
                .iter()
                .chain(&node.history_children)
                .copied()
                .find(|&c| self.data(c).key == segment)
                .ok_or_else(|| format!("child state '{segment}' does not exist on '{}'", self.data(current).id))?;
        }
        Ok(current)
    }

    #[inline]
    pub(crate) fn data(&self, n: Nid) -> &NodeData {
        &self.nodes[n as usize]
    }

    pub(crate) fn transition(&self, t: Tid) -> &TransitionData {
        &self.transitions[t as usize]
    }

    pub(crate) fn is_internal_event(&self, event_type: &str) -> bool {
        self.internal_events.iter().any(|d| matches_descriptor(event_type, d))
    }

    /// XState's state value for a configuration.
    pub(crate) fn state_value(&self, config: &[Nid]) -> Value {
        let all = crate::core::all_state_nodes(&self.tree, config, |n| self.data(n).initial.clone());
        let adj = self.tree.active_children(all.as_slice());
        self.node_value(ROOT, &adj)
    }

    fn node_value(&self, node: Nid, adj: &[Vec<Nid>]) -> Value {
        let children = &adj[node as usize];
        if children.is_empty() {
            return Value::Object(Map::new());
        }
        if self.tree.kind(node) == crate::core::Kind::Compound {
            let child = children[0];
            if self.tree.kind(child).is_atomic() {
                return Value::String(self.data(child).key.clone());
            }
        }
        let mut map = Map::new();
        for &c in children {
            map.insert(self.data(c).key.clone(), self.node_value(c, adj));
        }
        Value::Object(map)
    }
}