angular2:如何去除Observable.combine最新的电话?

时间:2016-12-14 21:18:22

标签: angular rxjs

我在angular2应用程序中遇到了性能问题,因为我有一个很大的Observable.combineLatest(),其中有许多输入快速变化,我想要去掉回调调用:

myData$ = Observable.combineLatest(
  this.store.let(fromRoot.getFoo),
  this.store.let(fromRoot.getBar),
  this.store.let(fromRoot.getFoobar),
  this.store.let(fromRoot.getBarfoo),
  (foo, bar, foobar, barfoo) => {
     ...
  });

事后调用去抖动,例如Observable.combineLatest(...)。debounceTime(300),没用,因为CPU密集型任务发生在仍然经常被调用的combineLatest回调中。

我想我必须结合另一个Observable,但我不知道该怎么做,有什么想法吗?

2 个答案:

答案 0 :(得分:8)

combineLatest方法project函数本质上是map运算符。你可以重新安排这样的事情:

myData$ = Observable.combineLatest(
  this.store.let(fromRoot.getFoo),
  this.store.let(fromRoot.getBar),
  this.store.let(fromRoot.getFoobar),
  this.store.let(fromRoot.getBarfoo)
)
.debounceTime(300)
.map(([foo, bar, foobar, barfoo]) => {
  ...
});

答案 1 :(得分:0)

对于rxjs> v6,您必须将rxjs管道函数与debounceTime运算符结合使用,例如

import {combineLatest, timer} from 'rxjs';
import {debounceTime} from 'rxjs/operators';

function testCombineLatest() {

  const startTime = Date.now();
  const timerOne$ = timer(1000, 1000);
  const timerTwo$ = timer(1300, 1000);

  combineLatest(timerOne$, timerTwo$)
    .pipe(debounceTime(600))
    .subscribe(([timer1, timer2]) => {
      console.log('TimeElapsed:', Date.now() - startTime);
      console.log('Timer Latest:', timer1, timer2);
    });
}
testCombineLatest();