Skip to main content

euv_ui/hook/throttled_value/
struct.rs

1use super::*;
2
3/// A value that emits the most recent input at most once
4/// per `interval_ms`.
5///
6/// Constructed via `ThrottledValue::new(interval_ms)`
7/// (Lombok `New`); the emitted value starts at
8/// `T::default()` and the throttle state starts at
9/// `Idle`. Use [`ThrottledValue::set`] to seed the
10/// emitted value.
11///
12/// Unlike [`DebouncedValue`], which waits for a quiet
13/// period, a throttled value commits a snapshot every
14/// `interval_ms` regardless of how often `set` was called.
15///
16/// Pair with `App::use_interval` — the interval callback
17/// calls `tick(Instant::now())` every `interval_ms`. The
18/// caller picks the time source so the hook stays free
19/// of browser / timer dependencies.
20#[derive(Clone, Data, Debug, New)]
21pub struct ThrottledValue<T: Clone + PartialEq + Default + 'static> {
22    /// The emitted value signal. Defaults to
23    /// `Signal::create(T::default())` via
24    /// `#[new(skip)]`.
25    #[new(skip)]
26    pub(crate) value: Signal<T>,
27    /// The latest input waiting for the next commit.
28    /// Defaults to `Signal::create(None)` via
29    /// `#[new(skip)]`.
30    #[new(skip)]
31    pub(crate) pending: Signal<Option<T>>,
32    /// The internal idle/cooldown state. Defaults to
33    /// `Signal::create(ThrottleState::Idle)` via
34    /// `#[new(skip)]`.
35    #[new(skip)]
36    pub(crate) state: Signal<ThrottleState>,
37    /// The throttle window in milliseconds.
38    pub(crate) interval_ms: u32,
39}