rue_core/reactive/effect.rs
1use crate::reactive::context;
2
3/// An effect (like Vue 3's `watchEffect()`).
4/// Runs a closure and automatically tracks any signals read inside it.
5/// Re-runs whenever a tracked signal changes.
6pub struct Effect {
7 effect_id: usize,
8}
9
10impl Effect {
11 /// Create a new effect from the given closure.
12 pub fn new<F: Fn() + Clone + 'static>(f: F) -> Self {
13 let effect_id = context::run_effect(Box::new(f.clone()));
14 // Run immediately to establish dependencies
15 context::track_effect(effect_id, f);
16 Effect { effect_id }
17 }
18
19 /// Get the effect ID.
20 pub fn id(&self) -> usize {
21 self.effect_id
22 }
23}
24
25/// Create a new effect (convenience function).
26pub fn effect<F: Fn() + Clone + 'static>(f: F) -> Effect {
27 Effect::new(f)
28}