pub struct DebouncedValue<T: Clone + PartialEq + Default + 'static> { /* private fields */ }Expand description
A value that only emits after a quiet period of
delay since its most recent set.
Constructed via DebouncedValue::new(delay_ms)
(Lombok New); the emitted value starts at
T::default() and the throttle state starts at
Idle. Use DebouncedValue::set (or
DebouncedValue::tick with a backdated Instant)
to seed the emitted value.
Typical use: pair with App::use_interval — the
interval callback calls tick(Instant::now()) every
N milliseconds. After delay_ms without a fresh
set, the pending value is committed.
This shape keeps the hook free of any browser /
timer dependency so the same code runs in
cargo test and in wasm32-unknown-unknown — the
caller supplies the time source.
Implementations§
Source§impl<T: Clone + PartialEq + Default + 'static> DebouncedValue<T>
impl<T: Clone + PartialEq + Default + 'static> DebouncedValue<T>
Sourcepub fn set(&self, next: T, now: Instant)
pub fn set(&self, next: T, now: Instant)
Schedules next to become the emitted value. The
commit happens on the next tick call at or after
now + delay_ms.
Calling set repeatedly within delay_ms of each
other means only the last value wins — that’s the
“debounce” contract.
§Arguments
T: Clone + PartialEq + Default + 'static- A generic type parameter.Instant- A monotonic instant in time (Instant).
Sourcepub fn tick(&self, now: Instant) -> bool
pub fn tick(&self, now: Instant) -> bool
Drives the state machine forward.
- If the state is
Idle, this is a no-op. - If the state is
Pending(set_at, value), this emitsvalue(writing it to thevaluesignal) iffnow - set_at >= delay_ms. Otherwise the pending value is preserved and the call returnsfalse.
Returns true when a pending value was emitted,
false otherwise.
§Arguments
Instant- A monotonic instant in time (Instant).
§Returns
bool- A boolean.
Sourcepub fn cancel(&self)
pub fn cancel(&self)
Cancels any pending value without emitting it. The emitted value is left untouched.
Sourcepub fn get(&self) -> T
pub fn get(&self) -> T
Returns the currently emitted value as a snapshot.
§Returns
T- The current value (or a snapshot thereof).
Sourcepub fn is_pending(&self) -> bool
pub fn is_pending(&self) -> bool
Returns true when a value is waiting to be
emitted.
§Returns
bool-truewhen a value is waiting to be emitted.
Source§impl<T: Clone + PartialEq + Default + 'static> DebouncedValue<T>
impl<T: Clone + PartialEq + Default + 'static> DebouncedValue<T>
pub fn get_value(&self) -> &Signal<T>
pub fn get_mut_value(&mut self) -> &mut Signal<T>
pub fn set_value(&mut self, val: Signal<T>) -> &mut Self
pub fn get_state(&self) -> &Signal<DebounceState<T>>
pub fn get_mut_state(&mut self) -> &mut Signal<DebounceState<T>>
pub fn set_state(&mut self, val: Signal<DebounceState<T>>) -> &mut Self
pub fn get_delay_ms(&self) -> &u32
pub fn get_mut_delay_ms(&mut self) -> &mut u32
pub fn set_delay_ms(&mut self, val: u32) -> &mut Self
Trait Implementations§
Source§impl<T: Clone + Clone + PartialEq + Default + 'static> Clone for DebouncedValue<T>
impl<T: Clone + Clone + PartialEq + Default + 'static> Clone for DebouncedValue<T>
Source§fn clone(&self) -> DebouncedValue<T>
fn clone(&self) -> DebouncedValue<T>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more