Skip to main content

ThrottledValue

Struct ThrottledValue 

Source
pub struct ThrottledValue<T: Clone + PartialEq + Default + 'static> { /* private fields */ }
Expand description

A value that emits the most recent input at most once per interval_ms.

Constructed via ThrottledValue::new(interval_ms) (Lombok New); the emitted value starts at T::default() and the throttle state starts at Idle. Use ThrottledValue::set to seed the emitted value.

Unlike DebouncedValue, which waits for a quiet period, a throttled value commits a snapshot every interval_ms regardless of how often set was called.

Pair with App::use_interval — the interval callback calls tick(now_ms) every interval_ms, where now_ms comes from performance.now() on the web. The caller picks the time source (plain u64 millis) so the hook stays free of browser / timer dependencies and works on wasm32-unknown-unknown (where std::time::Instant::now() panics).

Implementations§

Source§

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

Inherent implementation of ThrottledValue.

Source

pub fn set(&self, next: T, now_ms: u64)

Sends next through the throttle.

  • If the throttle is idle (no cooldown active), the value is emitted immediately, and a cooldown window of interval_ms opens starting at now.
  • If a cooldown is active, next is stored as the next pending value. The pending value will be committed on the next tick call once the cooldown expires. Intermediate set calls during the cooldown overwrite the pending slot — only the most recent input wins.

interval_ms = 0 collapses to “every set is immediately committed” — the cooldown branch is never taken.

§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 throttle forward.

  • If idle, no-op.
  • If a cooldown is active and the cooldown window has elapsed, any pending value is committed and the state returns to Idle. If nothing is pending, the state still returns to Idle. The cooldown simply lapses in that case.

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

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

pub fn cancel(&self)

Drops any pending value and ends the cooldown. 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_throttling(&self) -> bool

Returns true when the throttle is in a cooldown window (recent set that has not yet had a chance to commit any pending follow-up).

§Returns
  • bool - true when a throttle delay is active.
Source§

impl<T: Clone + PartialEq + Default + 'static> ThrottledValue<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_pending(&self) -> &Signal<Option<T>>

Source

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

Source

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

Source

pub fn get_state(&self) -> &Signal<ThrottleState>

Source

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

Source

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

Source

pub fn get_interval_ms(&self) -> &u32

Source

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

Source

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

Source§

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

Source

pub fn new(interval_ms: u32) -> Self

Trait Implementations§

Source§

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

Source§

fn clone(&self) -> ThrottledValue<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 ThrottledValue<T>
where T: Clone + PartialEq + Default + 'static,

ThrottledValue<T> is Copy when T is — every field is itself Copy (Signal<T>, Signal<Option<T>>, u32) or a simple enum, so the blanket impl is sound.

Source§

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

Debug formatting for ThrottledValue.

Source§

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

Formats the ThrottledValue via the supplied formatter.

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

Auto Trait Implementations§

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