Skip to main content

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, Copy, 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/// Heap-allocated state backing a [`super::UseAsyncHandle`].
32///
33/// Reachable only through the raw address stored in the handle.
34/// Allocated by [`super::UseAsyncHandle::new_for_fallback`] for the
35/// "no hook context" case and by [`HookContext::use_async`] when
36/// the hook is registered for the first time.
37#[derive(Clone, Data)]
38pub(crate) struct UseAsyncSlot<T, L>
39where
40    T: Clone + PartialEq + 'static,
41    L: Clone + PartialEq + HasLoadingHint + 'static,
42{
43    /// Reactive state, exposed to the user as
44    /// [`UseAsyncHandle::state`].
45    pub(crate) state: Signal<AsyncState<T, L>>,
46    /// Cancellation flag — flipped on drop. The in-flight future
47    /// reads this before writing back to `state`.
48    pub(crate) cancel: Rc<Cell<bool>>,
49}