interval<T> method

void interval<T>(
  1. RxInterface<T> rx,
  2. void callback(
    1. T
    ), {
  3. Duration duration = const Duration(seconds: 1),
})
inherited

Calls callback at most once per duration, ignoring intermediate changes. Useful for rate-limiting UI updates.

interval(scrollPosition, (pos) => loadMore(pos),
  duration: const Duration(seconds: 1));

Implementation

void interval<T>(
  RxInterface<T> rx,
  void Function(T) callback, {
  Duration duration = const Duration(seconds: 1),
}) {
  var canCall = true;
  void handle(T val) {
    if (!canCall || isDisposed) return;
    canCall = false;
    callback(val);
    late Timer timer;
    timer = Timer(duration, () {
      _timers.remove(timer);
      canCall = true;
    });
    _timers.add(timer);
  }

  if (rx is SintListenable<T>) {
    final disposer = rx.addListener(() {
      if (!isDisposed && !rx.isDisposed) {
        handle(rx.peek);
      }
    });
    _syncDisposers.add(disposer);
    return;
  }

  _workers.add(rx.listen(handle));
}