Skip to main content

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    /// Disposes this effect so it no longer reacts to upstream changes.
42    ///
43    /// # Arguments
44    ///
45    /// - `&Self` - Effect handle to dispose.
46    pub fn dispose(&self) {
47        let id: EffectId = self.id();
48        with_runtime(
49            |rt: &mut ReactiveRuntime| {
50                rt.dispose_effect(id);
51            },
52            (),
53        );
54    }
55}