withInitialDelay method

Stream<T> withInitialDelay(
  1. Duration minOperationTime, {
  2. Duration threshold = const Duration(milliseconds: 50),
  3. @Deprecated("Will be removed in version 2.0.0, use " "the 'threshold' parameter instead.") int thresholdInMillis = 50,
})

If the time it takes for the first event to appear is less than minOperationTime, then a Future.delayed is awaited for the remaining time. withInitialDelay only affects the first event of this Stream. The delay also affects any errors of this Stream.

The Stream returned by this method will take at least as much time as specified by minOperationTime to complete.

When a subscription on the returned Stream is paused, then the internal stopwatch that is used to check if minOperationTime has already passed, will also be stopped. Resuming the subscription starts the watch again.

Implementation

Stream<T> withInitialDelay(
  Duration minOperationTime, {
  Duration threshold = const Duration(milliseconds: 50),
  @Deprecated(
    "Will be removed in version 2.0.0, use "
    "the 'threshold' parameter instead.",
  )
  int thresholdInMillis = 50,
}) {
  // TODO(obemu): remove/change the asserts and if condition in v2.0.0

  // minOperationTime is less than 1 millisecond, therefore
  // we do not add a fake delay.
  if (minOperationTime.inMilliseconds < 1) return this;

  assert(minOperationTime < const Duration(minutes: 10));
  assert(minOperationTime.inMilliseconds >= thresholdInMillis);

  // Can be removed in v2.0.0, once [thresholdInMillis] is removed.
  var localThreshold = threshold;
  if (50 != thresholdInMillis) {
    if (const Duration(milliseconds: 50) != threshold)
      throw ArgumentError.value(
        thresholdInMillis,
        "thresholdInMillis",
        "Cannot provide a value for 'thresholdInMillis' and "
            "'threshold', you should only use the 'threshold' parameter",
      );

    localThreshold = Duration(milliseconds: thresholdInMillis);
  }

  return transform(
    InitialDelayStreamTransformer(
      minOperationTime: minOperationTime,
      threshold: localThreshold,
    ),
  );
}