【问题标题】:How to get previous result in observable with interval?如何通过间隔获得先前的结果?
【发布时间】:2020-12-23 22:01:17
【问题描述】:

我需要在 Pipe observable 中以间隔获取 observable 的前一个值。例子是:

const imageStatus = interval(5000)
this.statusSubscription = imageStatus.pipe(
  startWith(0),
  pairwise(),
  switchMap(() => this.settingsService.getImageStatus()),
  tap(res => {
    this.statusMessage = '';
    this.statusError = '';
    if (res.data.message) {
      this.statusError = ''
      this.statusMessage = res.data.message
    }
    if (res.data.error) {
      this.statusMessage = '';
      this.statusError = res.data.error
    }
  }),
  switchMap(res => {
    // here I need to cmpare prevRes and NextRes
    if (res.data.status !== 0) {
      return this.settingsService.getImageList()
    } else {
      return of({})
    }
  })
).subscribe(res => {
  if (res.data) {
    this.imageTableList = new MatTableDataSource(res.data)
  }
})

所以这里我需要知道之前区间迭代的getImageStatus() 的结果。我需要在第二个 switchMap 中比较以前的结果和新的结果

【问题讨论】:

    标签: angular rxjs observable


    【解决方案1】:

    “pairWise”需要放在switchMap之后,有些像

    interval(5000).pipe(
       startWith(0),
       switchMap(_=>this.settingsService.getImageStatus()),
       tap(res=>{..if you want to do something with the response of getImageStatus...}),
       pairwise(),
       switchMap(([old,value])=>{
          ..here you has in old the before value
          ..and in value the response actual..
          return ....
       })
    ).subscribe(res=>{
       ...
    })
    

    【讨论】:

      猜你喜欢
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多