peek, whenComplete & handleError
Side effects, per-error continue, and a finalize hook — without dropping off the chain to .stream.
Lecture
The pull layer's peek observes
values as they are pulled. The events-layer
peek is the same word for the same idea on a push
stream: run a side effect, pass the event through unchanged.
Optional onError / onDone hooks cover the
other two notifications. A throwing callback becomes an error
event and the chain continues — the event whose peek failed is
not re-emitted. fxdart events layer, after Rx's tap /
doOn*.
handleError is the per-error-and-continue form:
matching errors (every error when test is omitted)
run onError and the stream keeps going. That is not
onErrorResume, which
cancels the source and switches. Reach for handleError to log or
swallow a glitch; reach for onErrorResume to abandon the
connection. whenComplete is Rx's
finalize: the callback runs
exactly once, on done, on error, or on cancel.
Even if it throws, the chain still tears down.
The chain no longer drops to .stream for
endWith, ifEmpty, uniq,
takeRight, takeWhile either — they are
FxEvents methods now. uniq is global
(not adjacent): the seen-set grows without bound, so a long-lived
feed plus unbounded uniq is a memory leak; prefer
uniqAdjacent when only consecutive repeats should
go.
fxdart events layer, after Rx's tap,
catchError in its non-switching shape, and
finalize. Pull-layer names win where they already
mean the same thing: peek not tap,
takeRight not takeLast,
uniq not distinct.
Demo 1 · peek as a side effect
Demo 2 · endWith, and ifEmpty
Try it yourself
Exercise: whenComplete on done, and on cancel.
peek — the pull-layer original, observing values as they are pulled ·
onErrorResume — abandon-and-switch; handleError is the continue form ·
tap — data-first side effect on a single value, not a stream