From c75397ea753fc16cd1a46aa9a1c56b8b5c31a99c Mon Sep 17 00:00:00 2001 From: QuartzLibrary <81446760+QuartzLibrary@users.noreply.github.com> Date: Sun, 23 Feb 2025 17:27:40 +0000 Subject: [PATCH] chore: small refactor --- reactive_graph/src/effect/effect.rs | 41 ++++------------------------- reactive_graph/src/effect/inner.rs | 16 +++++------ 2 files changed, 12 insertions(+), 45 deletions(-) diff --git a/reactive_graph/src/effect/effect.rs b/reactive_graph/src/effect/effect.rs index 24f1bff25..e08e8f30a 100644 --- a/reactive_graph/src/effect/effect.rs +++ b/reactive_graph/src/effect/effect.rs @@ -374,47 +374,16 @@ impl Effect { /// This spawns a task that can be run on any thread. For an effect that will be spawned on /// the current thread, use [`new`](Effect::new). pub fn new_sync( - mut fun: impl EffectFunction + Send + Sync + 'static, + fun: impl EffectFunction + Send + Sync + 'static, ) -> Self where T: Send + Sync + 'static, { - let inner = cfg!(feature = "effects").then(|| { - let (mut rx, owner, inner) = effect_base(); - let mut first_run = true; - let value = Arc::new(RwLock::new(None::)); + if !cfg!(feature = "effects") { + return Self { inner: None }; + } - crate::spawn({ - let value = Arc::clone(&value); - let subscriber = inner.to_any_subscriber(); - - async move { - while rx.next().await.is_some() { - if !owner.paused() - && (subscriber.with_observer(|| { - subscriber.update_if_necessary() - }) || first_run) - { - first_run = false; - subscriber.clear_sources(&subscriber); - - let old_value = - mem::take(&mut *value.write().or_poisoned()); - let new_value = owner.with_cleanup(|| { - subscriber.with_observer(|| { - run_in_effect_scope(|| fun.run(old_value)) - }) - }); - *value.write().or_poisoned() = Some(new_value); - } - } - } - }); - - ArenaItem::new_with_storage(Some(inner)) - }); - - Self { inner } + Self::new_isomorphic(fun) } /// Creates a new effect, which runs once on the next “tick”, and then runs again when reactive values diff --git a/reactive_graph/src/effect/inner.rs b/reactive_graph/src/effect/inner.rs index a781f9c4a..58224f5a2 100644 --- a/reactive_graph/src/effect/inner.rs +++ b/reactive_graph/src/effect/inner.rs @@ -30,21 +30,19 @@ impl ReactiveNode for RwLock { fn update_if_necessary(&self) -> bool { let mut guard = self.write().or_poisoned(); - let (is_dirty, sources) = - (guard.dirty, (!guard.dirty).then(|| guard.sources.clone())); - if is_dirty { + if guard.dirty { guard.dirty = false; return true; } + let sources = guard.sources.clone(); + drop(guard); - for source in sources.into_iter().flatten() { - if source.update_if_necessary() { - return true; - } - } - false + + sources + .into_iter() + .any(|source| source.update_if_necessary()) } fn mark_check(&self) {