throttle method

T? throttle(
  1. T func()
)

Limits the maximum number of times a given event handler can be called over time.

Returns the result of the function. If the function is not ready to accept new events, it returns null.

Implementation

T? throttle(T Function() func) {
  if (_stateSC.isClosed || !isIdle) return null;
  _timer = Timer(_duration, () {
    _timer = null;
    if (_stateSC.isClosed) return;
    _stateSC.sink.add(ThrottlingStatus.idle);
  });
  _stateSC.sink.add(ThrottlingStatus.busy);
  try {
    return func();
  } on Object {
    rethrow;
  }
}