restartWhen method
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. There is no built-in delay,
backoff or limit: await a delay in the callback and use attempt to
give up.
With hideFailure (the default), the source is paused while onFailure
decides. A restart drops the failure and everything after it, so
Mallard.onStreamFailure never sees them. Giving up emits the failure
and resumes the source. Without hideFailure, the failure is emitted
straight away and events keep flowing while onFailure decides. Either
way, every restart is reported to Mallard.onStreamRestart.
One decision runs at a time. Failures that arrive meanwhile don't ask
again: with hideFailure they are held and dropped on a restart, and
without it they are emitted. A close waits for the pending decision, then
asks onClose unless the stream restarted. A callback that throws 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({
FutureOr<bool> Function(F err, int attempt)? onErr,
FutureOr<bool> Function(int attempt)? onClose,
bool hideErr = true,
}) => ResStream._(
_stream.restartWhen(
onFailure: onErr,
onClose: onClose,
hideFailure: hideErr,
),
);