euv_ui/hook/lazy/struct.rs
1use super::*;
2
3/// A lazy component that defers factory invocation until
4/// first access.
5///
6/// Use `LazyComponent::new(factory)` to construct, then
7/// `get()` to read (which triggers the factory on first
8/// call) or `prefetch()` to trigger without reading.
9///
10/// # Why use `Rc<dyn Fn() -> T>`?
11///
12/// Because the factory must be callable multiple times
13/// (e.g. after a `reset()`), and `dyn Fn` lets the user
14/// pass any closure that produces a `T`. The factory is
15/// stored as `Rc<dyn Fn() -> T>` (not `Box<dyn Fn>`) so
16/// `LazyComponent` can be cloned cheaply and shared
17/// between hook contexts.
18#[derive(Clone, Data)]
19pub struct LazyComponent<T: Clone + PartialEq + 'static> {
20 pub(crate) state: Signal<LoadState<T>>,
21 pub(crate) factory: Rc<dyn Fn() -> T>,
22}