Package 

Interface StreamThrottler


  • 
    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 Unit onValue(SuspendFunction1<T, Unit> callback) Registers the handler invoked when a value passes through the throttle.
      abstract Boolean submit(T value) Submits a value.
      abstract Unit reset() Resets the throttle window and discards any pending trailing delivery.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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.
      • reset

         abstract Unit reset()

        Resets the throttle window and discards any pending trailing delivery.