[英]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.