euv_ui/hook/use_async/struct.rs
1use super::*;
2
3/// `use_async`'s reactive handle, stored in the hook context slot and
4/// returned to the user on each render.
5///
6/// The handle exposes three fields:
7///
8/// - `state`: the current `AsyncState<T, L>` (matches what the user
9/// should `match` on in `html!`).
10/// - `refetch`: triggers the future to run again, regardless of
11/// whether the previous attempt completed or is still in flight.
12/// - `cancel`: drops the in-flight future (if any) and prevents its
13/// `Ok`/`Err` branches from mutating the state. Subsequent renders
14/// will still call the future again on the next mount.
15///
16/// Cloning a handle is cheap — `UseAsyncHandle` is `Copy` if its
17/// generic parameters are. Use it from event handlers the same way
18/// you'd use a `Signal<T>`.
19#[derive(Clone, Data)]
20pub struct UseAsyncHandle<T, L>
21where
22 T: Clone + PartialEq + 'static,
23 L: Clone + PartialEq + HasLoadingHint + 'static,
24{
25 /// Address of the heap-allocated `UseAsyncInner<T, L>` state.
26 pub(crate) inner: usize,
27 /// `Copy` marker so `UseAsyncHandle` itself is `Copy`.
28 pub(crate) _marker: core::marker::PhantomData<fn() -> (T, L)>,
29}
30
31/// Blanket `Copy` for any generic instance — both fields are
32/// themselves `Copy` (`usize`, `PhantomData<fn pointer>`).
33/// The `where` clause must be repeated because a separate impl
34/// block cannot inherit bounds from the type declaration.
35impl<T, L> core::marker::Copy for UseAsyncHandle<T, L>
36where
37 T: Clone + PartialEq + 'static,
38 L: Clone + PartialEq + HasLoadingHint + 'static,
39{
40}
41
42/// Heap-allocated state backing a [`super::UseAsyncHandle`].
43///
44/// Reachable only through the raw address stored in the handle.
45/// Allocated by [`super::UseAsyncHandle::new_for_fallback`] for the
46/// "no hook context" case and by [`HookContext::use_async`] when
47/// the hook is registered for the first time.
48#[derive(Clone, Data)]
49pub(crate) struct UseAsyncSlot<T, L>
50where
51 T: Clone + PartialEq + 'static,
52 L: Clone + PartialEq + HasLoadingHint + 'static,
53{
54 /// Reactive state, exposed to the user as
55 /// [`UseAsyncHandle::state`].
56 pub(crate) state: Signal<AsyncState<T, L>>,
57 /// Cancellation flag — flipped on drop. The in-flight future
58 /// reads this before writing back to `state`.
59 pub(crate) cancel: Rc<Cell<bool>>,
60}