Skip to main content

euv_ui/hook/debounced_value/
struct.rs

1use super::*;
2
3/// A value that only emits after a quiet period of
4/// `delay` since its most recent `set`.
5///
6/// Constructed via `DebouncedValue::new(delay_ms)`
7/// (Lombok `New`); the emitted value starts at
8/// `T::default()` and the throttle state starts at
9/// `Idle`. Use [`DebouncedValue::set`] (or
10/// [`DebouncedValue::tick`] with a backdated timestamp)
11/// to seed the emitted value.
12///
13/// Typical use: pair with `App::use_interval` — the
14/// interval callback calls `tick(now_ms)` every
15/// N milliseconds, where `now_ms` comes from
16/// `performance.now()` on the web. After `delay_ms`
17/// without a fresh `set`, the pending value is committed.
18///
19/// This shape keeps the hook free of any browser /
20/// timer dependency so the same code runs in
21/// `cargo test` and in `wasm32-unknown-unknown` — the
22/// caller supplies the time source as plain milliseconds
23/// (`std::time::Instant::now()` panics on wasm, so the
24/// hook API deliberately takes `u64` millis).
25#[derive(Clone, Data, Debug, New)]
26pub struct DebouncedValue<T: Clone + PartialEq + Default + 'static> {
27    /// The emitted value signal. Defaults to
28    /// `Signal::create(T::default())` via
29    /// `#[new(skip)]`.
30    #[new(skip)]
31    #[get(type(copy))]
32    pub(crate) value: Signal<T>,
33    /// The internal pending/empty state. Defaults to
34    /// `Signal::create(DebounceState::Idle)` via
35    /// `#[new(skip)]`.
36    #[new(skip)]
37    pub(crate) state: Signal<DebounceState<T>>,
38    /// The quiet period in milliseconds.
39    pub(crate) delay_ms: u32,
40}
41
42/// `DebouncedValue<T>` is `Copy` when `T` is — every field
43/// (`Signal<T>`, `Signal<DebounceState<T>>`, `u32`) is itself
44/// `Copy`, so the blanket impl is sound.
45impl<T> Copy for DebouncedValue<T> where T: Clone + PartialEq + Default + 'static {}