简体   繁体   中英

Observable combineLatest with interval/timer

I'm trying to use .combineLatest in conjunction with a set interval so that a new value is emitted every 5 seconds. I am then passing this.dailyStocks as an input into a child component but the data being passed into the input is the same every time and when I look at my network tab in dev tools, I do not see the additional calls being made.

  .flatMap(() => {
    return Observable.interval(5000).combineLatest(
      this.http.getData(args),
      this.http.getOtherData(args)
    )
  })
  .subscribe(res => {
    this.dailyStocks = res[1];
    this.dailyStocks = this.dailyStocks.slice(0)
  }

Child Component:

  @Input('dailyStocks')
  set dailyStocks(val: ListSymbolObj[]) {
    this._dailyStocks.next(val);
  }

  get dailyStocks() {
    return this._dailyStocks.getValue();
  }

  ngOnInit() {
    this._dailyStocks
          .filter(x => x != undefined)
          .take(1)
          .subscribe(res => {
            //res is same
          }
    }

To make request on every value from interval you should write it like this:

Observable.interval(5000).switchMap(() =>
  Observable.combineLatest(
    this.http.getData(args),
    this.http.getOtherData(args)      
  )
)

Currently, your code not making additional requests because it just combines together values from all three Observables. It is equivalent to the following code:

Observable.combineLatest(
  Observable.interval(5000),
  this.http.getData(args),
  this.http.getOtherData(args)      
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM