euv_core/reactive/effect/impl.rs
1use super::*;
2
3/// Lifecycle operations for `Effect`.
4impl Effect {
5 /// Creates and immediately runs a reactive side effect, auto-tracking all signals and memos read.
6 ///
7 /// # Arguments
8 ///
9 /// - `F` - Effect callback executed initially and on every tracked dependency change.
10 ///
11 /// # Returns
12 ///
13 /// - `Self` - Handle to the registered effect.
14 pub fn new<F>(callback: F) -> Self
15 where
16 F: FnMut() + 'static,
17 {
18 let id: EffectId = with_runtime(
19 |rt: &mut ReactiveRuntime| rt.create_effect(callback),
20 EffectId::default(),
21 );
22 run_effect(id);
23 Self { id }
24 }
25
26 /// Returns the underlying `EffectId`.
27 ///
28 /// # Arguments
29 ///
30 /// - `&Self` - Effect handle.
31 ///
32 /// # Returns
33 ///
34 /// - `EffectId` - Underlying arena identifier.
35 #[must_use]
36 pub fn id(&self) -> EffectId {
37 let Self { id } = *self;
38 id
39 }
40
41 /// Returns the number of active upstream dependencies tracked by this effect in O(1).
42 ///
43 /// # Arguments
44 ///
45 /// - `&Self` - Effect handle to inspect.
46 ///
47 /// # Returns
48 ///
49 /// - `usize` - Number of currently linked dependency edges, or `0` if disposed.
50 #[must_use]
51 pub fn dependency_count(&self) -> usize {
52 let id: EffectId = self.id();
53 with_runtime_ref(|rt: &ReactiveRuntime| rt.effect_dependency_count(id), 0)
54 }
55
56 /// Returns an ordered snapshot of `(edge_index, source_tag)` pairs currently linked to this effect.
57 ///
58 /// # Arguments
59 ///
60 /// - `&Self` - Effect handle to inspect.
61 ///
62 /// # Returns
63 ///
64 /// - `Vec<(u32, u32)>` - Active dependency edge indices and packed source tags in traversal order.
65 #[must_use]
66 pub fn dependency_edge_snapshot(&self) -> Vec<(u32, u32)> {
67 let id: EffectId = self.id();
68 with_runtime_ref(
69 |rt: &ReactiveRuntime| rt.effect_edge_snapshot(id),
70 Vec::new(),
71 )
72 }
73
74 /// Disposes this effect so it no longer reacts to upstream changes.
75 ///
76 /// # Arguments
77 ///
78 /// - `&Self` - Effect handle to dispose.
79 pub fn dispose(&self) {
80 let id: EffectId = self.id();
81 with_runtime(
82 |rt: &mut ReactiveRuntime| {
83 rt.dispose_effect(id);
84 },
85 (),
86 );
87 }
88}