簡體   English   中英

angular2:如何消除Observable.combineLatest調用的抖動?

[英]angular2: How to debounce Observable.combineLatest calls?

我的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) => {
     ...
  });

在事實之后調用debounce是沒有用的,例如Observable.combineLatest(...)。debounceTime(300),因為CPU密集型任務發生在仍然經常被調用的CombineLatest回調內部。

我想我必須結合另一個Observable,但是我不確定該怎么做,有什么想法嗎?

combineLatest方法的project函數本質map是一個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]) => {
  ...
});

對於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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM