Skip to content

Commit c75397e

Browse files
committed
chore: small refactor
1 parent 848fd72 commit c75397e

File tree

2 files changed

+12
-45
lines changed

2 files changed

+12
-45
lines changed

reactive_graph/src/effect/effect.rs

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -374,47 +374,16 @@ impl Effect<SyncStorage> {
374374
/// This spawns a task that can be run on any thread. For an effect that will be spawned on
375375
/// the current thread, use [`new`](Effect::new).
376376
pub fn new_sync<T, M>(
377-
mut fun: impl EffectFunction<T, M> + Send + Sync + 'static,
377+
fun: impl EffectFunction<T, M> + Send + Sync + 'static,
378378
) -> Self
379379
where
380380
T: Send + Sync + 'static,
381381
{
382-
let inner = cfg!(feature = "effects").then(|| {
383-
let (mut rx, owner, inner) = effect_base();
384-
let mut first_run = true;
385-
let value = Arc::new(RwLock::new(None::<T>));
386-
387-
crate::spawn({
388-
let value = Arc::clone(&value);
389-
let subscriber = inner.to_any_subscriber();
390-
391-
async move {
392-
while rx.next().await.is_some() {
393-
if !owner.paused()
394-
&& (subscriber.with_observer(|| {
395-
subscriber.update_if_necessary()
396-
}) || first_run)
397-
{
398-
first_run = false;
399-
subscriber.clear_sources(&subscriber);
400-
401-
let old_value =
402-
mem::take(&mut *value.write().or_poisoned());
403-
let new_value = owner.with_cleanup(|| {
404-
subscriber.with_observer(|| {
405-
run_in_effect_scope(|| fun.run(old_value))
406-
})
407-
});
408-
*value.write().or_poisoned() = Some(new_value);
409-
}
410-
}
411-
}
412-
});
413-
414-
ArenaItem::new_with_storage(Some(inner))
415-
});
382+
if !cfg!(feature = "effects") {
383+
return Self { inner: None };
384+
}
416385

417-
Self { inner }
386+
Self::new_isomorphic(fun)
418387
}
419388

420389
/// Creates a new effect, which runs once on the next “tick”, and then runs again when reactive values

reactive_graph/src/effect/inner.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,19 @@ impl ReactiveNode for RwLock<EffectInner> {
3030

3131
fn update_if_necessary(&self) -> bool {
3232
let mut guard = self.write().or_poisoned();
33-
let (is_dirty, sources) =
34-
(guard.dirty, (!guard.dirty).then(|| guard.sources.clone()));
3533

36-
if is_dirty {
34+
if guard.dirty {
3735
guard.dirty = false;
3836
return true;
3937
}
4038

39+
let sources = guard.sources.clone();
40+
4141
drop(guard);
42-
for source in sources.into_iter().flatten() {
43-
if source.update_if_necessary() {
44-
return true;
45-
}
46-
}
47-
false
42+
43+
sources
44+
.into_iter()
45+
.any(|source| source.update_if_necessary())
4846
}
4947

5048
fn mark_check(&self) {

0 commit comments

Comments
 (0)