如何通过间隔获得可观察的先前结果?
How to get previous result in observable with interval?
我需要获取 Pipe observable with interval 中 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
中比较之前的结果和新结果
“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=>{
...
})
我需要获取 Pipe observable with interval 中 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
“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=>{
...
})