only run effects on client

This commit is contained in:
Greg Johnston 2024-05-15 08:18:22 -04:00
parent d6e19c0a60
commit 1bc0b414e3
4 changed files with 32 additions and 25 deletions

View file

@ -54,12 +54,13 @@ send_wrapper = "0.6.0"
[features]
default = ["serde"]
hydration = ["leptos_server/hydration", "hydration_context/browser"]
csr = ["leptos_macro/csr", "leptos_reactive/csr"]
csr = ["leptos_macro/csr", "leptos_reactive/csr", "reactive_graph/effects"]
hydrate = [
"leptos_macro/hydrate",
"leptos_reactive/hydrate",
"hydration",
"tachys/hydrate",
"reactive_graph/effects"
]
default-tls = ["leptos_server/default-tls", "server_fn/default-tls"]
rustls = ["leptos_server/rustls", "server_fn/rustls"]

View file

@ -36,6 +36,7 @@ nightly = []
serde = ["dep:serde"]
tracing = ["dep:tracing"]
hydration = ["dep:hydration_context"]
effects = [] # whether to run effects: should be disabled for something like server rendering
sandboxed-arenas = []
[package.metadata.docs.rs]

View file

@ -52,9 +52,10 @@ impl Effect {
T: 'static,
{
let (mut rx, owner, inner) = effect_base();
let value = Arc::new(RwLock::new(None));
let value = Arc::new(RwLock::new(None::<T>));
let mut first_run = true;
#[cfg(feature = "effects")]
Executor::spawn_local({
let value = Arc::clone(&value);
let subscriber = inner.to_any_subscriber();
@ -89,8 +90,9 @@ impl Effect {
{
let (mut rx, owner, inner) = effect_base();
let mut first_run = true;
let value = Arc::new(RwLock::new(None));
let value = Arc::new(RwLock::new(None::<T>));
#[cfg(feature = "effects")]
Executor::spawn({
let value = Arc::clone(&value);
let subscriber = inner.to_any_subscriber();

View file

@ -44,39 +44,42 @@ where
initial_value: Option<T>,
) -> Self {
let (observer, mut rx) = channel();
let value = Arc::new(RwLock::new(None));
let value = Arc::new(RwLock::new(None::<T>));
let owner = Owner::new();
let inner = Arc::new(RwLock::new(EffectInner {
observer,
sources: SourceSet::new(),
}));
let initial_value = Some(owner.with(|| {
inner
.to_any_subscriber()
.with_observer(|| fun(initial_value))
}));
*value.write().or_poisoned() = initial_value;
#[cfg(feature = "effects")]
{
let initial_value = Some(owner.with(|| {
inner
.to_any_subscriber()
.with_observer(|| fun(initial_value))
}));
*value.write().or_poisoned() = initial_value;
Executor::spawn_local({
let value = Arc::clone(&value);
let subscriber = inner.to_any_subscriber();
Executor::spawn_local({
let value = Arc::clone(&value);
let subscriber = inner.to_any_subscriber();
async move {
while rx.next().await.is_some() {
if subscriber.update_if_necessary() {
subscriber.clear_sources(&subscriber);
async move {
while rx.next().await.is_some() {
if subscriber.update_if_necessary() {
subscriber.clear_sources(&subscriber);
let old_value =
mem::take(&mut *value.write().or_poisoned());
let new_value = owner.with_cleanup(|| {
subscriber.with_observer(|| fun(old_value))
});
*value.write().or_poisoned() = Some(new_value);
let old_value =
mem::take(&mut *value.write().or_poisoned());
let new_value = owner.with_cleanup(|| {
subscriber.with_observer(|| fun(old_value))
});
*value.write().or_poisoned() = Some(new_value);
}
}
}
}
});
});
}
RenderEffect { value, inner }
}