pub struct LazyComponent<T: Clone + PartialEq + 'static> { /* private fields */ }Expand description
A lazy component that defers factory invocation until first access.
Use LazyComponent::new(factory) to construct, then
get() to read (which triggers the factory on first
call) or prefetch() to trigger without reading.
§Why use Rc<dyn Fn() -> T>?
Because the factory must be callable multiple times
(e.g. after a reset()), and dyn Fn lets the user
pass any closure that produces a T. The factory is
stored as Rc<dyn Fn() -> T> (not Box<dyn Fn>) so
LazyComponent can be cloned cheaply and shared
between hook contexts.
Implementations§
Source§impl<T: Clone + PartialEq + 'static> LazyComponent<T>
Inherent implementation of LazyComponent.
impl<T: Clone + PartialEq + 'static> LazyComponent<T>
Inherent implementation of LazyComponent.
Sourcepub fn new<F>(factory: F) -> Selfwhere
F: Fn() -> T + 'static,
pub fn new<F>(factory: F) -> Selfwhere
F: Fn() -> T + 'static,
Creates a new lazy component with the given factory. The factory is NOT called yet.
§Arguments
F: Fn() -> T + 'static- A generic type parameter.
Sourcepub fn prefetch(&self)
pub fn prefetch(&self)
Triggers the factory without reading the value.
Idempotent: calling prefetch() twice does not
run the factory twice.
Sourcepub fn get(&self) -> Option<T>
pub fn get(&self) -> Option<T>
Reads the value, calling the factory on the first call. Subsequent calls return the cached value.
§Returns
Option<T>- The current value (or a snapshot thereof).
Sourcepub fn loaded(&self) -> Option<T>
pub fn loaded(&self) -> Option<T>
Returns the loaded value, or None if the
state is Pending, Loading, or Failed.
Use Self::get (which runs the factory if
needed) when you want the value-or-None semantics.
This method is for the rare case where you already
know the value was loaded and you want to inspect
it without triggering a synchronous factory call.
§Returns
Option<T>-Some(value)when an asynchronously-loaded value is available, otherwiseNone.
Sourcepub fn reset(&self)
pub fn reset(&self)
Resets the lazy component to Pending. The next
get() call will re-run the factory.
Sourcepub fn change_factory<F>(&self, factory: F)where
F: Fn() -> T + 'static,
pub fn change_factory<F>(&self, factory: F)where
F: Fn() -> T + 'static,
Replaces the factory. The state is reset to
Pending so the next get() runs the new
factory.
§Arguments
F: Fn() -> T + 'static- A generic type parameter.
Source§impl<T: Clone + PartialEq + 'static> LazyComponent<T>
impl<T: Clone + PartialEq + 'static> LazyComponent<T>
pub fn get_state(&self) -> &Signal<LoadState<T>>
pub fn get_mut_state(&mut self) -> &mut Signal<LoadState<T>>
pub fn set_state(&mut self, val: Signal<LoadState<T>>) -> &mut Self
pub fn get_factory(&self) -> &Rc<dyn Fn() -> T>
pub fn get_mut_factory(&mut self) -> &mut Rc<dyn Fn() -> T>
pub fn set_factory(&mut self, val: Rc<dyn Fn() -> T>) -> &mut Self
Trait Implementations§
Source§impl<T: Clone + Clone + PartialEq + 'static> Clone for LazyComponent<T>
impl<T: Clone + Clone + PartialEq + 'static> Clone for LazyComponent<T>
Source§fn clone(&self) -> LazyComponent<T>
fn clone(&self) -> LazyComponent<T>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T: Clone + PartialEq + Debug + 'static> Debug for LazyComponent<T>
Debug formatting for LazyComponent.
impl<T: Clone + PartialEq + Debug + 'static> Debug for LazyComponent<T>
Debug formatting for LazyComponent.