bufferWhen

signature: bufferWhen(closingSelector: function): Observable

Collect all values until closing selector emits, emit buffered values.

bufferWhen - 图1

Examples

Example 1: Emit buffer based on interval

( StackBlitz | jsBin |
jsFiddle )

  1. import { interval } from 'rxjs/observable/interval';
  2. import { bufferWhen } from 'rxjs/operators';
  3. //emit value every 1 second
  4. const oneSecondInterval = interval(1000);
  5. //return an observable that emits value every 5 seconds
  6. const fiveSecondInterval = () => interval(5000);
  7. //every five seconds, emit buffered values
  8. const bufferWhenExample = oneSecondInterval.pipe(bufferWhen(fiveSecondInterval));
  9. //log values
  10. //ex. output: [0,1,2,3]...[4,5,6,7,8]
  11. const subscribe = bufferWhenExample.subscribe(val =>
  12. console.log('Emitted Buffer: ', val)
  13. );

Additional Resources


:file_folder: Source Code:
https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/bufferWhen.ts