xmachines-runtime 5.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
//! Rust-defined actor logic: XState's `createStepLogic` and
//! `createAsyncLogic`, with identical snapshots and effects.
//!
//! Async work never runs inside the system. Async logic asks for it with an
//! `@xstate.effect` external effect; the host does the work and delivers the
//! outcome with [`crate::System::resolve_effect`] and an
//! `xstate.async.resolve` (`data`) or `xstate.async.reject` (`data`) event.

use std::any::Any;
use std::sync::Arc;

use serde_json::{json, Map, Value};

use crate::system::{
    parse_status, AnySnapshot, Context, Effect, EffectExec, EffectKind, EffectType, ExternalSpec, Logic, Status,
    SystemError, Transition,
};

/// A step logic snapshot: XState's `{ status, output, error, input, context, effects }`.
#[derive(Debug, Clone, PartialEq)]
pub struct StepSnapshot {
    pub status: Status,
    pub output: Option<Value>,
    pub error: Option<Value>,
    pub input: Option<Value>,
    pub context: Option<Value>,
    /// Keyed effect states, such as `{ "async": { "status": "active" } }`.
    pub effects: Option<Map<String, Value>>,
}

impl AnySnapshot for StepSnapshot {
    fn status(&self) -> Status {
        self.status
    }

    fn output(&self) -> Option<Value> {
        self.output.clone()
    }

    fn error(&self) -> Option<Value> {
        self.error.clone()
    }

    fn fail(&mut self, error: Value) {
        self.status = Status::Error;
        self.error = Some(error);
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }

    fn clone_box(&self) -> Option<Box<dyn AnySnapshot>> {
        Some(Box::new(self.clone()))
    }
}

impl StepSnapshot {
    /// XState's snapshot data.
    pub fn to_data(&self) -> Value {
        let mut m = Map::new();
        m.insert("status".into(), Value::String(self.status.as_str().into()));
        if let Some(v) = &self.output {
            m.insert("output".into(), v.clone());
        }
        if let Some(v) = &self.error {
            m.insert("error".into(), v.clone());
        }
        if let Some(v) = &self.input {
            m.insert("input".into(), v.clone());
        }
        if let Some(v) = &self.context {
            m.insert("context".into(), v.clone());
        }
        if let Some(v) = &self.effects {
            m.insert("effects".into(), Value::Object(v.clone()));
        }
        Value::Object(m)
    }
}

/// What a step function sees.
pub struct StepArgs<'a> {
    pub context: Option<&'a Value>,
    pub event: &'a Value,
    pub input: Option<&'a Value>,
}

/// A partial update of the snapshot, as returned by XState's `run`.
/// `Some(None)` for `input` clears it.
#[derive(Debug, Clone, Default)]
pub struct StepPatch {
    pub context: Option<Value>,
    pub status: Option<Status>,
    pub output: Option<Value>,
    pub error: Option<Value>,
    pub input: Option<Option<Value>>,
    pub effects: Option<Map<String, Value>>,
}

/// The step logic enqueuer.
pub struct StepEnqueue<'a> {
    effects: Vec<Effect>,
    tracked: Map<String, Value>,
    recorded: Option<&'a Map<String, Value>>,
    is_init: bool,
    parent: Option<crate::system::ActorRef>,
    self_ref: crate::system::ActorRef,
}

impl StepEnqueue<'_> {
    /// Emits an event to observers.
    pub fn emit(&mut self, event: Value) {
        let mut params = event.as_object().cloned().unwrap_or_default();
        params.remove("type");
        let ty = event.get("type").and_then(Value::as_str).unwrap_or("").to_owned();
        self.effects.push(Effect::External(ExternalSpec {
            kind: EffectKind::Emit,
            ty: EffectType::Fixed(ty),
            params: Some(Value::Object(params)),
            args: Vec::new(),
            event: Some(event),
            reason: None,
            exec: None,
        }));
    }

    /// Sends an event to the parent actor.
    pub fn send_back(&mut self, event: Value) {
        if let Some(parent) = self.parent.clone() {
            self.effects.push(Effect::SendTo { target: parent, event });
        }
    }

    /// Sends an event back into this logic.
    pub fn raise(&mut self, event: Value) {
        self.effects.push(Effect::SendTo { target: self.self_ref.clone(), event });
    }

    /// Asks the host to run a side effect.
    pub fn effect(&mut self, exec: Option<EffectExec>) {
        self.effects.push(Effect::External(ExternalSpec {
            kind: EffectKind::Action,
            ty: EffectType::Fixed("@xstate.effect".into()),
            params: None,
            args: Vec::new(),
            event: None,
            reason: None,
            exec,
        }));
    }

    /// Asks the host to run a keyed side effect once; tracked on
    /// `snapshot.effects[key]`.
    pub fn effect_keyed(&mut self, key: &str, exec: Option<EffectExec>) {
        let recorded = self.recorded.and_then(|r| r.get(key));
        if let Some(recorded) = recorded {
            let active = recorded.get("status").and_then(Value::as_str) == Some("active");
            if !(self.is_init && active) {
                return;
            }
        }
        self.effects.push(Effect::External(ExternalSpec {
            kind: EffectKind::Action,
            ty: EffectType::Fixed("@xstate.effect".into()),
            params: Some(json!({ "key": key })),
            args: Vec::new(),
            event: None,
            reason: None,
            exec,
        }));
        self.tracked.insert(key.to_owned(), json!({ "status": "active" }));
    }
}

/// The function that defines a step logic.
pub type StepFn = Arc<dyn Fn(&StepArgs<'_>, &mut StepEnqueue<'_>) -> Option<StepPatch> + Send + Sync>;

/// Computes a step logic's initial context from its input.
pub type ContextFn = Arc<dyn Fn(Option<&Value>) -> Option<Value> + Send + Sync>;

/// XState's `createStepLogic`: an actor with context that handles events.
pub struct StepLogic {
    id: Option<String>,
    context: Option<ContextFn>,
    run: StepFn,
    timeout: Option<(Value, u64)>,
}

impl StepLogic {
    /// A step logic. `context` computes the initial context from the input.
    pub fn new(id: Option<String>, context: Option<ContextFn>, run: StepFn) -> Self {
        Self { id, context, run, timeout: None }
    }

    /// The logic's `id`.
    pub fn id(&self) -> Option<&str> {
        self.id.as_deref()
    }

    fn step(&self, snapshot: StepSnapshot, event: &Value, cx: &mut dyn Context) -> Transition {
        if snapshot.status != Status::Active {
            return Transition { snapshot: Box::new(snapshot), effects: Vec::new() };
        }
        let ty = event.get("type").and_then(Value::as_str).unwrap_or("");
        let with_effect = |mut s: StepSnapshot, key: Option<&Value>, state: Value| {
            let key = key.and_then(Value::as_str).unwrap_or("").to_owned();
            s.effects.get_or_insert_with(Map::new).insert(key, state);
            Transition { snapshot: Box::new(s), effects: Vec::new() }
        };
        match ty {
            "xstate.stop" => {
                let mut s = snapshot;
                s.status = Status::Stopped;
                s.input = None;
                let cleanup = Effect::External(ExternalSpec {
                    kind: EffectKind::Action,
                    ty: EffectType::Fixed("@xstate.cleanup".into()),
                    params: None,
                    args: Vec::new(),
                    event: None,
                    reason: None,
                    exec: None,
                });
                return Transition { snapshot: Box::new(s), effects: vec![cleanup] };
            }
            "xstate.logic.effect.start" => {
                return with_effect(snapshot, event.get("key"), json!({ "status": "active" }))
            }
            "xstate.logic.effect.resolve" => {
                let mut state = json!({ "status": "done" });
                if let Some(o) = event.get("output") {
                    state["output"] = o.clone();
                }
                return with_effect(snapshot, event.get("key"), state);
            }
            "xstate.logic.effect.reject" => {
                let mut state = json!({ "status": "error" });
                if let Some(e) = event.get("error") {
                    state["error"] = e.clone();
                }
                return with_effect(snapshot, event.get("key"), state);
            }
            _ => {}
        }
        let self_ref = cx.self_ref();
        let mut enq = StepEnqueue {
            effects: Vec::new(),
            tracked: Map::new(),
            recorded: snapshot.effects.as_ref(),
            is_init: ty == "xstate.init",
            parent: cx.parent_ref(),
            self_ref,
        };
        let patch = (self.run)(
            &StepArgs { context: snapshot.context.as_ref(), event, input: snapshot.input.as_ref() },
            &mut enq,
        );
        let StepEnqueue { effects, tracked, .. } = enq;
        let mut next = snapshot.clone();
        if let Some(p) = &patch {
            if let Some(c) = &p.context {
                next.context = Some(c.clone());
            }
            if let Some(s) = p.status {
                next.status = s;
            }
            if let Some(o) = &p.output {
                next.output = Some(o.clone());
            }
            if let Some(e) = &p.error {
                next.error = Some(e.clone());
            }
            if let Some(i) = &p.input {
                next.input = i.clone();
            }
        }
        let patch_effects = patch.as_ref().and_then(|p| p.effects.clone());
        if patch_effects.is_some() || !tracked.is_empty() {
            let mut merged = snapshot.effects.clone().unwrap_or_default();
            for (k, v) in patch_effects.into_iter().flatten() {
                merged.insert(k, v);
            }
            for (k, v) in tracked {
                merged.insert(k, v);
            }
            next.effects = Some(merged);
        }
        let mut effects = effects;
        if matches!(next.status, Status::Done | Status::Error) {
            effects.push(Effect::Terminate);
        }
        Transition { snapshot: Box::new(next), effects }
    }
}

impl Logic for StepLogic {
    fn initial_transition(&self, input: Option<&Value>, cx: &mut dyn Context) -> Result<Transition, SystemError> {
        let snapshot = StepSnapshot {
            status: Status::Active,
            output: None,
            error: None,
            input: input.cloned(),
            context: self.context.as_ref().and_then(|f| f(input)),
            effects: None,
        };
        let mut init = Map::new();
        init.insert("type".into(), Value::String("xstate.init".into()));
        if let Some(i) = input {
            init.insert("input".into(), i.clone());
        }
        Ok(self.step(snapshot, &Value::Object(init), cx))
    }

    fn transition(
        &self,
        snapshot: &mut Box<dyn AnySnapshot>,
        event: Value,
        cx: &mut dyn Context,
    ) -> Result<Vec<Effect>, SystemError> {
        let event = &event;
        let current = crate::system::downcast::<StepSnapshot>(&**snapshot)
            .cloned()
            .ok_or_else(|| SystemError::Failed("step logic received a foreign snapshot".into()))?;
        let result = self.step(current, event, cx);
        *snapshot = result.snapshot;
        Ok(result.effects)
    }

    fn logic_id(&self) -> Option<String> {
        self.id.clone()
    }

    fn snapshot_data(&self, snapshot: &dyn AnySnapshot) -> Value {
        crate::system::downcast::<StepSnapshot>(snapshot).map_or(Value::Null, StepSnapshot::to_data)
    }

    fn restore(&self, data: &Value) -> Result<Box<dyn AnySnapshot>, SystemError> {
        Ok(Box::new(StepSnapshot {
            status: parse_status(data)?,
            output: data.get("output").cloned(),
            error: data.get("error").cloned(),
            input: data.get("input").cloned(),
            context: data.get("context").cloned(),
            effects: data.get("effects").and_then(Value::as_object).cloned(),
        }))
    }

    fn async_timeout(&self) -> Option<(Value, u64)> {
        self.timeout.clone()
    }
}

/// XState's `createAsyncLogic`: one unit of async work, run by the host.
///
/// On start it requests an `@xstate.effect`; deliver
/// `{ "type": "xstate.async.resolve", "data": output }` or
/// `{ "type": "xstate.async.reject", "data": error }` as its result. With a
/// `timeout` (milliseconds, or a duration string such as `"10s"`), the
/// system rejects it with a `TimeoutError` when the deadline passes.
pub fn async_logic(id: Option<String>, timeout: Option<Value>) -> Result<StepLogic, SystemError> {
    let timeout = match timeout {
        None => None,
        Some(v) => {
            let ms = match &v {
                Value::Number(n) => n.as_f64(),
                Value::String(s) => crate::delay::parse_duration_ms(s),
                _ => None,
            }
            .and_then(crate::delay::ms_to_u64)
            .ok_or_else(|| SystemError::Rejected("Unable to resolve async actor timeout.".into()))?;
            Some((v, ms))
        }
    };
    let run: StepFn = Arc::new(|args, enq| {
        let ty = args.event.get("type").and_then(Value::as_str).unwrap_or("");
        match ty {
            "xstate.async.resolve" => {
                let output = args.event.get("data").or_else(|| args.event.get("output")).cloned();
                let mut state = json!({ "status": "done" });
                if let Some(o) = &output {
                    state["output"] = o.clone();
                }
                Some(StepPatch {
                    status: Some(Status::Done),
                    output,
                    input: Some(None),
                    effects: Some(Map::from_iter([("async".to_owned(), state)])),
                    ..Default::default()
                })
            }
            "xstate.async.reject" => {
                let error = args.event.get("data").or_else(|| args.event.get("error")).cloned();
                let mut state = json!({ "status": "error" });
                if let Some(e) = &error {
                    state["error"] = e.clone();
                }
                Some(StepPatch {
                    status: Some(Status::Error),
                    error,
                    input: Some(None),
                    effects: Some(Map::from_iter([("async".to_owned(), state)])),
                    ..Default::default()
                })
            }
            "xstate.init" => {
                enq.effect(None);
                Some(StepPatch {
                    effects: Some(Map::from_iter([("async".to_owned(), json!({ "status": "active" }))])),
                    ..Default::default()
                })
            }
            _ => None,
        }
    });
    Ok(StepLogic { id, context: None, run, timeout })
}