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,

source

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).)

source

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.

source

pub fn loading(&self) -> ReadSignal<bool>

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

source

pub fn refetch(&self)

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

source

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> Clone for Resource<S, T>where S: 'static, T: 'static,

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<S, T> Debug for Resource<S, T>where S: 'static + Debug, T: 'static + Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<S, T> Hash for Resource<S, T>where S: 'static + Hash, T: 'static + Hash,

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<S, T> PartialEq<Resource<S, T>> for Resource<S, T>where S: 'static + PartialEq, T: 'static + PartialEq,

source§

fn eq(&self, other: &Resource<S, T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<S, T> Copy for Resource<S, T>where S: 'static, T: 'static,

source§

impl<S, T> Eq for Resource<S, T>where S: 'static + Eq, T: 'static + Eq,

source§

impl<S, T> StructuralEq for Resource<S, T>where S: 'static, T: 'static,

source§

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> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CallHasher for Twhere T: Hash + ?Sized,

source§

default fn get_hash<H, B>(value: &H, build_hasher: &B) -> u64where H: Hash + ?Sized, B: BuildHasher,

source§

impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

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

fn in_current_span(self) -> Instrumented<Self>

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

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more