leptos::server

Struct Resource

Source
pub struct Resource<T, Ser = JsonSerdeCodec>
where T: Send + Sync + 'static,
{ /* private fields */ }
Expand description

An asynchronous resource.

Resources allow asynchronously loading data and serializing it from the server to the client, so that it loads on the server, and is then deserialized on the client. This improves performance by beginning data loading on the server when the request is made, rather than beginning it on the client after WASM has been loaded.

You can access the value of the resource either synchronously using .get() or asynchronously using .await.

Implementations§

Source§

impl<T> Resource<T, FromToStringCodec>

Source

pub fn new_str<S, Fut>( source: impl Fn() -> S + Send + Sync + 'static, fetcher: impl Fn(S) -> Fut + Send + Sync + 'static, ) -> Resource<T, FromToStringCodec>
where S: PartialEq + Clone + Send + Sync + 'static, T: Send + Sync + 'static, Fut: Future<Output = T> + Send + 'static,

Creates a new resource with the encoding FromToStringCodec.

This takes a source function and a fetcher. The resource memoizes and reactively tracks the value returned by source. Whenever that value changes, it will run the fetcher to generate a new Future to load data.

On creation, if you are on the server, this will run the fetcher once to generate a Future whose value will be serialized from the server to the client. If you are on the client, the initial value will be deserialized without re-running that async task.

Source

pub fn new_str_blocking<S, Fut>( source: impl Fn() -> S + Send + Sync + 'static, fetcher: impl Fn(S) -> Fut + Send + Sync + 'static, ) -> Resource<T, FromToStringCodec>
where S: PartialEq + Clone + Send + Sync + 'static, T: Send + Sync + 'static, Fut: Future<Output = T> + Send + 'static,

Creates a new blocking resource with the encoding FromToStringCodec.

This takes a source function and a fetcher. The resource memoizes and reactively tracks the value returned by source. Whenever that value changes, it will run the fetcher to generate a new Future to load data.

On creation, if you are on the server, this will run the fetcher once to generate a Future whose value will be serialized from the server to the client. If you are on the client, the initial value will be deserialized without re-running that async task.

Blocking resources prevent any of the HTTP response from being sent until they have loaded. This is useful if you need their data to set HTML document metadata or information that needs to appear in HTTP headers.

Source§

impl<T> Resource<T>

Source

pub fn new<S, Fut>( source: impl Fn() -> S + Send + Sync + 'static, fetcher: impl Fn(S) -> Fut + Send + Sync + 'static, ) -> Resource<T>
where S: PartialEq + Clone + Send + Sync + 'static, T: Send + Sync + 'static, Fut: Future<Output = T> + Send + 'static,

Creates a new resource with the encoding JsonSerdeCodec.

This takes a source function and a fetcher. The resource memoizes and reactively tracks the value returned by source. Whenever that value changes, it will run the fetcher to generate a new Future to load data.

On creation, if you are on the server, this will run the fetcher once to generate a Future whose value will be serialized from the server to the client. If you are on the client, the initial value will be deserialized without re-running that async task.

Source

pub fn new_blocking<S, Fut>( source: impl Fn() -> S + Send + Sync + 'static, fetcher: impl Fn(S) -> Fut + Send + Sync + 'static, ) -> Resource<T>
where S: PartialEq + Clone + Send + Sync + 'static, T: Send + Sync + 'static, Fut: Future<Output = T> + Send + 'static,

Creates a new blocking resource with the encoding JsonSerdeCodec.

This takes a source function and a fetcher. The resource memoizes and reactively tracks the value returned by source. Whenever that value changes, it will run the fetcher to generate a new Future to load data.

On creation, if you are on the server, this will run the fetcher once to generate a Future whose value will be serialized from the server to the client. If you are on the client, the initial value will be deserialized without re-running that async task.

Blocking resources prevent any of the HTTP response from being sent until they have loaded. This is useful if you need their data to set HTML document metadata or information that needs to appear in HTTP headers.

Source§

impl<T, Ser> Resource<T, Ser>
where Ser: Encoder<T> + Decoder<T>, <Ser as Encoder<T>>::Error: Debug, <Ser as Decoder<T>>::Error: Debug, <<Ser as Decoder<T>>::Encoded as FromEncodedStr>::DecodingError: Debug, <Ser as Encoder<T>>::Encoded: IntoEncodedString, <Ser as Decoder<T>>::Encoded: FromEncodedStr, T: Send + Sync,

Source

pub fn new_with_options<S, Fut>( source: impl Fn() -> S + Send + Sync + 'static, fetcher: impl Fn(S) -> Fut + Send + Sync + 'static, blocking: bool, ) -> Resource<T, Ser>
where S: Send + Sync + Clone + PartialEq + 'static, T: Send + Sync + 'static, Fut: Future<Output = T> + Send + 'static,

Creates a new resource with the encoding Ser.

This takes a source function and a fetcher. The resource memoizes and reactively tracks the value returned by source. Whenever that value changes, it will run the fetcher to generate a new Future to load data.

On creation, if you are on the server, this will run the fetcher once to generate a Future whose value will be serialized from the server to the client. If you are on the client, the initial value will be deserialized without re-running that async task.

If blocking is true, this is a blocking resource.

Blocking resources prevent any of the HTTP response from being sent until they have loaded. This is useful if you need their data to set HTML document metadata or information that needs to appear in HTTP headers.

Source

pub fn map<U>(&self, f: impl FnOnce(&T) -> U) -> Option<U>

Synchronously, reactively reads the current value of the resource and applies the function f to its value if it is Some(_).

Source

pub fn refetch(&self)

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

Source§

impl<T, E, Ser> Resource<Result<T, E>, Ser>
where Ser: Encoder<Result<T, E>> + Decoder<Result<T, E>>, <Ser as Encoder<Result<T, E>>>::Error: Debug, <Ser as Decoder<Result<T, E>>>::Error: Debug, <<Ser as Decoder<Result<T, E>>>::Encoded as FromEncodedStr>::DecodingError: Debug, <Ser as Encoder<Result<T, E>>>::Encoded: IntoEncodedString, <Ser as Decoder<Result<T, E>>>::Encoded: FromEncodedStr, T: Send + Sync, E: Send + Sync + Clone,

Source

pub fn and_then<U>(&self, f: impl FnOnce(&T) -> U) -> Option<Result<U, E>>

Applies the given function when a resource that returns Result<T, E> has resolved and loaded an Ok(_), rather than requiring nested .map() calls over the Option<Result<_, _>> returned by the resource.

This is useful when used with features like server functions, in conjunction with <ErrorBoundary/> and <Suspense/>, when these other components are left to handle the None and Err(_) states.

Source§

impl<T, Ser> Resource<T, Ser>
where T: Send + Sync + 'static,

Source

pub fn by_ref(&self) -> AsyncDerivedRefFuture<T>

Returns a new Future that is ready when the resource has loaded, and accesses its inner value by reference.

Methods from Deref<Target = AsyncDerived<T>>§

Source

pub fn ready(&self) -> AsyncDerivedReadyFuture

Returns a Future that is ready when this resource has next finished loading.

Source

pub fn by_ref(&self) -> AsyncDerivedRefFuture<T>

Returns a Future that resolves when the computation is finished, and accesses the inner value by reference rather than by cloning it.

Trait Implementations§

Source§

impl<T, Ser> AddAnyAttr for Resource<T, Ser>
where T: RenderHtml + Send + Sync + Clone, Ser: Send + 'static,

Source§

type Output<SomeNewAttr: Attribute> = Box<dyn FnMut() -> Suspend<<T as AddAnyAttr>::Output<<<SomeNewAttr as Attribute>::CloneableOwned as Attribute>::CloneableOwned>> + Send>

The new type once the attribute has been added.
Source§

fn add_any_attr<NewAttr>( self, attr: NewAttr, ) -> <Resource<T, Ser> as AddAnyAttr>::Output<NewAttr>
where NewAttr: Attribute, <Resource<T, Ser> as AddAnyAttr>::Output<NewAttr>: RenderHtml,

Adds an attribute to the view.
Source§

impl<T, Ser> Clone for Resource<T, Ser>
where T: Send + Sync + 'static,

Source§

fn clone(&self) -> Resource<T, Ser>

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<T, Ser> Debug for Resource<T, Ser>
where T: Send + Sync + 'static,

Source§

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

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

impl<T, Ser> DefinedAt for Resource<T, Ser>
where T: Send + Sync + 'static,

Source§

fn defined_at(&self) -> Option<&'static Location<'static>>

Returns the location at which the signal was defined. This is usually simply None in release mode.
Source§

impl<T, Ser> Deref for Resource<T, Ser>
where T: Send + Sync + 'static,

Source§

type Target = AsyncDerived<T>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &<Resource<T, Ser> as Deref>::Target

Dereferences the value.
Source§

impl<T, Ser> From<ArcResource<T, Ser>> for Resource<T, Ser>
where T: Send + Sync,

Source§

fn from(arc_resource: ArcResource<T, Ser>) -> Resource<T, Ser>

Converts to this type from the input type.
Source§

impl<T, Ser> From<Resource<T, Ser>> for ArcResource<T, Ser>
where T: Send + Sync,

Source§

fn from(resource: Resource<T, Ser>) -> ArcResource<T, Ser>

Converts to this type from the input type.
Source§

impl<T, Ser> IntoFuture for Resource<T, Ser>
where T: Clone + Send + Sync + 'static,

Source§

type Output = T

The output that the future will produce on completion.
Source§

type IntoFuture = AsyncDerivedFuture<T>

Which kind of future are we turning this into?
Source§

fn into_future(self) -> <Resource<T, Ser> as IntoFuture>::IntoFuture

Creates a future from a value. Read more
Source§

impl<T, Ser> ReadUntracked for Resource<T, Ser>
where T: Send + Sync + 'static,

Source§

type Value = <AsyncDerived<T> as ReadUntracked>::Value

The guard type that will be returned, which can be dereferenced to the value.
Source§

fn try_read_untracked( &self, ) -> Option<<Resource<T, Ser> as ReadUntracked>::Value>

Returns the guard, or None if the signal has already been disposed.
Source§

fn read_untracked(&self) -> Self::Value

Returns the guard. Read more
Source§

fn custom_try_read(&self) -> Option<Option<Self::Value>>

This is a backdoor to allow overriding the Read::try_read implementation despite it being auto implemented. Read more
Source§

impl<T, Ser> Render for Resource<T, Ser>
where T: Render + Send + Sync + Clone, Ser: Send + 'static,

Source§

type State = RenderEffectState<SuspendState<T>>

The “view state” for this type, which can be retained between updates. Read more
Source§

fn build(self) -> <Resource<T, Ser> as Render>::State

Creates the view for the first time, without hydrating from existing HTML.
Source§

fn rebuild(self, state: &mut <Resource<T, Ser> as Render>::State)

Updates the view with new data.
Source§

impl<T, Ser> RenderHtml for Resource<T, Ser>
where T: RenderHtml + Send + Sync + Clone, Ser: Send + 'static,

Source§

const MIN_LENGTH: usize = 0usize

The minimum length of HTML created when this view is rendered.
Source§

type AsyncOutput = Option<T>

The type of the view after waiting for all asynchronous data to load.
Source§

fn dry_resolve(&mut self)

“Runs” the view without other side effects. For primitive types, this is a no-op. For reactive types, this can be used to gather data about reactivity or about asynchronous data that needs to be loaded.
Source§

fn resolve( self, ) -> impl Future<Output = <Resource<T, Ser> as RenderHtml>::AsyncOutput> + Send

Waits for any asynchronous sections of the view to load and returns the output.
Source§

fn to_html_with_buf( self, buf: &mut String, position: &mut Position, escape: bool, mark_branches: bool, )

Renders a view to HTML, writing it into the given buffer.
Source§

fn to_html_async_with_buf<const OUT_OF_ORDER: bool>( self, buf: &mut StreamBuilder, position: &mut Position, escape: bool, mark_branches: bool, )
where Resource<T, Ser>: Sized,

Renders a view into a buffer of (synchronous or asynchronous) HTML chunks.
Source§

fn hydrate<const FROM_SERVER: bool>( self, cursor: &Cursor, position: &PositionState, ) -> <Resource<T, Ser> as Render>::State

Makes a set of DOM nodes rendered from HTML interactive. Read more
Source§

const EXISTS: bool = true

Whether this should actually exist in the DOM, if it is the child of an element.
Source§

fn html_len(&self) -> usize

An estimated length for this view, when rendered to HTML. Read more
Source§

fn to_html(self) -> String
where Self: Sized,

Renders a view to an HTML string.
Source§

fn to_html_branching(self) -> String
where Self: Sized,

Renders a view to HTML with branch markers. This can be used to support libraries that diff HTML pages against one another, by marking sections of the view that branch to different types with marker comments.
Source§

fn to_html_stream_in_order(self) -> StreamBuilder
where Self: Sized,

Renders a view to an in-order stream of HTML.
Source§

fn to_html_stream_in_order_branching(self) -> StreamBuilder
where Self: Sized,

Renders a view to an in-order stream of HTML with branch markers. This can be used to support libraries that diff HTML pages against one another, by marking sections of the view that branch to different types with marker comments.
Source§

fn to_html_stream_out_of_order(self) -> StreamBuilder
where Self: Sized,

Renders a view to an out-of-order stream of HTML.
Source§

fn to_html_stream_out_of_order_branching(self) -> StreamBuilder
where Self: Sized,

Renders a view to an out-of-order stream of HTML with branch markers. This can be used to support libraries that diff HTML pages against one another, by marking sections of the view that branch to different types with marker comments.
Source§

fn hydrate_from<const FROM_SERVER: bool>(self, el: &Element) -> Self::State
where Self: Sized,

Hydrates using RenderHtml::hydrate, beginning at the given element.
Source§

fn hydrate_from_position<const FROM_SERVER: bool>( self, el: &Element, position: Position, ) -> Self::State
where Self: Sized,

Hydrates using RenderHtml::hydrate, beginning at the given element and position.
Source§

impl<T, Ser> Track for Resource<T, Ser>
where T: Send + Sync + 'static,

Source§

fn track(&self)

Subscribes to this signal in the current reactive scope without doing anything with its value.
Source§

impl<T, Ser> Copy for Resource<T, Ser>
where T: Send + Sync + 'static,

Auto Trait Implementations§

§

impl<T, Ser> Freeze for Resource<T, Ser>

§

impl<T, Ser> RefUnwindSafe for Resource<T, Ser>
where Ser: RefUnwindSafe,

§

impl<T, Ser> Send for Resource<T, Ser>
where Ser: Send,

§

impl<T, Ser> Sync for Resource<T, Ser>
where Ser: Sync,

§

impl<T, Ser> Unpin for Resource<T, Ser>
where Ser: Unpin,

§

impl<T, Ser> UnwindSafe for Resource<T, Ser>
where Ser: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<V, Key, Sig, T> BindAttribute<Key, Sig, T> for V
where V: AddAnyAttr, Key: AttributeKey, Sig: IntoSplitSignal<Value = T>, T: FromEventTarget + AttributeValue + PartialEq + Sync + 'static, Signal<BoolOrT<T>>: IntoProperty, <Sig as IntoSplitSignal>::Read: Get<Value = T> + Send + Sync + Clone + 'static, <Sig as IntoSplitSignal>::Write: Send + Clone + 'static, Element: GetValue<T>,

Source§

type Output = <V as AddAnyAttr>::Output<Bind<Key, T, <Sig as IntoSplitSignal>::Read, <Sig as IntoSplitSignal>::Write>>

The type of the element with the two-way binding added.
Source§

fn bind( self, key: Key, signal: Sig, ) -> <V as BindAttribute<Key, Sig, T>>::Output

Adds a two-way binding to the element, which adds an attribute and an event listener to the element when the element is created or hydrated. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T, K, V> CustomAttribute<K, V> for T

Source§

fn attr(self, key: K, value: V) -> Self::Output<CustomAttr<K, V>>

Adds an HTML attribute by key and value.
Source§

impl<V, T, P, D> DirectiveAttribute<T, P, D> for V
where V: AddAnyAttr, D: IntoDirective<T, P>, P: Clone + 'static, T: 'static,

Source§

type Output = <V as AddAnyAttr>::Output<Directive<T, D, P>>

The type of the element with the directive added.
Source§

fn directive( self, handler: D, param: P, ) -> <V as DirectiveAttribute<T, P, D>>::Output

Adds a directive to the element, which runs some custom logic in the browser when the element is created or hydrated.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where 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> IntoAny for T
where T: Send + RenderHtml + 'static, <T as Render>::State: 'static,

Source§

fn into_any(self) -> AnyView

Converts the view into a type-erased AnyView.
Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoRender for T
where T: Render,

Source§

type Output = T

The renderable type into which this type can be converted.
Source§

fn into_render(self) -> <T as IntoRender>::Output

Consumes this value, transforming it into the renderable type.
Source§

impl<T> IntoView for T
where T: Render + RenderHtml + Send,

Source§

fn into_view(self) -> View<T>

Wraps the inner type.
Source§

impl<T> Read for T
where T: Track + ReadUntracked,

Source§

type Value = <T as ReadUntracked>::Value

The guard type that will be returned, which can be dereferenced to the value.
Source§

fn try_read(&self) -> Option<<T as Read>::Value>

Subscribes to the signal, and returns the guard, or None if the signal has already been disposed.
Source§

fn read(&self) -> Self::Value

Subscribes to the signal, and returns the guard. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> StorageAccess<T> for T

Source§

fn as_borrowed(&self) -> &T

Borrows the value.
Source§

fn into_taken(self) -> T

Takes the value.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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 T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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> With for T
where T: Read,

Source§

type Value = <<T as Read>::Value as Deref>::Target

The type of the value contained in the signal.
Source§

fn try_with<U>(&self, fun: impl FnOnce(&<T as With>::Value) -> U) -> Option<U>

Subscribes to the signal, applies the closure to the value, and returns the result, or None if the signal has already been disposed.
Source§

fn with<U>(&self, fun: impl FnOnce(&Self::Value) -> U) -> U

Subscribes to the signal, applies the closure to the value, and returns the result. Read more
Source§

impl<T> WithUntracked for T

Source§

type Value = <<T as ReadUntracked>::Value as Deref>::Target

The type of the value contained in the signal.
Source§

fn try_with_untracked<U>( &self, fun: impl FnOnce(&<T as WithUntracked>::Value) -> U, ) -> Option<U>

Applies the closure to the value, and returns the result, or None if the signal has already been disposed.
Source§

fn with_untracked<U>(&self, fun: impl FnOnce(&Self::Value) -> U) -> U

Applies the closure to the value, and returns the result. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T