chore: small refactor

This commit is contained in:
QuartzLibrary
2025-02-23 17:27:40 +00:00
parent 848fd724dd
commit c75397ea75
2 changed files with 12 additions and 45 deletions

View File

@@ -374,47 +374,16 @@ impl Effect<SyncStorage> {
/// This spawns a task that can be run on any thread. For an effect that will be spawned on /// 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). /// the current thread, use [`new`](Effect::new).
pub fn new_sync<T, M>( pub fn new_sync<T, M>(
mut fun: impl EffectFunction<T, M> + Send + Sync + 'static, fun: impl EffectFunction<T, M> + Send + Sync + 'static,
) -> Self ) -> Self
where where
T: Send + Sync + 'static, T: Send + Sync + 'static,
{ {
let inner = cfg!(feature = "effects").then(|| { if !cfg!(feature = "effects") {
let (mut rx, owner, inner) = effect_base(); return Self { inner: None };
let mut first_run = true; }
let value = Arc::new(RwLock::new(None::<T>));
crate::spawn({ Self::new_isomorphic(fun)
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 }
} }
/// Creates a new effect, which runs once on the next “tick”, and then runs again when reactive values /// Creates a new effect, which runs once on the next “tick”, and then runs again when reactive values

View File

@@ -30,21 +30,19 @@ impl ReactiveNode for RwLock<EffectInner> {
fn update_if_necessary(&self) -> bool { fn update_if_necessary(&self) -> bool {
let mut guard = self.write().or_poisoned(); 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; guard.dirty = false;
return true; return true;
} }
let sources = guard.sources.clone();
drop(guard); drop(guard);
for source in sources.into_iter().flatten() {
if source.update_if_necessary() { sources
return true; .into_iter()
} .any(|source| source.update_if_necessary())
}
false
} }
fn mark_check(&self) { fn mark_check(&self) {