Struct leptos::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(), 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(), Some(vec!["2".to_string()]));

Implementations§

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() is equivalent to value.with(T::clone).)

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.

Returns a signal that indicates whether the resource is currently loading.

Re-runs the async function with the current source data.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
🔬This is a nightly-only experimental API. (fn_traits)
Performs the call operation.
🔬This is a nightly-only experimental API. (fn_traits)
Performs the call operation.
The returned type after the call operator is used.
🔬This is a nightly-only experimental API. (fn_traits)
Performs the call operation.
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Converts the object into an [Attribute].
Converts the object into a [Property].
Consumes self, returning a Signal<T>.
Converts the value into View.
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more