[英]Testing RxJS observable that tracks the last emitted value (no complete signal)?
[英]Javascript rxjs - do something when last value emitted from observable
以下情况:我进行了两个异步http调用。
为了简单起见:假设第一个调用返回了我一个名称列表: ['marta', 'edgar', 'david']
。 第二个http调用将名称发布到数据库中。 我的实现可以很好地满足此操作要求。 它看起来如下:
public deployAllPartners(): void {
this.isDeploying = true;
this.getAllPartner().subscribe(shortname => this.adminService.deploySinglePartnerForTesting(shortname).subscribe());
}
private getAllPartner(): Observable<string> {
return this.partnerService.getPartnersOverview()
.flatMap((partnerList) => partnerList.partner) <== returns an array
.map((partner) => partner.shortname);
}
现在我想要的是,在部署姓氏时,布尔值isDeploying
变为false。 部署短名称时是否触发任何RxJS运算符 ? 也许像finally()
东西? 为了完整起见:布尔值用于在HTML中加载gif,并且只有在isDeploying=true
时gif才显示在UI中,而当值为false时则隐藏。
如果要在源Observable完成后“结束”链,则可以使用concat
或者因为RxJS 6.2.0也可以使用新的endWith
运算符。
或者,如果您不想附加任何值而只是产生一些副作用,则可以在您的subscribe()
调用中使用complete
处理程序。
import { from, of } from 'rxjs';
import { concat } from 'rxjs/operators';
from(['a', 'b', 'c'])
.pipe(
concat(of('end')),
)
.subscribe({
next: console.log,
complete: () => console.log('completed'),
});
观看现场演示: https : //stackblitz.com/edit/rxjs6-demo-tplu6y?file=index.ts
对RXJS6使用finalize:
this.getAllPartner().
.pipe(
finalize(() => this.isDeploying = false)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.