pub struct Resource<T, Ser = JsonSerdeCodec>{ /* 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>where
FromToStringCodec: Encoder<T> + Decoder<T>,
<FromToStringCodec as Encoder<T>>::Error: Debug,
<FromToStringCodec as Decoder<T>>::Error: Debug,
<<FromToStringCodec as Decoder<T>>::Encoded as FromEncodedStr>::DecodingError: Debug,
<FromToStringCodec as Encoder<T>>::Encoded: IntoEncodedString,
<FromToStringCodec as Decoder<T>>::Encoded: FromEncodedStr,
T: Send + Sync,
impl<T> Resource<T, FromToStringCodec>where
FromToStringCodec: Encoder<T> + Decoder<T>,
<FromToStringCodec as Encoder<T>>::Error: Debug,
<FromToStringCodec as Decoder<T>>::Error: Debug,
<<FromToStringCodec as Decoder<T>>::Encoded as FromEncodedStr>::DecodingError: Debug,
<FromToStringCodec as Encoder<T>>::Encoded: IntoEncodedString,
<FromToStringCodec as Decoder<T>>::Encoded: FromEncodedStr,
T: Send + Sync,
Sourcepub fn new_str<S, Fut>(
source: impl Fn() -> S + Send + Sync + 'static,
fetcher: impl Fn(S) -> Fut + Send + Sync + 'static,
) -> Resource<T, FromToStringCodec>
pub fn new_str<S, Fut>( source: impl Fn() -> S + Send + Sync + 'static, fetcher: impl Fn(S) -> Fut + Send + Sync + 'static, ) -> Resource<T, FromToStringCodec>
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.
Sourcepub fn new_str_blocking<S, Fut>(
source: impl Fn() -> S + Send + Sync + 'static,
fetcher: impl Fn(S) -> Fut + Send + Sync + 'static,
) -> Resource<T, FromToStringCodec>
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>
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>where
JsonSerdeCodec: Encoder<T> + Decoder<T>,
<JsonSerdeCodec as Encoder<T>>::Error: Debug,
<JsonSerdeCodec as Decoder<T>>::Error: Debug,
<<JsonSerdeCodec as Decoder<T>>::Encoded as FromEncodedStr>::DecodingError: Debug,
<JsonSerdeCodec as Encoder<T>>::Encoded: IntoEncodedString,
<JsonSerdeCodec as Decoder<T>>::Encoded: FromEncodedStr,
T: Send + Sync,
impl<T> Resource<T>where
JsonSerdeCodec: Encoder<T> + Decoder<T>,
<JsonSerdeCodec as Encoder<T>>::Error: Debug,
<JsonSerdeCodec as Decoder<T>>::Error: Debug,
<<JsonSerdeCodec as Decoder<T>>::Encoded as FromEncodedStr>::DecodingError: Debug,
<JsonSerdeCodec as Encoder<T>>::Encoded: IntoEncodedString,
<JsonSerdeCodec as Decoder<T>>::Encoded: FromEncodedStr,
T: Send + Sync,
Sourcepub fn new<S, Fut>(
source: impl Fn() -> S + Send + Sync + 'static,
fetcher: impl Fn(S) -> Fut + Send + Sync + 'static,
) -> Resource<T>
pub fn new<S, Fut>( source: impl Fn() -> S + Send + Sync + 'static, fetcher: impl Fn(S) -> Fut + Send + Sync + 'static, ) -> Resource<T>
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.
Sourcepub fn new_blocking<S, Fut>(
source: impl Fn() -> S + Send + Sync + 'static,
fetcher: impl Fn(S) -> Fut + Send + Sync + 'static,
) -> Resource<T>
pub fn new_blocking<S, Fut>( source: impl Fn() -> S + Send + Sync + 'static, fetcher: impl Fn(S) -> Fut + Send + Sync + 'static, ) -> Resource<T>
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>
impl<T, Ser> Resource<T, Ser>
Sourcepub 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>
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>
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§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,
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,
Sourcepub fn and_then<U>(&self, f: impl FnOnce(&T) -> U) -> Option<Result<U, E>>
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.
Methods from Deref<Target = AsyncDerived<T>>§
Sourcepub fn ready(&self) -> AsyncDerivedReadyFuture ⓘ
pub fn ready(&self) -> AsyncDerivedReadyFuture ⓘ
Returns a Future
that is ready when this resource has next finished loading.
Sourcepub fn by_ref(&self) -> AsyncDerivedRefFuture<T> ⓘ
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>
impl<T, Ser> AddAnyAttr for Resource<T, Ser>
Source§type Output<SomeNewAttr: Attribute> = Box<dyn FnMut() -> Suspend<<T as AddAnyAttr>::Output<<<SomeNewAttr as Attribute>::CloneableOwned as Attribute>::CloneableOwned>> + Send>
type Output<SomeNewAttr: Attribute> = Box<dyn FnMut() -> Suspend<<T as AddAnyAttr>::Output<<<SomeNewAttr as Attribute>::CloneableOwned as Attribute>::CloneableOwned>> + Send>
Source§fn add_any_attr<NewAttr>(
self,
attr: NewAttr,
) -> <Resource<T, Ser> as AddAnyAttr>::Output<NewAttr>
fn add_any_attr<NewAttr>( self, attr: NewAttr, ) -> <Resource<T, Ser> as AddAnyAttr>::Output<NewAttr>
Source§impl<T, Ser> DefinedAt for Resource<T, Ser>
impl<T, Ser> DefinedAt for Resource<T, Ser>
Source§fn defined_at(&self) -> Option<&'static Location<'static>>
fn defined_at(&self) -> Option<&'static Location<'static>>
None
in
release mode.Source§impl<T, Ser> From<ArcResource<T, Ser>> for Resource<T, Ser>
impl<T, Ser> From<ArcResource<T, Ser>> for Resource<T, Ser>
Source§fn from(arc_resource: ArcResource<T, Ser>) -> Resource<T, Ser>
fn from(arc_resource: ArcResource<T, Ser>) -> Resource<T, Ser>
Source§impl<T, Ser> From<Resource<T, Ser>> for ArcResource<T, Ser>
impl<T, Ser> From<Resource<T, Ser>> for ArcResource<T, Ser>
Source§fn from(resource: Resource<T, Ser>) -> ArcResource<T, Ser>
fn from(resource: Resource<T, Ser>) -> ArcResource<T, Ser>
Source§impl<T, Ser> IntoFuture for Resource<T, Ser>
impl<T, Ser> IntoFuture for Resource<T, Ser>
Source§type IntoFuture = AsyncDerivedFuture<T>
type IntoFuture = AsyncDerivedFuture<T>
Source§fn into_future(self) -> <Resource<T, Ser> as IntoFuture>::IntoFuture
fn into_future(self) -> <Resource<T, Ser> as IntoFuture>::IntoFuture
Source§impl<T, Ser> ReadUntracked for Resource<T, Ser>
impl<T, Ser> ReadUntracked for Resource<T, Ser>
Source§type Value = <AsyncDerived<T> as ReadUntracked>::Value
type Value = <AsyncDerived<T> as ReadUntracked>::Value
Source§fn try_read_untracked(
&self,
) -> Option<<Resource<T, Ser> as ReadUntracked>::Value>
fn try_read_untracked( &self, ) -> Option<<Resource<T, Ser> as ReadUntracked>::Value>
None
if the signal has already been disposed.Source§fn read_untracked(&self) -> Self::Value
fn read_untracked(&self) -> Self::Value
Source§fn custom_try_read(&self) -> Option<Option<Self::Value>>
fn custom_try_read(&self) -> Option<Option<Self::Value>>
Read::try_read
implementation despite it being auto implemented. Read moreSource§impl<T, Ser> Render for Resource<T, Ser>
impl<T, Ser> Render for Resource<T, Ser>
Source§impl<T, Ser> RenderHtml for Resource<T, Ser>
impl<T, Ser> RenderHtml for Resource<T, Ser>
Source§const MIN_LENGTH: usize = 0usize
const MIN_LENGTH: usize = 0usize
Source§type AsyncOutput = Option<T>
type AsyncOutput = Option<T>
Source§fn dry_resolve(&mut self)
fn dry_resolve(&mut self)
Source§fn resolve(
self,
) -> impl Future<Output = <Resource<T, Ser> as RenderHtml>::AsyncOutput> + Send
fn resolve( self, ) -> impl Future<Output = <Resource<T, Ser> as RenderHtml>::AsyncOutput> + Send
Source§fn to_html_with_buf(
self,
buf: &mut String,
position: &mut Position,
escape: bool,
mark_branches: bool,
)
fn to_html_with_buf( self, buf: &mut String, position: &mut Position, escape: bool, mark_branches: bool, )
Source§fn to_html_async_with_buf<const OUT_OF_ORDER: bool>(
self,
buf: &mut StreamBuilder,
position: &mut Position,
escape: bool,
mark_branches: bool,
)
fn to_html_async_with_buf<const OUT_OF_ORDER: bool>( self, buf: &mut StreamBuilder, position: &mut Position, escape: bool, mark_branches: bool, )
Source§fn hydrate<const FROM_SERVER: bool>(
self,
cursor: &Cursor,
position: &PositionState,
) -> <Resource<T, Ser> as Render>::State
fn hydrate<const FROM_SERVER: bool>( self, cursor: &Cursor, position: &PositionState, ) -> <Resource<T, Ser> as Render>::State
Source§const EXISTS: bool = true
const EXISTS: bool = true
Source§fn html_len(&self) -> usize
fn html_len(&self) -> usize
Source§fn to_html_branching(self) -> Stringwhere
Self: Sized,
fn to_html_branching(self) -> Stringwhere
Self: Sized,
Source§fn to_html_stream_in_order(self) -> StreamBuilderwhere
Self: Sized,
fn to_html_stream_in_order(self) -> StreamBuilderwhere
Self: Sized,
Source§fn to_html_stream_in_order_branching(self) -> StreamBuilderwhere
Self: Sized,
fn to_html_stream_in_order_branching(self) -> StreamBuilderwhere
Self: Sized,
Source§fn to_html_stream_out_of_order(self) -> StreamBuilderwhere
Self: Sized,
fn to_html_stream_out_of_order(self) -> StreamBuilderwhere
Self: Sized,
Source§fn to_html_stream_out_of_order_branching(self) -> StreamBuilderwhere
Self: Sized,
fn to_html_stream_out_of_order_branching(self) -> StreamBuilderwhere
Self: Sized,
Source§fn hydrate_from<const FROM_SERVER: bool>(self, el: &Element) -> Self::Statewhere
Self: Sized,
fn hydrate_from<const FROM_SERVER: bool>(self, el: &Element) -> Self::Statewhere
Self: Sized,
RenderHtml::hydrate
, beginning at the given element.Source§fn hydrate_from_position<const FROM_SERVER: bool>(
self,
el: &Element,
position: Position,
) -> Self::Statewhere
Self: Sized,
fn hydrate_from_position<const FROM_SERVER: bool>(
self,
el: &Element,
position: Position,
) -> Self::Statewhere
Self: Sized,
RenderHtml::hydrate
, beginning at the given element and position.impl<T, Ser> Copy for Resource<T, Ser>
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<V, Key, Sig, T> BindAttribute<Key, Sig, T> for Vwhere
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>,
impl<V, Key, Sig, T> BindAttribute<Key, Sig, T> for Vwhere
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>>
type Output = <V as AddAnyAttr>::Output<Bind<Key, T, <Sig as IntoSplitSignal>::Read, <Sig as IntoSplitSignal>::Write>>
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T, K, V> CustomAttribute<K, V> for T
impl<T, K, V> CustomAttribute<K, V> for T
Source§fn attr(self, key: K, value: V) -> Self::Output<CustomAttr<K, V>>
fn attr(self, key: K, value: V) -> Self::Output<CustomAttr<K, V>>
Source§impl<V, T, P, D> DirectiveAttribute<T, P, D> for V
impl<V, T, P, D> DirectiveAttribute<T, P, D> for V
Source§type Output = <V as AddAnyAttr>::Output<Directive<T, D, P>>
type Output = <V as AddAnyAttr>::Output<Directive<T, D, P>>
Source§fn directive(
self,
handler: D,
param: P,
) -> <V as DirectiveAttribute<T, P, D>>::Output
fn directive( self, handler: D, param: P, ) -> <V as DirectiveAttribute<T, P, D>>::Output
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§impl<T> IntoRender for Twhere
T: Render,
impl<T> IntoRender for Twhere
T: Render,
Source§fn into_render(self) -> <T as IntoRender>::Output
fn into_render(self) -> <T as IntoRender>::Output
Source§impl<T> Read for Twhere
T: Track + ReadUntracked,
impl<T> Read for Twhere
T: Track + ReadUntracked,
Source§impl<T> StorageAccess<T> for T
impl<T> StorageAccess<T> for T
Source§fn as_borrowed(&self) -> &T
fn as_borrowed(&self) -> &T
Source§fn into_taken(self) -> T
fn into_taken(self) -> T
Source§impl<T> With for Twhere
T: Read,
impl<T> With for Twhere
T: Read,
Source§type Value = <<T as Read>::Value as Deref>::Target
type Value = <<T as Read>::Value as Deref>::Target
Source§impl<T> WithUntracked for Twhere
T: DefinedAt + ReadUntracked,
impl<T> WithUntracked for Twhere
T: DefinedAt + ReadUntracked,
Source§type Value = <<T as ReadUntracked>::Value as Deref>::Target
type Value = <<T as ReadUntracked>::Value as Deref>::Target
Source§fn try_with_untracked<U>(
&self,
fun: impl FnOnce(&<T as WithUntracked>::Value) -> U,
) -> Option<U>
fn try_with_untracked<U>( &self, fun: impl FnOnce(&<T as WithUntracked>::Value) -> U, ) -> Option<U>
None
if the signal has already been disposed.