Skip to main content

gpui_hooks/hooks/
use_effect.rs

1use super::{Dependency, Hook};
2use std::any::Any;
3use std::cell::RefCell;
4
5/// UseEffect hook - runs side effects when dependencies change
6pub struct UseEffect {
7    deps: Vec<Box<dyn Dependency>>,
8    cleanup: Option<Box<dyn FnOnce()>>,
9    has_run: bool,
10}
11
12impl UseEffect {
13    pub fn new(deps: Vec<Box<dyn Dependency>>) -> Self {
14        Self {
15            deps,
16            cleanup: None,
17            has_run: false,
18        }
19    }
20
21    /// Check if dependencies have changed
22    pub fn deps_changed(&self, new_deps: &[Box<dyn Dependency>]) -> bool {
23        if !self.has_run {
24            return true;
25        }
26        if self.deps.len() != new_deps.len() {
27            return true;
28        }
29        self.deps
30            .iter()
31            .zip(new_deps.iter())
32            .any(|(old, new)| !old.equals(new.as_ref()))
33    }
34
35    /// Update dependencies
36    pub fn update_deps(&mut self, new_deps: Vec<Box<dyn Dependency>>) {
37        self.deps = new_deps;
38    }
39
40    /// Mark as run
41    pub fn mark_run(&mut self) {
42        self.has_run = true;
43    }
44
45    /// Set cleanup function
46    pub fn set_cleanup(&mut self, cleanup: Box<dyn FnOnce()>) {
47        // Run previous cleanup if exists
48        if let Some(prev_cleanup) = self.cleanup.take() {
49            prev_cleanup();
50        }
51        self.cleanup = Some(cleanup);
52    }
53
54    /// Run cleanup if exists
55    pub fn run_cleanup(&mut self) {
56        if let Some(cleanup) = self.cleanup.take() {
57            cleanup();
58        }
59    }
60}
61
62impl Hook for UseEffect {
63    fn as_any(&self) -> &dyn Any {
64        self
65    }
66    fn as_any_mut(&mut self) -> &mut dyn Any {
67        self
68    }
69}
70
71/// Trait for using effect hooks
72///
73/// This trait is automatically implemented for any type that implements `HasHooks`
74/// (which includes all types using `#[hook_element]`).
75pub trait UseEffectHook {
76    /// Get access to the hooks storage (internal use)
77    fn _hooks_ref(&self) -> &RefCell<Vec<Box<dyn Hook>>>;
78
79    /// Get and increment the hook index (internal use)
80    fn _next_hook_index(&self) -> usize;
81
82    /// Use an effect hook
83    /// Note: deps parameter should be passed directly (array or tuple) to avoid temporary value lifetime issues
84    fn use_effect<F, C, D>(&self, deps: D, effect: F)
85    where
86        F: FnOnce() -> Option<C>,
87        C: FnOnce() + 'static,
88        D: IntoIterator<Item: Dependency + Clone>,
89    {
90        let idx = self._next_hook_index();
91        let hooks_ref = self._hooks_ref();
92
93        // Convert deps to boxed vector
94        let boxed_deps: Vec<Box<dyn Dependency>> = deps
95            .into_iter()
96            .map(|d| Box::new(d.clone()) as Box<dyn Dependency>)
97            .collect();
98
99        // Create or update hook
100        let hooks_len = hooks_ref.borrow().len();
101        if idx >= hooks_len {
102            let mut effect_hook = UseEffect::new(boxed_deps.clone());
103
104            // Run effect immediately
105            if let Some(cleanup) = effect() {
106                effect_hook.set_cleanup(Box::new(cleanup));
107            }
108            effect_hook.mark_run();
109
110            hooks_ref.borrow_mut().push(Box::new(effect_hook));
111        } else {
112            let mut hooks = hooks_ref.borrow_mut();
113            let hook = hooks.get_mut(idx).expect("Hook index out of bounds");
114            let effect_hook = hook
115                .as_any_mut()
116                .downcast_mut::<UseEffect>()
117                .expect("Hook type mismatch at index");
118
119            if effect_hook.deps_changed(&boxed_deps) {
120                // Run cleanup from previous effect
121                effect_hook.run_cleanup();
122
123                // Update deps
124                effect_hook.update_deps(boxed_deps);
125
126                // Run new effect
127                if let Some(cleanup) = effect() {
128                    effect_hook.set_cleanup(Box::new(cleanup));
129                }
130                effect_hook.mark_run();
131            }
132        }
133    }
134
135    /// Clean up all effects (call when component is dropped)
136    fn cleanup_effects(&self) {
137        for hook in self._hooks_ref().borrow_mut().iter_mut() {
138            if let Some(effect) = hook.as_any_mut().downcast_mut::<UseEffect>() {
139                effect.run_cleanup();
140            }
141        }
142    }
143}