KThrottler
A coroutine-based throttler that ensures at most one value is forwarded per period.
Throttling is useful for rate-limiting expensive operations like window resizing, scrolling events, or "fast-click" button protection.
Uses leading-edge semantics: the first value in each window is emitted immediately; subsequent values within the same period are dropped.
Example usage:
val throttler = KThrottler<Unit>(scope = viewModelScope, period = 500.milliseconds)
throttler.onThrottled {
println("Button clicked - performing action")
}
// In UI:
Button(onClick = { throttler.emit(Unit) }) { Text("Click Me") }Content copied to clipboard
Parameters
scope
The CoroutineScope in which the throttler runs.
period
The minimum time interval between consecutive emissions. Default: 500ms.
Type Parameters
T
The type of values being throttled.