bufferToggle

signature: bufferToggle(openings: Observable, closingSelector: Function): Observable

Toggle on to catch emitted values from source, toggle off to emit buffered values as array.

bufferToggle - 图1

Examples

Example 1: Toggle buffer on and off at interval

( StackBlitz | jsBin |
jsFiddle )

  1. import { interval } from 'rxjs/observable/interval';
  2. import { bufferToggle } from 'rxjs/operators';
  3. //emit value every second
  4. const sourceInterval = interval(1000);
  5. //start first buffer after 5s, and every 5s after
  6. const startInterval = interval(5000);
  7. //emit value after 3s, closing corresponding buffer
  8. const closingInterval = val => {
  9. console.log(`Value ${val} emitted, starting buffer! Closing in 3s!`);
  10. return interval(3000);
  11. };
  12. //every 5s a new buffer will start, collecting emitted values for 3s then emitting buffered values
  13. const bufferToggleInterval = sourceInterval.pipe(
  14. bufferToggle(
  15. startInterval,
  16. closingInterval
  17. )
  18. );
  19. //log to console
  20. //ex. emitted buffers [4,5,6]...[9,10,11]
  21. const subscribe = bufferToggleInterval.subscribe(val =>
  22. console.log('Emitted Buffer:', val)
  23. );

Additional Resources


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