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 timestamp)
to seed the emitted value.
Typical use: pair with App::use_interval — the
interval callback calls tick(now_ms) every
N milliseconds, where now_ms comes from
performance.now() on the web. 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 as plain milliseconds
(std::time::Instant::now() panics on wasm, so the
hook API deliberately takes u64 millis).
Implementations§
Source§impl<T: Clone + PartialEq + Default + 'static> DebouncedValue<T>
Inherent implementation of DebouncedValue.
impl<T: Clone + PartialEq + Default + 'static> DebouncedValue<T>
Inherent implementation of DebouncedValue.
Sourcepub fn set(&self, next: T, now_ms: u64)
pub fn set(&self, next: T, now_ms: u64)
Schedules next to become the emitted value. The
commit happens on the next tick call at or after
now_ms + 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.u64- The current time in milliseconds (any monotonic source; on the web useperformance.now()).
Sourcepub fn tick(&self, now_ms: u64) -> bool
pub fn tick(&self, now_ms: u64) -> 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_ms - 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
u64- The current time in milliseconds.
§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 moreimpl<T> Copy for DebouncedValue<T>
DebouncedValue<T> is Copy when T is — every field
(Signal<T>, Signal<DebounceState<T>>, u32) is itself
Copy, so the blanket impl is sound.
Source§impl<T: Clone + PartialEq + Debug + Default + 'static> Display for DebouncedValue<T>
Debug formatting for DebouncedValue.
impl<T: Clone + PartialEq + Debug + Default + 'static> Display for DebouncedValue<T>
Debug formatting for DebouncedValue.