windowWhen

签名: windowWhen(closingSelector: function(): Observable): Observable

在提供的时间帧处关闭窗口,并发出从源 observable 中收集的值的 observable 。

windowWhen - 图1

示例

示例 1: 根据定时器开启和关闭窗口

( 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. // 立即发出值,然后每秒发出值
  5. const source = timer(0, 1000);
  6. const example = source.pipe(
  7. // 每5秒关闭窗口并发出从源 observable 中收集的值的 observable
  8. windowWhen(() => interval(5000)),
  9. tap(_ => console.log('NEW WINDOW!'))
  10. );
  11. const subscribeTwo = example
  12. .pipe(
  13. // 窗口发出嵌套的 observable
  14. mergeAll()
  15. /*
  16. 输出:
  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));

其他资源


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