windowToggle

signature: windowToggle(openings: Observable, closingSelector: function(value): Observable): Observable

Collect and emit observable of values from source between opening and closing emission.

windowToggle - 图1

Examples

Example 1: Toggle window at increasing interval

( StackBlitz |
jsBin |
jsFiddle )

  1. import { interval } from 'rxjs/observable/interval';
  2. import { timer } from 'rxjs/observable/timer';
  3. import { tap, windowToggle, mergeAll } from 'rxjs/operators';
  4. //emit immediately then every 1s
  5. const source = timer(0, 1000);
  6. //toggle window on every 5
  7. const toggle = interval(5000);
  8. const example = source.pipe(
  9. //turn window on every 5s
  10. windowToggle(toggle, val => interval(val * 1000)),
  11. tap(_ => console.log('NEW WINDOW!'))
  12. );
  13. const subscribeTwo = example
  14. .pipe(
  15. //window emits nested observable
  16. mergeAll()
  17. /*
  18. output:
  19. "NEW WINDOW!"
  20. 5
  21. "NEW WINDOW!"
  22. 10
  23. 11
  24. "NEW WINDOW!"
  25. 15
  26. 16
  27. "NEW WINDOW!"
  28. 20
  29. 21
  30. 22
  31. */
  32. )
  33. .subscribe(val => console.log(val));

Additional Resources


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