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 /// - `CallbackFn` - 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<CallbackFn>(callback: CallbackFn) -> Self
15 where
16 CallbackFn: FnMut() + 'static,
17 {
18 let id: EffectId = with_runtime(
19 |runtime_ref: &mut ReactiveRuntime| runtime_ref.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(
54 |runtime_ref: &ReactiveRuntime| runtime_ref.effect_dependency_count(id),
55 0,
56 )
57 }
58
59 /// Returns an ordered snapshot of `(edge_index, source_tag)` pairs currently linked to this effect.
60 ///
61 /// # Arguments
62 ///
63 /// - `&Self` - Effect handle to inspect.
64 ///
65 /// # Returns
66 ///
67 /// - `Vec<(u32, u32)>` - Active dependency edge indices and packed source tags in traversal order.
68 #[must_use]
69 pub fn dependency_edge_snapshot(&self) -> Vec<(u32, u32)> {
70 let id: EffectId = self.id();
71 with_runtime_ref(
72 |runtime_ref: &ReactiveRuntime| runtime_ref.effect_edge_snapshot(id),
73 Vec::new(),
74 )
75 }
76
77 /// Disposes this effect so it no longer reacts to upstream changes.
78 ///
79 /// # Arguments
80 ///
81 /// - `&Self` - Effect handle to dispose.
82 pub fn dispose(&self) {
83 let id: EffectId = self.id();
84 with_runtime(
85 |runtime_ref: &mut ReactiveRuntime| {
86 runtime_ref.dispose_effect(id);
87 },
88 (),
89 );
90 }
91}