Struct leptos_reactive::Resource
source · pub struct Resource<S, T>where
S: 'static,
T: 'static,{ /* private fields */ }
Expand description
A signal that reflects the
current state of an asynchronous task, allowing you to integrate async
Futures into the synchronous reactive system.
Takes a fetcher
function that generates a Future when called and a
source
signal that provides the argument for the fetcher
. Whenever the
value of the source
changes, a new Future will be created and run.
When server-side rendering is used, the server will handle running the Future and will stream the result to the client. This process requires the output type of the Future to be Serializable. If your output cannot be serialized, or you just want to make sure the Future runs locally, use create_local_resource().
// any old async function; maybe this is calling a REST API or something
async fn fetch_cat_picture_urls(how_many: i32) -> Vec<String> {
// pretend we're fetching cat pics
vec![how_many.to_string()]
}
// a signal that controls how many cat pics we want
let (how_many_cats, set_how_many_cats) = create_signal(cx, 1);
// create a resource that will refetch whenever `how_many_cats` changes
let cats = create_resource(cx, how_many_cats, fetch_cat_picture_urls);
// when we read the signal, it contains either
// 1) None (if the Future isn't ready yet) or
// 2) Some(T) (if the future's already resolved)
assert_eq!(cats.read(cx), Some(vec!["1".to_string()]));
// when the signal's value changes, the `Resource` will generate and run a new `Future`
set_how_many_cats(2);
assert_eq!(cats.read(cx), Some(vec!["2".to_string()]));
Implementations§
source§impl<S, T> Resource<S, T>where
S: Clone + 'static,
T: 'static,
impl<S, T> Resource<S, T>where S: Clone + 'static, T: 'static,
sourcepub fn read(&self, cx: Scope) -> Option<T>where
T: Clone,
pub fn read(&self, cx: Scope) -> Option<T>where T: Clone,
Clones and returns the current value of the resource (Option::None if the resource is still pending). Also subscribes the running effect to this resource.
If you want to get the value without cloning it, use Resource::with.
(value.read(cx)
is equivalent to value.with(cx, T::clone)
.)
sourcepub fn with<U>(&self, cx: Scope, f: impl FnOnce(&T) -> U) -> Option<U>
pub fn with<U>(&self, cx: Scope, f: impl FnOnce(&T) -> U) -> Option<U>
Applies a function to the current value of the resource, and subscribes the running effect to this resource. If the resource hasn’t yet resolved, the function won’t be called and this will return Option::None.
If you want to get the value by cloning it, you can use Resource::read.
sourcepub fn loading(&self) -> ReadSignal<bool>
pub fn loading(&self) -> ReadSignal<bool>
Returns a signal that indicates whether the resource is currently loading.
sourcepub async fn to_serialization_resolver(&self, cx: Scope) -> (ResourceId, String)where
T: Serializable,
pub async fn to_serialization_resolver(&self, cx: Scope) -> (ResourceId, String)where T: Serializable,
Returns a std::future::Future that will resolve when the resource has loaded, yield its ResourceId and a JSON string.
Trait Implementations§
source§impl<S, T> PartialEq<Resource<S, T>> for Resource<S, T>where
S: 'static + PartialEq,
T: 'static + PartialEq,
impl<S, T> PartialEq<Resource<S, T>> for Resource<S, T>where S: 'static + PartialEq, T: 'static + PartialEq,
impl<S, T> Copy for Resource<S, T>where S: 'static, T: 'static,
impl<S, T> Eq for Resource<S, T>where S: 'static + Eq, T: 'static + Eq,
impl<S, T> StructuralEq for Resource<S, T>where S: 'static, T: 'static,
impl<S, T> StructuralPartialEq for Resource<S, T>where S: 'static, T: 'static,
Auto Trait Implementations§
impl<S, T> RefUnwindSafe for Resource<S, T>where S: RefUnwindSafe, T: RefUnwindSafe,
impl<S, T> Send for Resource<S, T>where S: Send, T: Send,
impl<S, T> Sync for Resource<S, T>where S: Sync, T: Sync,
impl<S, T> Unpin for Resource<S, T>where S: Unpin, T: Unpin,
impl<S, T> UnwindSafe for Resource<S, T>where S: UnwindSafe, T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CallHasher for Twhere
T: Hash + ?Sized,
impl<T> CallHasher for Twhere T: Hash + ?Sized,
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.