asyncMapSample<S> method
Like asyncMap but events are discarded while work is happening in
convert
.
If this stream is a broadcast stream the result will be as well.
When used with a broadcast stream behavior also differs from asyncMap in
that the convert
function is only called once per event, rather than
once per listener per event.
If no work is happening when an event is emitted it will be immediately
passed to convert
. If there is ongoing work when an event is emitted it
will be held until the work is finished. New events emitted will replace a
pending event.
Errors from this stream are forwarded directly to the result stream. Errors during the conversion are also forwarded to the result stream and are considered completing work so the next values are let through.
The result stream will not close until this stream closes and all pending conversions have finished.
Implementation
Stream<S> asyncMapSample<S>(Future<S> Function(T) convert) {
var workFinished = StreamController<void>()
// Let the first event through.
..add(null);
return aggregateSample(
trigger: workFinished.stream,
aggregate: _dropPrevious,
longPoll: true,
onEmpty: _ignore)
._asyncMapThen(convert, workFinished.add);
}