sample method

Stream<T> sample(
  1. Stream<void> trigger,
  2. {bool longPoll = true}
)

Emits the most recent new value from this stream when trigger emits an event.

If longPoll is false, then an event on trigger when there is no pending source event will be ignored. If longPoll is true (the default), then an event on trigger when there is no pending source event will cause the next source event to immediately flow to the result stream.

If longPoll is false, if there is no pending source event when trigger emits, then the trigger event will be ignored.

If longPoll is true, and there are no buffered values when trigger emits one or more events, then the next value from this stream is immediately emitted on the returned stream as a single element list. Subsequent events on trigger while there have been no events on this stream are ignored.

The result stream will close as soon as there is a guarantee it will not emit any more events. There will not be any more events emitted if:

  • trigger is closed and there is no waiting long poll.
  • Or, this source stream is closed and any pending source event has been delivered.

If this source stream is a broadcast stream, the result will be as well. Errors from this source stream or the trigger are immediately forwarded to the output.

See also:

  • buffer which use trigger stream in the same way, but keeps a list of pending source events.

Implementation

Stream<T> sample(Stream<void> trigger, {bool longPoll = true}) =>
    aggregateSample(
        trigger: trigger,
        aggregate: _dropPrevious,
        longPoll: longPoll,
        onEmpty: _ignore);