createOneMinuteTickStreamController function

StreamController<Object> createOneMinuteTickStreamController()

Implementation

StreamController<Object> createOneMinuteTickStreamController() {
  final controller = StreamController<Object>.broadcast();
  controller.onListen = () async {
    final nowTime = DateTime.now();
    final nextTime = DateTime(
      nowTime.year,
      nowTime.month,
      nowTime.day,
      nowTime.hour,
      nowTime.minute,
    ).add(const Duration(minutes: 1));
    controller.add(const Object());
    await Future.delayed(nextTime.difference(nowTime));
    while (true) {
      if (controller.isClosed) return;
      controller.add(const Object());
      await Future.delayed(const Duration(minutes: 1));
    }
  };
  return controller;
}