KThrottler

class KThrottler<T>(scope: CoroutineScope, val period: Duration = 500.milliseconds)(source)

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") }

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.

Constructors

Link copied to clipboard
constructor(scope: CoroutineScope, period: Duration = 500.milliseconds)

Properties

Link copied to clipboard
val flow: Flow<T>

Throttled output flow.

Link copied to clipboard

Functions

Link copied to clipboard
fun cancel()
Link copied to clipboard
fun emit(value: T)
Link copied to clipboard
fun onThrottled(block: suspend (T) -> Unit)