-
public interface StreamThrottler<T extends Object>Rate-limits a bursty stream of values so that at most one value is delivered per time window.
Behaviour depends on the StreamThrottlePolicy:
Leading: First value delivered immediately, rest dropped until window expires. Best for: typing indicators, presence updates.
Trailing: Values collected during the window, only the last value delivered when the window expires. Best for: position/progress updates where latest state matters.
LeadingAndTrailing: First value delivered immediately, AND the last value delivered when the window expires (if a newer value was submitted during the window). Best for: scroll position tracking, where both responsiveness and final accuracy matter.
Thread-safety: All functions are safe to call from multiple coroutines.
Cancellation:reset clears any pending trailing delivery and resets the window.
// Typing indicators — leading edge, fire immediately, suppress duplicates val throttler = StreamThrottler<TypingEvent>(scope, logger, policy = StreamThrottlePolicy.leading()) throttler.onValue { event -> api.sendTypingEvent(event) } // Position updates — trailing edge, only latest state matters val throttler = StreamThrottler<Position>(scope, logger, policy = StreamThrottlePolicy.trailing()) // Scroll tracking — both edges val throttler = StreamThrottler<Int>(scope, logger, policy = StreamThrottlePolicy.leadingAndTrailing())
-
-
Method Summary
Modifier and Type Method Description abstract UnitonValue(SuspendFunction1<T, Unit> callback)Registers the handler invoked when a value passes through the throttle. abstract Booleansubmit(T value)Submits a value. abstract Unitreset()Resets the throttle window and discards any pending trailing delivery. -
-
Method Detail
-
onValue
abstract Unit onValue(SuspendFunction1<T, Unit> callback)
Registers the handler invoked when a value passes through the throttle.
- Parameters:
callback- Suspend function called with the accepted value.
-
submit
abstract Boolean submit(T value)
Submits a value. Behaviour depends on the configured StreamThrottlePolicy.
- Parameters:
value- The value to throttle.
-
-
-
-