interval<T> function

Worker interval<T>(
  1. RxInterface<T> listener,
  2. dynamic callback(
    1. T
    ),
  3. {Duration time = const Duration(seconds: 1),
  4. dynamic condition = true}
)

Ignore all changes in listener during time (1 sec by default) or until condition is met (can be a bool expression or a bool Function()), It brings the 1st "value" since the period of time, so if you click a counter button 3 times in 1 sec, it will show you "1" (after 1 sec of the first press) click counter 3 times in 1 sec, it will show you "4" (after 1 sec) click counter 2 times in 1 sec, it will show you "7" (after 1 sec).

Sample: // wait 1 sec each time an event starts, only if counter is lower than 20. worker = interval( count, (value) => print(value), time: 1.seconds, condition: () => count < 20, );

Implementation

Worker interval<T>(RxInterface<T> listener, Function(T) callback,
    {Duration time = const Duration(seconds: 1), dynamic condition = true}) {
  var debounceActive = false;
  time ??= const Duration(seconds: 1);
  StreamSubscription sub = listener.subject.stream.listen((event) async {
    if (debounceActive || !_conditional(condition)) return;
    debounceActive = true;
    await Future.delayed(time);
    debounceActive = false;
    callback(event);
  });
  return Worker(sub.cancel, '[interval]');
}