createTimedStream function Null safety

Stream<DateTime> createTimedStream(
  1. TimedGenerator generator,
  2. Future stopSignal
)

Creates a stream tha produces DateTime objects at the times specified by the generator. Stops the stream when stopSignal is received.

Implementation

Stream<DateTime> createTimedStream(
  TimedGenerator generator,
  Future stopSignal,
) async* {
  var now = DateTime.now();
  DateTime? next;
  while ((next = generator(now)) != null) {
    if (now.compareTo(next!) > 0) continue;
    Duration waitTime = next.difference(now);
    try {
      await stopSignal.timeout(waitTime);
      return;
    } on TimeoutException catch (_) {
      yield next;
    }
    now = DateTime.now();
  }
}