use crate::events::EventDispatcher;
use crate::fsm::FsmRuntime;
use crate::reactive::ReactiveGraph;
pub struct BlincReactiveRuntime {
pub reactive: ReactiveGraph,
pub fsm: FsmRuntime,
pub events: EventDispatcher,
}
impl BlincReactiveRuntime {
pub fn new() -> Self {
Self {
reactive: ReactiveGraph::new(),
fsm: FsmRuntime::new(),
events: EventDispatcher::new(),
}
}
pub fn stats(&self) -> RuntimeStats {
RuntimeStats {
reactive: self.reactive.stats(),
fsm_count: self.fsm.len(),
}
}
}
impl Default for BlincReactiveRuntime {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone)]
pub struct RuntimeStats {
pub reactive: crate::reactive::ReactiveStats,
pub fsm_count: usize,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::fsm::StateMachine;
#[test]
fn test_runtime_integration() {
let mut runtime = BlincReactiveRuntime::new();
let count = runtime.reactive.create_signal(0i32);
const IDLE: u32 = 0;
const ACTIVE: u32 = 1;
const CLICK: u32 = 1;
let fsm = runtime.fsm.create(
StateMachine::builder(IDLE)
.on(IDLE, CLICK, ACTIVE)
.on(ACTIVE, CLICK, IDLE)
.build(),
);
assert_eq!(runtime.reactive.get(count), Some(0));
assert_eq!(runtime.fsm.current_state(fsm), Some(IDLE));
runtime.reactive.set(count, 42);
runtime.fsm.send(fsm, CLICK);
assert_eq!(runtime.reactive.get(count), Some(42));
assert_eq!(runtime.fsm.current_state(fsm), Some(ACTIVE));
let stats = runtime.stats();
assert_eq!(stats.reactive.signal_count, 1);
assert_eq!(stats.fsm_count, 1);
}
}