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.
impl<T: Clone + PartialEq + Default + 'static> ThrottledValue<T>
Inherent implementation of ThrottledValue.
Sourcepub fn set(&self, next: T, now_ms: u64)
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_msopens starting atnow. - If a cooldown is active,
nextis stored as the next pending value. The pending value will be committed on the nexttickcall once the cooldown expires. Intermediatesetcalls 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 useperformance.now()).
Sourcepub fn tick(&self, now_ms: u64) -> bool
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 toIdle. 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.
Sourcepub fn cancel(&self)
pub fn cancel(&self)
Drops any pending value and ends the cooldown. 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_throttling(&self) -> bool
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-truewhen a throttle delay is active.
Source§impl<T: Clone + PartialEq + Default + 'static> ThrottledValue<T>
impl<T: Clone + PartialEq + Default + 'static> ThrottledValue<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_pending(&self) -> &Signal<Option<T>>
pub fn get_mut_pending(&mut self) -> &mut Signal<Option<T>>
pub fn set_pending(&mut self, val: Signal<Option<T>>) -> &mut Self
pub fn get_state(&self) -> &Signal<ThrottleState>
pub fn get_mut_state(&mut self) -> &mut Signal<ThrottleState>
pub fn set_state(&mut self, val: Signal<ThrottleState>) -> &mut Self
pub fn get_interval_ms(&self) -> &u32
pub fn get_mut_interval_ms(&mut self) -> &mut u32
pub fn set_interval_ms(&mut self, val: u32) -> &mut Self
Trait Implementations§
Source§impl<T: Clone + Clone + PartialEq + Default + 'static> Clone for ThrottledValue<T>
impl<T: Clone + Clone + PartialEq + Default + 'static> Clone for ThrottledValue<T>
Source§fn clone(&self) -> ThrottledValue<T>
fn clone(&self) -> ThrottledValue<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 ThrottledValue<T>
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: Clone + PartialEq + Debug + Default + 'static> Display for ThrottledValue<T>
Debug formatting for ThrottledValue.
impl<T: Clone + PartialEq + Debug + Default + 'static> Display for ThrottledValue<T>
Debug formatting for ThrottledValue.