windowToggle

签名: windowToggle(openings: Observable, closingSelector: function(value): Observable): Observable

以 openings 发出为起始,以 closingSelector 发出为结束,收集并发出源 observable 中的值的 observable 。

windowToggle - 图1

示例

示例 1: 根据渐增的定时器开关窗口

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

其他资源


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