Issue
async goToAuthorizePage(): Promise<void> {
const loader: HTMLIonLoadingElement = await this.loadingService.presentLoader();
this.pickUpParcelService.pickUpParcelChanged$.pipe(take(1)).subscribe(res => {
// code
this.pickUpParcelService.setPickUpParcel(pickUpParcel); // This line again emit the value
this.router.navigateByUrl('authorize-and-sign');
}, () => this.loadingService.dismissLoader(loader)); // never end or fire
}
pick-up-parcel.service.ts
private pickUpParcelSubject$: BehaviorSubject<Partial<PickUpParcelModel>> =
new BehaviorSubject<Partial<PickUpParcelModel>>(null);
pickUpParcelChanged$: Observable<Partial<PickUpParcelModel>> =
this.pickUpParcelSubject$.asObservable();
constructor(private afs: AngularFirestore) { }
setPickUpParcel(pickUpParcel: Partial<PickUpParcelModel>): void {
this.pickUpParcelSubject$.next(pickUpParcel);
}
With my code above where I cannot close the loader. I can do that if I'll put that within the subscription. But then I need to repeat it again on error use case too. That is why I would like to put it in a single place like a completion event (i.e. DRY principal). Even though I have used pipe(take(1)
why it doesn't complete? Do I have a better approach to follow here?
Solution
Try this one as third function is of complete one
async goToAuthorizePage(): Promise<void> {
const loader: HTMLIonLoadingElement = await this.loadingService.presentLoader();
this.pickUpParcelService.pickUpParcelChanged$.pipe(take(1)).subscribe(res => {
// 1st block
// your code
}, (errorBlock) => {} , 2nd block
(completeblock) => this.loadingService.dismissLoader(loader)); 3rd block
}
or you can try finalize rxjs operator in pipe
this.pickUpParcelService.pickUpParcelChanged$.pipe(take(1) , finalize(() => this.loadingService.dismissLoader(loader))).subscribe(res => {
// your code
})
Answered By - LogicBlower
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.