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 /// The reactive load state.
21 ///
22 /// Public because `ui/tests/lazy/fn.rs` reads it
23 /// through `get_state()`.
24 pub state: Signal<LoadState<T>>,
25 /// The deferred value factory.
26 ///
27 /// `get_factory()` has no caller outside this crate
28 /// (`impl.rs` invokes it internally), so the generated
29 /// getter, mutable getter and setter are all
30 /// `pub(crate)`.
31 #[get(pub(crate))]
32 #[get_mut(pub(crate))]
33 #[set(pub(crate))]
34 pub(crate) factory: Rc<dyn Fn() -> T>,
35}