Skip to main content

DebouncedValue

Struct DebouncedValue 

Source
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.

Source

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 use performance.now()).
Source

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 emits value (writing it to the value signal) iff now_ms - set_at >= delay_ms. Otherwise the pending value is preserved and the call returns false.

Returns true when a pending value was emitted, false otherwise.

§Arguments
  • u64 - The current time in milliseconds.
§Returns
  • bool - A boolean.
Source

pub fn cancel(&self)

Cancels any pending value without emitting it. The emitted value is left untouched.

Source

pub fn get(&self) -> T

Returns the currently emitted value as a snapshot.

§Returns
  • T - The current value (or a snapshot thereof).
Source

pub fn is_pending(&self) -> bool

Returns true when a value is waiting to be emitted.

§Returns
  • bool - true when a value is waiting to be emitted.
Source§

impl<T: Clone + PartialEq + Default + 'static> DebouncedValue<T>

Source

pub fn get_value(&self) -> Signal<T>

Source

pub fn get_mut_value(&mut self) -> &mut Signal<T>

Source

pub fn set_value(&mut self, val: Signal<T>) -> &mut Self

Source

pub fn get_state(&self) -> &Signal<DebounceState<T>>

Source

pub fn get_mut_state(&mut self) -> &mut Signal<DebounceState<T>>

Source

pub fn set_state(&mut self, val: Signal<DebounceState<T>>) -> &mut Self

Source

pub fn get_delay_ms(&self) -> &u32

Source

pub fn get_mut_delay_ms(&mut self) -> &mut u32

Source

pub fn set_delay_ms(&mut self, val: u32) -> &mut Self

Source§

impl<T: Clone + PartialEq + Default + 'static> DebouncedValue<T>

Source

pub fn new(delay_ms: u32) -> Self

Trait Implementations§

Source§

impl<T: Clone + Clone + PartialEq + Default + 'static> Clone for DebouncedValue<T>

Source§

fn clone(&self) -> DebouncedValue<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Copy for DebouncedValue<T>
where T: Clone + PartialEq + Default + 'static,

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: Debug + Clone + PartialEq + Default + 'static> Debug for DebouncedValue<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Clone + PartialEq + Debug + Default + 'static> Display for DebouncedValue<T>

Debug formatting for DebouncedValue.

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> FmtResult

Formats the DebouncedValue via the supplied formatter.

§Arguments
  • &mut Formatter<'_> - The formatter receiving the formatted output.
§Returns
  • FmtResult - Result of the formatting operation.

Auto Trait Implementations§

§

impl<T> Freeze for DebouncedValue<T>
where Signal<T>: Freeze, Signal<DebounceState<T>>: Freeze,

§

impl<T> RefUnwindSafe for DebouncedValue<T>
where Signal<T>: RefUnwindSafe, Signal<DebounceState<T>>: RefUnwindSafe,

§

impl<T> Send for DebouncedValue<T>
where Signal<T>: Send, Signal<DebounceState<T>>: Send,

§

impl<T> Sync for DebouncedValue<T>
where Signal<T>: Sync, Signal<DebounceState<T>>: Sync,

§

impl<T> Unpin for DebouncedValue<T>
where Signal<T>: Unpin, Signal<DebounceState<T>>: Unpin,

§

impl<T> UnsafeUnpin for DebouncedValue<T>
where Signal<T>: UnsafeUnpin, Signal<DebounceState<T>>: UnsafeUnpin,

§

impl<T> UnwindSafe for DebouncedValue<T>
where Signal<T>: UnwindSafe, Signal<DebounceState<T>>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more