If you want to utilize rxjs and Observables in your Angular project, it's quite simple.
First, create a new BehaviorSubject called uploadPercent:
uploadPercent = new BehaviorSubject<number>(0);
Then, update the value of uploadPercent using:
uploadPercent.next(task.percentageChanges());
To monitor the progress and take action accordingly, set up a pipeline within the ngOnInit hook:
this.uploadPercent.pipe(
takeUntil(this.destroy$)
).subscribe(x => {
if (x >= 100) {
window.location.reload();
}
});
Create a Subject named destroy$ for cleanup purposes:
destroy$ = new Subject<void>();
Ensure to complete the subscription in ngOnDestroy method to avoid memory leaks:
ngOnDestroy() {
this.destroy$.next();
this.destroy$.complete();
}
For more information on rxjs, check out the official documentation:
https://rxjs.dev/guide/overview
You can also visit this website for additional resources:
https://www.learnrxjs.io/