Add loading indicator to orderedAsyncGuards

This commit is contained in:
Adina Țeudan 2024-02-10 11:13:15 +02:00
parent 0885ad776d
commit 67485fdac2

View File

@ -3,6 +3,7 @@ import { inject, Injectable, InjectionToken, Injector, runInInjectionContext } f
import { concatMap, firstValueFrom, from, last, of, takeWhile } from 'rxjs';
import { LoadingService } from '../loading';
import { SkeletonService } from './skeleton.service';
import { tap } from 'rxjs/operators';
@Injectable({
providedIn: 'root',
@ -68,8 +69,10 @@ export type AsyncGuard = (route: ActivatedRouteSnapshot, state: RouterStateSnaps
export function orderedAsyncGuards(guards: Array<AsyncGuard>): CanActivateFn {
return (route, state) => {
const injector = inject(Injector);
const loadingService = inject(LoadingService);
return from(guards).pipe(
tap(() => loadingService.start()),
// For each guard, fire canActivate and wait for it
// to complete.
concatMap(guard => runInInjectionContext(injector, () => guard(route, state))),
@ -78,6 +81,7 @@ export function orderedAsyncGuards(guards: Array<AsyncGuard>): CanActivateFn {
takeWhile(value => value === true, /* inclusive */ true),
// Return the last guard's result.
last(),
tap(() => loadingService.stop()),
);
};
}