repose_core/
effects_ext.rs1use crate::{remember_with_key, scoped_effect};
2use std::cell::RefCell;
3
4pub fn disposable_effect<K: PartialEq + Clone + 'static>(
6 _key: K,
7 effect: impl FnOnce() -> Box<dyn FnOnce()> + 'static,
8) {
9 scoped_effect(|| {
10 let cleanup = effect();
11 cleanup
12 });
13}
14
15pub fn side_effect(effect: impl Fn()) {
17 effect();
18}
19
20pub fn launched_effect_internal<K: PartialEq + Clone + 'static>(
22 callsite: &'static str,
23 key: K,
24 effect: impl FnOnce() + 'static,
25) {
26 let last_key = remember_with_key(format!("launched:{callsite}"), || RefCell::new(None::<K>));
28
29 let mut last = last_key.borrow_mut();
30 if last.as_ref() != Some(&key) {
31 *last = Some(key);
32 effect();
34 }
35}
36
37#[macro_export] macro_rules! launched_effect {
39 ($key:expr, $effect:expr) => {
40 $crate::effects_ext::launched_effect_internal(
41 concat!(module_path!(), ":", line!(), ":", column!()),
42 $key,
43 $effect,
44 )
45 };
46}