use super::SignalId;
use std::collections::HashSet;
pub struct ReactiveRuntime {
dirty: HashSet<SignalId>,
pending_effects: Vec<Box<dyn Fn()>>,
}
impl ReactiveRuntime {
pub fn new() -> Self {
Self {
dirty: HashSet::new(),
pending_effects: Vec::new(),
}
}
pub fn mark_dirty(&mut self, id: SignalId) {
self.dirty.insert(id);
}
pub fn schedule_effect(&mut self, f: Box<dyn Fn()>) {
self.pending_effects.push(f);
}
pub fn flush(&mut self) {
self.dirty.clear();
let effects = std::mem::take(&mut self.pending_effects);
for effect in effects {
effect();
}
}
pub fn has_pending(&self) -> bool {
!self.dirty.is_empty() || !self.pending_effects.is_empty()
}
}
impl Default for ReactiveRuntime {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::Arc;
#[test]
fn test_mark_dirty_duplicate() {
let mut runtime = ReactiveRuntime::new();
runtime.mark_dirty(SignalId(1));
runtime.mark_dirty(SignalId(1)); assert!(runtime.has_pending());
assert_eq!(runtime.dirty.len(), 1); }
#[test]
fn test_flush_clears_effects() {
let mut runtime = ReactiveRuntime::new();
runtime.schedule_effect(Box::new(|| {}));
assert!(runtime.has_pending());
runtime.flush();
assert!(!runtime.has_pending());
assert!(runtime.pending_effects.is_empty()); }
#[test]
fn test_reactive_runtime_new() {
let runtime = ReactiveRuntime::new();
assert!(!runtime.has_pending());
}
#[test]
fn test_reactive_runtime_default() {
let runtime = ReactiveRuntime::default();
assert!(!runtime.has_pending());
}
#[test]
fn test_reactive_runtime_mark_dirty() {
let mut runtime = ReactiveRuntime::new();
runtime.mark_dirty(SignalId(1));
assert!(runtime.has_pending());
}
#[test]
fn test_reactive_runtime_mark_dirty_multiple() {
let mut runtime = ReactiveRuntime::new();
runtime.mark_dirty(SignalId(1));
runtime.mark_dirty(SignalId(2));
runtime.mark_dirty(SignalId(3));
assert!(runtime.has_pending());
}
#[test]
fn test_reactive_runtime_schedule_effect() {
let mut runtime = ReactiveRuntime::new();
runtime.schedule_effect(Box::new(|| {}));
assert!(runtime.has_pending());
}
#[test]
fn test_reactive_runtime_schedule_effect_multiple() {
let mut runtime = ReactiveRuntime::new();
runtime.schedule_effect(Box::new(|| {}));
runtime.schedule_effect(Box::new(|| {}));
runtime.schedule_effect(Box::new(|| {}));
assert!(runtime.has_pending());
}
#[test]
fn test_reactive_runtime_flush_executes_effects() {
let executed = Arc::new(AtomicBool::new(false));
let executed_clone = executed.clone();
let mut runtime = ReactiveRuntime::new();
runtime.schedule_effect(Box::new(move || {
executed_clone.store(true, Ordering::SeqCst);
}));
assert!(!executed.load(Ordering::SeqCst));
runtime.flush();
assert!(executed.load(Ordering::SeqCst));
}
#[test]
fn test_reactive_runtime_flush_multiple_effects() {
let count = Arc::new(AtomicUsize::new(0));
let mut runtime = ReactiveRuntime::new();
for _ in 0..5 {
let count_clone = count.clone();
runtime.schedule_effect(Box::new(move || {
count_clone.fetch_add(1, Ordering::SeqCst);
}));
}
runtime.flush();
assert_eq!(count.load(Ordering::SeqCst), 5);
}
#[test]
fn test_reactive_runtime_flush_clears_pending() {
let mut runtime = ReactiveRuntime::new();
runtime.mark_dirty(SignalId(1));
runtime.schedule_effect(Box::new(|| {}));
assert!(runtime.has_pending());
runtime.flush();
assert!(!runtime.has_pending());
}
#[test]
fn test_reactive_runtime_has_pending_initially_false() {
let runtime = ReactiveRuntime::new();
assert!(!runtime.has_pending());
}
#[test]
fn test_reactive_runtime_has_pending_after_mark_dirty() {
let mut runtime = ReactiveRuntime::new();
runtime.mark_dirty(SignalId(1));
assert!(runtime.has_pending());
}
#[test]
fn test_reactive_runtime_has_pending_after_schedule_effect() {
let mut runtime = ReactiveRuntime::new();
runtime.schedule_effect(Box::new(|| {}));
assert!(runtime.has_pending());
}
#[test]
fn test_reactive_runtime_flush_when_empty() {
let mut runtime = ReactiveRuntime::new();
runtime.flush(); assert!(!runtime.has_pending());
}
#[test]
fn test_reactive_runtime_flush_idempotent() {
let mut runtime = ReactiveRuntime::new();
runtime.schedule_effect(Box::new(|| {}));
runtime.flush();
runtime.flush(); }
}