window

签名: window(windowBoundaries: Observable): Observable

时间窗口值的 observable 。

示例

示例 1: 打开由内部 observable 指定的窗口

( StackBlitz |
jsBin |
jsFiddle )

  1. import { timer } from 'rxjs/observable/timer';
  2. import { window, scan, mergeAll } from 'rxjs/operators';
  3. // 立即发出值,然后每秒发出值
  4. const source = timer(0, 1000);
  5. const example = source.pipe(window(interval(3000)));
  6. const count = example.pipe(scan((acc, curr) => acc + 1, 0));
  7. /*
  8. "Window 1:"
  9. 0
  10. 1
  11. 2
  12. "Window 2:"
  13. 3
  14. 4
  15. 5
  16. ...
  17. */
  18. const subscribe = count.subscribe(val => console.log(`Window ${val}:`));
  19. const subscribeTwo = example
  20. .pipe(mergeAll())
  21. .subscribe(val => console.log(val));

其他资源


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