|
@@ -1,16 +1,28 @@
|
|
|
use core::{self, fmt::Debug};
|
|
|
-use std::cell::RefCell;
|
|
|
use std::fmt::{self, Formatter};
|
|
|
-use std::rc::Rc;
|
|
|
//
|
|
|
use dioxus_core::prelude::*;
|
|
|
|
|
|
use crate::use_signal;
|
|
|
use crate::{dependency::Dependency, CopyValue};
|
|
|
|
|
|
-#[derive(Default, Clone)]
|
|
|
+#[derive(Copy, Clone, PartialEq)]
|
|
|
pub(crate) struct EffectStack {
|
|
|
- pub(crate) effects: Rc<RefCell<Vec<Effect>>>,
|
|
|
+ pub(crate) effects: CopyValue<Vec<Effect>>,
|
|
|
+}
|
|
|
+
|
|
|
+impl Default for EffectStack {
|
|
|
+ fn default() -> Self {
|
|
|
+ Self {
|
|
|
+ effects: CopyValue::new_in_scope(Vec::new(), ScopeId::ROOT),
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+impl EffectStack {
|
|
|
+ pub(crate) fn current(&self) -> Option<Effect> {
|
|
|
+ self.effects.read().last().copied()
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
pub(crate) fn get_effect_stack() -> EffectStack {
|
|
@@ -57,6 +69,7 @@ pub fn use_effect_with_dependencies<D: Dependency>(
|
|
|
pub struct Effect {
|
|
|
pub(crate) source: ScopeId,
|
|
|
pub(crate) callback: CopyValue<Box<dyn FnMut()>>,
|
|
|
+ pub(crate) effect_stack: EffectStack,
|
|
|
}
|
|
|
|
|
|
impl Debug for Effect {
|
|
@@ -67,7 +80,7 @@ impl Debug for Effect {
|
|
|
|
|
|
impl Effect {
|
|
|
pub(crate) fn current() -> Option<Self> {
|
|
|
- get_effect_stack().effects.borrow().last().copied()
|
|
|
+ get_effect_stack().effects.read().last().copied()
|
|
|
}
|
|
|
|
|
|
/// Create a new effect. The effect will be run immediately and whenever any signal it reads changes.
|
|
@@ -77,6 +90,7 @@ impl Effect {
|
|
|
let myself = Self {
|
|
|
source: current_scope_id().expect("in a virtual dom"),
|
|
|
callback: CopyValue::new(Box::new(callback)),
|
|
|
+ effect_stack: get_effect_stack(),
|
|
|
};
|
|
|
|
|
|
myself.try_run();
|
|
@@ -88,11 +102,11 @@ impl Effect {
|
|
|
pub fn try_run(&self) {
|
|
|
if let Some(mut callback) = self.callback.try_write() {
|
|
|
{
|
|
|
- get_effect_stack().effects.borrow_mut().push(*self);
|
|
|
+ self.effect_stack.effects.write().push(*self);
|
|
|
}
|
|
|
callback();
|
|
|
{
|
|
|
- get_effect_stack().effects.borrow_mut().pop();
|
|
|
+ self.effect_stack.effects.write().pop();
|
|
|
}
|
|
|
}
|
|
|
}
|