restartWhen method

ResStream<S, F> restartWhen({
  1. bool onErr(
    1. F err,
    2. int attempt
    )?,
  2. bool onClose(
    1. int attempt
    )?,
  3. Duration delay(
    1. int attempt
    )?,
  4. bool hideErr = true,
})

Cancels the source and runs it again when onFailure or onClose returns true, such as to reconnect a dropped websocket feed.

onFailure is asked on each failure and onClose when the source closes. Give at least one; a trigger left out never restarts. attempt counts the restarts since the last success; use it to give up. delay gives the wait before a restart from the same attempt, such as (attempt) => Duration(seconds: 1 << attempt) to back off. Without it, the source runs again straight away.

A restart cancels the source at once and is reported to Mallard.onStreamRestart, then the delay is waited out. Nothing is emitted meanwhile, and a listener cancelling ends the wait.

With hideFailure (the default), a restart drops the failure, so Mallard.onStreamFailure never sees it. Without it, the failure is emitted before the restart. A failure that doesn't restart is emitted.

A callback that throws, delay included, emits an untyped error and counts as false. If the factory throws on a restart, the stream emits that error and closes. A restart while the listener is paused runs the source paused. A listener cancelling never restarts, and untyped stream errors pass through without asking.

A restart is invisible downstream. On a combined stream it reruns every source, so restart each source instead, before combineWith or inside chainStream. An onClose that always returns true without a delay restarts forever.

Implementation

ResStream<S, F> restartWhen({
  bool Function(F err, int attempt)? onErr,
  bool Function(int attempt)? onClose,
  Duration Function(int attempt)? delay,
  bool hideErr = true,
}) => ResStream._(
  _stream.restartWhen(
    onFailure: onErr,
    onClose: onClose,
    delay: delay,
    hideFailure: hideErr,
  ),
);