Skip to main content

euv_core/reactive/hook/
impl.rs

1use crate::*;
2
3/// Implementation of hook context lifecycle and hook index management.
4impl HookContext {
5    /// Resets the hook index for a new render cycle.
6    ///
7    /// Sets the internal hook index back to zero so that subsequent
8    /// `use_signal` calls start indexing from the beginning of the hook list.
9    pub fn reset_hook_index(&mut self) {
10        if let Ok(mut inner) = self.get_inner().try_borrow_mut() {
11            inner.set_hook_index(0);
12        }
13    }
14
15    /// Notifies the hook context that a match arm is being entered.
16    ///
17    /// If the arm index has changed, all existing hooks and cleanups
18    /// are cleared and re-initialized for the new arm. If the arm
19    /// is unchanged, only the hook index is reset.
20    ///
21    /// # Arguments
22    ///
23    /// - `usize` - The index of the new match arm.
24    pub fn set_arm_changed(&mut self, changed: usize) {
25        let cleanups: Vec<Box<dyn FnOnce()>>;
26        {
27            let Ok(mut inner) = self.get_inner().try_borrow_mut() else {
28                return;
29            };
30            if inner.get_arm_changed() == changed {
31                drop(inner);
32                self.reset_hook_index();
33                return;
34            }
35            cleanups = take(inner.get_mut_cleanups());
36            inner.get_mut_hooks().clear();
37            inner.set_arm_changed(changed);
38        }
39        for cleanup in cleanups {
40            cleanup();
41        }
42        self.reset_hook_index();
43    }
44}
45
46/// Clones the hook context, sharing the same inner state.
47///
48/// All clones share the same underlying `Rc<RefCell<HookContextInner>>`,
49/// so modifications through one clone are visible through all others.
50///
51/// # Returns
52///
53/// - `Self`: A new `HookContext` sharing the same inner state.
54impl Clone for HookContext {
55    fn clone(&self) -> Self {
56        Self::new(self.get_inner().clone())
57    }
58}
59
60/// Provides a default empty hook context.
61///
62/// Creates a fresh `Rc<RefCell<HookContextInner>>` with default values
63/// (empty hook list, zero hook index, empty cleanup list).
64///
65/// # Returns
66///
67/// - `Self`: A new `HookContext` with default inner state.
68impl Default for HookContext {
69    fn default() -> Self {
70        Self::new(Rc::new(RefCell::new(HookContextInner::default())))
71    }
72}
73
74/// Implementation of interval handle lifecycle management.
75impl IntervalHandle {
76    /// Cancels the associated browser interval timer.
77    ///
78    /// Calls `window.clearInterval` with the stored interval ID.
79    /// After calling this method the interval callback will no longer fire.
80    ///
81    /// # Panics
82    ///
83    /// Panics if `window()` is unavailable on the current platform.
84    pub fn clear(&self) {
85        if let Some(cleanup_window) = web_sys::window() {
86            cleanup_window.clear_interval_with_handle(self.get_interval_id());
87        }
88    }
89}
90
91/// Compares interval handles by their interval ID.
92impl PartialEq for IntervalHandle {
93    /// Compares two interval handles for equality by their interval IDs.
94    ///
95    /// # Arguments
96    ///
97    /// - `&Self` - The first interval handle.
98    /// - `&Self` - The second interval handle.
99    ///
100    /// # Returns
101    ///
102    /// - `bool` - `true` if both handles have the same interval ID.
103    fn eq(&self, other: &Self) -> bool {
104        self.get_interval_id() == other.get_interval_id()
105    }
106}