-
-
Notifications
You must be signed in to change notification settings - Fork 702
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for effects with custom runtimes #3469
Open
LLBlumire
wants to merge
2
commits into
leptos-rs:main
Choose a base branch
from
LLBlumire:feature/custom-runtimes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,9 @@ use any_spawner::Executor; | |
use futures::StreamExt; | ||
use or_poisoned::OrPoisoned; | ||
use std::{ | ||
future::Future, | ||
mem, | ||
pin::Pin, | ||
sync::{atomic::AtomicBool, Arc, RwLock}, | ||
}; | ||
|
||
|
@@ -196,6 +198,57 @@ impl Effect<LocalStorage> { | |
Self { inner } | ||
} | ||
|
||
/// Creates a new effect, which runs once on the next “tick”, and then runs again when reactive values | ||
/// that are read inside it change. | ||
/// | ||
/// This spawns a task on the local thread using | ||
/// [`spawn_local`](any_spawner::Executor::spawn_local). For an effect that can be spawned on | ||
/// any thread, use [`new_sync`](Effect::new_sync). | ||
pub fn new_with_runtime<T, M>( | ||
mut fun: impl EffectFunction<T, M> + 'static, | ||
pass_to_rt: impl FnOnce(Pin<Box<dyn Future<Output = ()> + 'static>>), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd suggest a name like I think the signature can be something like
if you want to avoid the |
||
) -> Self | ||
where | ||
T: 'static, | ||
{ | ||
let inner = cfg!(feature = "effects").then(|| { | ||
let (mut rx, owner, inner) = effect_base(); | ||
let value = Arc::new(RwLock::new(None::<T>)); | ||
let mut first_run = true; | ||
|
||
let task = Box::pin({ | ||
let value = Arc::clone(&value); | ||
let subscriber = inner.to_any_subscriber(); | ||
|
||
async move { | ||
while rx.next().await.is_some() { | ||
if 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); | ||
} | ||
} | ||
} | ||
}); | ||
pass_to_rt(task); | ||
|
||
ArenaItem::new_with_storage(Some(inner)) | ||
}); | ||
|
||
Self { inner } | ||
} | ||
|
||
/// A version of [`Effect::new`] that only listens to any dependency | ||
/// that is accessed inside `dependency_fn`. | ||
/// | ||
|
@@ -414,6 +467,55 @@ impl Effect<SyncStorage> { | |
Self { inner } | ||
} | ||
|
||
/// See [`Self::new_sync`]. This function additional allows for a custom | ||
/// runtime to be supplied by the caller. | ||
pub fn new_sync_with_runtime<T, M>( | ||
mut fun: impl EffectFunction<T, M> + Send + Sync + 'static, | ||
pass_to_rt: impl FnOnce( | ||
Pin<Box<dyn Future<Output = ()> + 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::<T>)); | ||
|
||
let task = { | ||
let value = Arc::clone(&value); | ||
let subscriber = inner.to_any_subscriber(); | ||
|
||
Box::pin(async move { | ||
while rx.next().await.is_some() { | ||
if 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); | ||
} | ||
} | ||
}) | ||
}; | ||
pass_to_rt(task); | ||
|
||
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 | ||
/// that are read inside it change. | ||
/// | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docs should probably be updated to
new_sync
x 2 here tonew_sync_with_runtime