dioxus/packages/signals/src/effect.rs

97 lines
3 KiB
Rust
Raw Normal View History

2023-08-07 12:32:46 -07:00
use core::{self, fmt::Debug};
use std::fmt::{self, Formatter};
use std::marker::PhantomData;
2023-08-07 12:32:46 -07:00
2023-08-04 17:23:57 -07:00
use dioxus_core::prelude::*;
use crate::dependency::Dep;
use crate::{dependency::Dependency, CopyValue};
use crate::{use_signal, Signal};
2023-08-04 17:23:57 -07:00
#[derive(Default, Clone, Copy)]
pub(crate) struct EffectStack {
pub(crate) effects: CopyValue<Vec<Effect>>,
}
pub(crate) fn get_effect_stack() -> EffectStack {
match consume_context() {
Some(rt) => rt,
None => {
let store = EffectStack::default();
provide_root_context(store).expect("in a virtual dom")
}
}
}
2023-08-07 16:56:49 -07:00
/// Create a new effect. The effect will be run immediately and whenever any signal it reads changes.
/// The signal will be owned by the current component and will be dropped when the component is dropped.
pub fn use_effect(cx: &ScopeState, callback: impl FnMut() + 'static) {
cx.use_hook(|| Effect::new(callback));
}
/// Create a new effect. The effect will be run immediately and whenever any signal it reads changes.
/// The signal will be owned by the current component and will be dropped when the component is dropped.
pub fn use_effect_with_dependencies<D: Dependency>(
cx: &ScopeState,
dependencies: D,
mut callback: impl FnMut(D::Out) + 'static,
) where
D::Out: 'static,
{
let dependencies_signal = use_signal(cx, || dependencies.out());
cx.use_hook(|| {
Effect::new(move || {
let deref = &*dependencies_signal.read();
callback(deref.clone());
});
});
let changed = { dependencies.changed(&*dependencies_signal.read()) };
if changed {
dependencies_signal.set(dependencies.out());
}
}
2023-08-07 16:56:49 -07:00
/// Effects allow you to run code when a signal changes. Effects are run immediately and whenever any signal it reads changes.
2023-08-04 17:23:57 -07:00
#[derive(Copy, Clone, PartialEq)]
pub struct Effect {
2023-08-07 12:32:46 -07:00
pub(crate) callback: CopyValue<Box<dyn FnMut()>>,
}
impl Debug for Effect {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_fmt(format_args!("{:?}", self.callback.value))
}
2023-08-04 17:23:57 -07:00
}
impl Effect {
pub(crate) fn current() -> Option<Self> {
get_effect_stack().effects.read().last().copied()
}
2023-08-07 16:56:49 -07:00
/// Create a new effect. The effect will be run immediately and whenever any signal it reads changes.
///
/// The signal will be owned by the current component and will be dropped when the component is dropped.
2023-08-04 17:23:57 -07:00
pub fn new(callback: impl FnMut() + 'static) -> Self {
let myself = Self {
callback: CopyValue::new(Box::new(callback)),
};
myself.try_run();
myself
}
/// Run the effect callback immediately. Returns `true` if the effect was run. Returns `false` is the effect is dead.
pub fn try_run(&self) {
if let Some(mut callback) = self.callback.try_write() {
{
get_effect_stack().effects.write().push(*self);
}
callback();
{
get_effect_stack().effects.write().pop();
}
}
}
}