rustolio_web/hooks/effect/
mod.rs1mod store;
12
13use std::usize;
14
15use super::{Scope, signal_updater_callback};
16
17pub(super) use store::EffectStore;
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub struct Effect {
22 id: usize,
23}
24
25impl Effect {
26 pub fn new(f: impl Fn() + 'static) -> Self {
27 let id = EffectStore::new();
28 Scope::register_effect(id);
29
30 signal_updater_callback(move || !EffectStore::is_dropped(id), f);
31
32 Self { id }
33 }
34
35 pub fn new_result(f: impl Fn() -> rustolio_utils::Result<()> + 'static) -> Self {
36 Self::new(crate::error::convert_fn_0("Effect", f))
37 }
38
39 pub fn remove(&self) {
40 #[cfg(debug_assertions)]
41 if self.id == usize::MAX {
42 panic!("Cannot drop global effect")
43 }
44
45 EffectStore::drop(self.id);
46 }
47
48 #[doc(hidden)]
55 pub unsafe fn deregister(&mut self) {
56 unsafe {
57 Scope::deregister_effect(self.id)
59 };
60 self.id = usize::MAX;
61 }
62}