hyphae 0.6.0

Reactive cells and runtime primitives for rship
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
use std::{
    collections::HashMap,
    hash::Hash,
    sync::{
        Arc,
        atomic::{AtomicBool, Ordering},
    },
};

use arc_swap::ArcSwap;

use super::{CellValue, Watchable};
use crate::{
    cell::{Cell, CellImmutable, CellMutable},
    signal::Signal,
};

/// Type alias for transition handler callbacks.
/// Returns a value that is emitted downstream for this transition.
type TransitionFn<S, R> = Arc<dyn Fn(&S, &S) -> R + Send + Sync>;
/// Type alias for state enter/exit callbacks.
type StateFn<S> = Arc<dyn Fn(&S) + Send + Sync>;
/// Type alias for guard condition callbacks.
type GuardFn<S> = Arc<dyn Fn(&S, &S) -> bool + Send + Sync>;
/// Type alias for invalid transition handler callbacks.
type InvalidFn<S> = Arc<dyn Fn(&S, &S) + Send + Sync>;

/// Builder for defining state machine transitions.
pub struct StateMachineBuilder<S, R> {
    transitions: HashMap<(S, S), TransitionFn<S, R>>,
    on_enter: HashMap<S, StateFn<S>>,
    on_exit: HashMap<S, StateFn<S>>,
    guards: HashMap<(S, S), GuardFn<S>>,
    on_any_enter: Vec<StateFn<S>>,
    on_invalid: Option<InvalidFn<S>>,
    default: Option<R>,
}

impl<S: Eq + Hash + CellValue, R: CellValue> StateMachineBuilder<S, R> {
    fn new() -> Self {
        Self {
            transitions: HashMap::new(),
            on_enter: HashMap::new(),
            on_exit: HashMap::new(),
            guards: HashMap::new(),
            on_any_enter: Vec::new(),
            on_invalid: None,
            default: None,
        }
    }

    /// Set the initial value of the output cell.
    /// If not called, `R::default()` is used.
    pub fn with_default(&mut self, value: R) -> &mut Self {
        self.default = Some(value);
        self
    }

    /// Define a valid transition from `from` to `to` with a handler.
    /// The handler receives (from_state, to_state) and returns a value
    /// that is emitted downstream.
    pub fn on<F>(&mut self, from: S, to: S, handler: F) -> &mut Self
    where
        F: Fn(&S, &S) -> R + Send + Sync + 'static,
    {
        self.transitions.insert((from, to), Arc::new(handler));
        self
    }

    /// Handler called when entering a specific state (from any valid transition).
    pub fn on_enter<F>(&mut self, state: S, handler: F) -> &mut Self
    where
        F: Fn(&S) + Send + Sync + 'static,
    {
        self.on_enter.insert(state, Arc::new(handler));
        self
    }

    /// Handler called when exiting a specific state (via any valid transition).
    pub fn on_exit<F>(&mut self, state: S, handler: F) -> &mut Self
    where
        F: Fn(&S) + Send + Sync + 'static,
    {
        self.on_exit.insert(state, Arc::new(handler));
        self
    }

    /// Handler called when entering any state.
    pub fn on_any<F>(&mut self, handler: F) -> &mut Self
    where
        F: Fn(&S) + Send + Sync + 'static,
    {
        self.on_any_enter.push(Arc::new(handler));
        self
    }

    /// Guard condition: transition only happens if predicate returns true.
    pub fn guard<F>(&mut self, from: S, to: S, predicate: F) -> &mut Self
    where
        F: Fn(&S, &S) -> bool + Send + Sync + 'static,
    {
        self.guards.insert((from, to), Arc::new(predicate));
        self
    }

    /// Handler called when an invalid transition is attempted.
    pub fn on_invalid<F>(&mut self, handler: F) -> &mut Self
    where
        F: Fn(&S, &S) + Send + Sync + 'static,
    {
        self.on_invalid = Some(Arc::new(handler));
        self
    }
}

pub trait StateTransitionExt<S>: Watchable<S> {
    /// State machine operator for defining valid transitions and transition handlers.
    ///
    /// Each transition handler returns a value of type `R` that is emitted downstream.
    /// The state machine tracks the source state `S` internally but emits `R`.
    /// The internal state always advances to match the upstream value, even for
    /// undefined transitions — only defined transitions produce output.
    /// Use `on_invalid` to observe undefined transitions without emitting.
    ///
    /// # Example
    ///
    /// ```
    /// use hyphae::{Cell, Mutable, Gettable, StateTransitionExt, FilterExt};
    ///
    /// #[derive(Clone, PartialEq, Eq, Hash, Debug)]
    /// enum State { Idle, Loading, Ready, Error }
    ///
    /// let source = Cell::new(State::Idle);
    /// let sm = source.state_transition(|sm| {
    ///     sm.on(State::Idle, State::Loading, |_, _| true);    // emit true
    ///     sm.on(State::Loading, State::Ready, |_, _| false);  // emit false
    ///     sm.on(State::Loading, State::Error, |_, _| false);  // emit false
    /// });
    ///
    /// // Filter to only react to specific transitions
    /// let triggers = sm.filter(|v| *v);
    /// ```
    #[track_caller]
    fn state_transition<R, F>(&self, configure: F) -> Cell<R, CellImmutable>
    where
        S: CellValue + Eq + Hash,
        R: CellValue + Default,
        F: FnOnce(&mut StateMachineBuilder<S, R>),
        Self: Clone + Send + Sync + 'static,
    {
        let mut builder = StateMachineBuilder::new();
        configure(&mut builder);

        let transitions = Arc::new(builder.transitions);
        let on_enter = Arc::new(builder.on_enter);
        let on_exit = Arc::new(builder.on_exit);
        let guards = Arc::new(builder.guards);
        let on_any_enter = Arc::new(builder.on_any_enter);
        let on_invalid = builder.on_invalid;

        let initial = builder.default.take().unwrap_or_default();
        let derived = Cell::<R, CellMutable>::new(initial);
        let derived = if let Some(name) = self.name() {
            derived.with_name(format!("{}::state_transition", name))
        } else {
            derived
        };

        let weak = derived.downgrade();
        let first = Arc::new(AtomicBool::new(true));
        let current_state: Arc<ArcSwap<S>> = Arc::new(ArcSwap::from_pointee(self.get()));

        let guard = self.subscribe(move |signal| {
            if let Some(d) = weak.upgrade() {
                match signal {
                    Signal::Value(next) => {
                        if first.swap(false, Ordering::SeqCst) {
                            return;
                        }

                        let current = current_state.load();
                        let key = ((**current).clone(), (**next).clone());

                        // Always advance state to track upstream reality.
                        // This ensures undefined transitions don't "stick"
                        // the state machine — only defined transitions
                        // produce output.
                        current_state.store(next.clone());

                        // Check if transition is defined
                        if !transitions.contains_key(&key) {
                            if let Some(ref handler) = on_invalid {
                                handler(&*current, &**next);
                            }
                            return;
                        }

                        // Check guard if defined
                        if let Some(guard_fn) = guards.get(&key)
                            && !guard_fn(&*current, &**next)
                        {
                            return; // Guard rejected
                        }

                        // Valid transition - execute handlers
                        // 1. on_exit for current state
                        if let Some(exit_fn) = on_exit.get(&*current) {
                            exit_fn(&*current);
                        }

                        // 2. transition handler — returns value to emit
                        let output = transitions
                            .get(&key)
                            .map(|trans_fn| trans_fn(&*current, &**next));

                        // 3. on_enter for next state
                        if let Some(enter_fn) = on_enter.get(&**next) {
                            enter_fn(&**next);
                        }

                        // 4. on_any handlers
                        for handler in on_any_enter.iter() {
                            handler(&**next);
                        }

                        // Emit handler's return value
                        if let Some(value) = output {
                            d.notify(Signal::Value(Arc::new(value)));
                        }
                    }
                    Signal::Complete => d.notify(Signal::Complete),
                    Signal::Error(e) => d.notify(Signal::Error(e.clone())),
                }
            }
        });
        derived.own(guard);

        derived.lock()
    }
}

impl<S, W: Watchable<S>> StateTransitionExt<S> for W {}

#[cfg(test)]
mod tests {
    use std::sync::atomic::{AtomicU32, Ordering};

    use super::*;
    use crate::Mutable;

    #[derive(Clone, PartialEq, Eq, Hash, Debug)]
    enum State {
        Idle,
        Loading,
        Ready,
        Error,
    }

    #[test]
    fn test_state_transition_valid() {
        let source = Cell::new(State::Idle);
        let transition_count = Arc::new(AtomicU32::new(0));

        let tc = transition_count.clone();
        let sm = source.state_transition(|sm| {
            sm.on(State::Idle, State::Loading, move |_, _| {
                tc.fetch_add(1, Ordering::SeqCst);
                true
            });
            sm.on(State::Loading, State::Ready, |_, _| true);
            sm.on(State::Loading, State::Error, |_, _| true);
        });

        let emissions = Arc::new(AtomicU32::new(0));
        let e = emissions.clone();
        let _guard = sm.subscribe(move |_| {
            e.fetch_add(1, Ordering::SeqCst);
        });

        assert_eq!(emissions.load(Ordering::SeqCst), 1); // Initial

        // Valid transition
        source.set(State::Loading);
        assert_eq!(emissions.load(Ordering::SeqCst), 2);
        assert_eq!(transition_count.load(Ordering::SeqCst), 1);

        // Another valid transition
        source.set(State::Ready);
        assert_eq!(emissions.load(Ordering::SeqCst), 3);
    }

    #[test]
    fn test_state_transition_undefined_advances_state() {
        let source = Cell::new(State::Idle);
        let sm = source.state_transition(|sm| {
            sm.on(State::Idle, State::Loading, |_, _| true);
            sm.on(State::Loading, State::Ready, |_, _| true);
        });

        let emissions = Arc::new(AtomicU32::new(0));
        let e = emissions.clone();
        let _guard = sm.subscribe(move |_| {
            e.fetch_add(1, Ordering::SeqCst);
        });

        assert_eq!(emissions.load(Ordering::SeqCst), 1); // Initial

        // Undefined: Idle -> Ready — state advances to Ready, no emission
        source.set(State::Ready);
        assert_eq!(emissions.load(Ordering::SeqCst), 1);

        // Undefined: Ready -> Error — state advances to Error, no emission
        source.set(State::Error);
        assert_eq!(emissions.load(Ordering::SeqCst), 1);

        // Undefined: Error -> Loading — state advances to Loading, no emission
        // (state machine tracks actual upstream state)
        source.set(State::Loading);
        assert_eq!(emissions.load(Ordering::SeqCst), 1);

        // Defined: Loading -> Ready — emits!
        source.set(State::Ready);
        assert_eq!(emissions.load(Ordering::SeqCst), 2);
    }

    #[test]
    fn test_state_transition_on_enter_exit() {
        let source = Cell::new(State::Idle);
        let enter_count = Arc::new(AtomicU32::new(0));
        let exit_count = Arc::new(AtomicU32::new(0));

        let ec = enter_count.clone();
        let xc = exit_count.clone();
        let _sm: Cell<bool, _> = source.state_transition(|sm| {
            sm.on(State::Idle, State::Loading, |_, _| true);
            sm.on_exit(State::Idle, move |_| {
                xc.fetch_add(1, Ordering::SeqCst);
            });
            sm.on_enter(State::Loading, move |_| {
                ec.fetch_add(1, Ordering::SeqCst);
            });
        });

        source.set(State::Loading);
        assert_eq!(exit_count.load(Ordering::SeqCst), 1);
        assert_eq!(enter_count.load(Ordering::SeqCst), 1);
    }

    #[test]
    fn test_state_transition_guard() {
        let source = Cell::new(State::Idle);
        let allow = Arc::new(AtomicBool::new(false));

        let a = allow.clone();
        let sm = source.state_transition(|sm| {
            sm.on(State::Idle, State::Loading, |_, _| true);
            sm.guard(State::Idle, State::Loading, move |_, _| {
                a.load(Ordering::SeqCst)
            });
        });

        let emissions = Arc::new(AtomicU32::new(0));
        let e = emissions.clone();
        let _guard = sm.subscribe(move |_| {
            e.fetch_add(1, Ordering::SeqCst);
        });

        // Guard rejects
        source.set(State::Loading);
        assert_eq!(emissions.load(Ordering::SeqCst), 1); // Still 1

        // Reset and allow
        source.set(State::Idle); // This is invalid too, but reset source
        allow.store(true, Ordering::SeqCst);

        // Create fresh cell since current state might be Loading in sm
        let source2 = Cell::new(State::Idle);
        let a2 = allow.clone();
        let sm2 = source2.state_transition(|sm| {
            sm.on(State::Idle, State::Loading, |_, _| true);
            sm.guard(State::Idle, State::Loading, move |_, _| {
                a2.load(Ordering::SeqCst)
            });
        });

        let emissions2 = Arc::new(AtomicU32::new(0));
        let e2 = emissions2.clone();
        let _guard2 = sm2.subscribe(move |_| {
            e2.fetch_add(1, Ordering::SeqCst);
        });

        source2.set(State::Loading);
        assert_eq!(emissions2.load(Ordering::SeqCst), 2); // Now passes
    }

    #[test]
    fn test_state_transition_on_invalid() {
        let source = Cell::new(State::Idle);
        let invalid_count = Arc::new(AtomicU32::new(0));

        let ic = invalid_count.clone();
        let _sm: Cell<bool, _> = source.state_transition(|sm| {
            sm.on(State::Idle, State::Loading, |_, _| true);
            sm.on_invalid(move |_, _| {
                ic.fetch_add(1, Ordering::SeqCst);
            });
        });

        // Invalid transition
        source.set(State::Ready);
        assert_eq!(invalid_count.load(Ordering::SeqCst), 1);

        // Another invalid
        source.set(State::Error);
        assert_eq!(invalid_count.load(Ordering::SeqCst), 2);
    }

    #[test]
    fn test_state_transition_selective_emit() {
        use crate::{FilterExt, Gettable};

        let source = Cell::new(State::Idle);
        let sm = source.state_transition(|sm| {
            sm.on(State::Idle, State::Loading, |_, _| true);
            sm.on(State::Loading, State::Ready, |_, _| false);
            sm.on(State::Ready, State::Idle, |_, _| false);
        });
        let triggers = sm.filter(|v| *v);

        let emission_count = Arc::new(AtomicU32::new(0));
        let ec = emission_count.clone();
        let _guard = triggers.subscribe(move |_| {
            ec.fetch_add(1, Ordering::SeqCst);
        });

        assert_eq!(emission_count.load(Ordering::SeqCst), 1); // Initial (false)

        source.set(State::Loading); // true - emits
        assert_eq!(emission_count.load(Ordering::SeqCst), 2);

        source.set(State::Ready); // false - filtered
        assert_eq!(emission_count.load(Ordering::SeqCst), 2);

        source.set(State::Idle); // false - filtered
        assert_eq!(emission_count.load(Ordering::SeqCst), 2);

        source.set(State::Loading); // true again - emits
        assert_eq!(emission_count.load(Ordering::SeqCst), 3);
        assert!(triggers.get());
    }
}