timestampedStream method

Stream<({DateTime timestamp, Object? value})> timestampedStream({
  1. bool yieldAll = false,
})

Implementation

Stream<({Object? value, DateTime timestamp})> timestampedStream(
    {bool yieldAll = false}) async* {
  yield (
    value: currentValue,
    timestamp: DateTime.fromMicrosecondsSinceEpoch(timestamp),
  );
  var lastYielded = (
    value: currentValue,
    timestamp: DateTime.fromMicrosecondsSinceEpoch(timestamp),
  );

  while (true) {
    await Future.delayed(
        Duration(milliseconds: (options.periodicRateSeconds * 1000).round()));

    var current = (
      value: currentValue,
      timestamp: DateTime.fromMicrosecondsSinceEpoch(timestamp),
    );
    if (current != lastYielded || yieldAll) {
      yield current;
      lastYielded = current;
    }
  }
}