I am trying to create function which wraps given Observable and adds loading screen to it.
The function function wrapWithLoadingScreen<T>(obs$: Observable<T>): Observable<T> should work like :
- show loading screen.
- execute observable received as parameter
- hide loading screen after all values are emitted.
My initial idea of implementation was:
function wrapWithLoadingScreen<T>(obs$: Observable<T>): Observable<T> {
return of(null).pipe(
tap(() => console.log("show loading screen")),
switchMap(() => obs$),
finalize(() => console.log("hide loading screen"))
);
}
but when i chain other operators to result of this function then "hide loading screen" is executed after those chains (not after original observable finishing).
Here is an example: https://stackblitz.com/edit/rxjs-wrapping-observable
the result in console of above is
show loading screen
im reducing
im reducing
reducing finished so loading screen should be hidden
the result is 6
hide loading screen
what im aiming for should be
show loading screen
im reducing
im reducing
hide loading screen
reducing finished so loading screen should be hidden
the result is 6