windowCount

签名: windowCount(windowSize: number, startWindowEvery: number): Observable

源 observable 中的值的 observable,每次发出N个值(N由参数决定)。

windowCount - 图1

示例

示例 1: 每发出x个项就开启一个新窗口

( StackBlitz |
jsBin |
jsFiddle )

  1. import { interval } from 'rxjs/observable/interval';
  2. import { windowCount, mergeAll, tap } from 'rxjs/operators';
  3. // 每1秒发出值
  4. const source = interval(1000);
  5. const example = source.pipe(
  6. // 每发出4个值就开启新窗口
  7. windowCount(4),
  8. tap(_ => console.log('NEW WINDOW!'))
  9. );
  10. const subscribeTwo = example
  11. .pipe(
  12. // 窗口发出嵌套的 observable
  13. mergeAll()
  14. /*
  15. 输出:
  16. "NEW WINDOW!"
  17. 0
  18. 1
  19. 2
  20. 3
  21. "NEW WINDOW!"
  22. 4
  23. 5
  24. 6
  25. 7
  26. */
  27. )
  28. .subscribe(val => console.log(val));

其他资源


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