interval<T> function Null safety

Worker interval<T>(
  1. RxInterface<T> listener,
  2. WorkerCallback<T> callback,
  3. {Duration time = const Duration(seconds: 1),
  4. dynamic condition = true,
  5. Function? onError,
  6. void onDone(
      )?,
    1. bool? cancelOnError}
    )

    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,
      WorkerCallback<T> callback, {
      Duration time = const Duration(seconds: 1),
      dynamic condition = true,
      Function? onError,
      void Function()? onDone,
      bool? cancelOnError,
    }) {
      var debounceActive = false;
      StreamSubscription sub = listener.listen(
        (event) async {
          if (debounceActive || !_conditional(condition)) return;
          debounceActive = true;
          await Future.delayed(time);
          debounceActive = false;
          callback(event);
        },
        onError: onError,
        onDone: onDone,
        cancelOnError: cancelOnError,
      );
      return Worker(sub.cancel, '[interval]');
    }