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>
impl<T: Clone + PartialEq + 'static> LazyComponent<T>
Sourcepub fn new(factory: impl Fn() -> T + 'static) -> Self
pub fn new(factory: impl Fn() -> T + 'static) -> Self
Creates a new lazy component with the given factory. The factory is NOT called yet.
Sourcepub fn state(&self) -> Signal<LoadState<T>>
pub fn state(&self) -> Signal<LoadState<T>>
Returns the reactive state signal. Subscribers see
transitions from Pending → Loading → Loaded
(or Failed).
Sourcepub fn is_resolved(&self) -> bool
pub fn is_resolved(&self) -> bool
Returns true if the factory has produced a
value (or failed).
Sourcepub fn is_pending(&self) -> bool
pub fn is_pending(&self) -> bool
Returns true if the factory is still pending or
loading.
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.
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.
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(&self, factory: impl Fn() -> T + 'static)
pub fn change_factory(&self, factory: impl Fn() -> T + 'static)
Replaces the factory. The state is reset to
Pending so the next get() runs the new
factory.