windowWhen

signature: windowWhen(closingSelector: function(): Observable): Observable

Close window at provided time frame emitting observable of collected values from source.

windowWhen - 图1

Examples

Example 1: Open and close window at interval

( StackBlitz |
jsBin |
jsFiddle )

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

Additional Resources


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