Unordered async guards

This commit is contained in:
Adina Țeudan 2024-08-07 21:30:41 +03:00
parent 85c6d91b2e
commit a8b5cfce14

View File

@ -86,3 +86,23 @@ export function orderedAsyncGuards(guards: Array<AsyncGuard>): CanActivateFn {
);
};
}
export function unorderedAsyncGuards(guards: Array<AsyncGuard>): CanActivateFn {
return async (route, state) => {
const injector = inject(Injector);
const loadingService = inject(LoadingService);
loadingService.start();
try {
const result = await Promise.all(guards.map(guard => runInInjectionContext(injector, () => guard(route, state))));
loadingService.stop();
return result.every(Boolean);
} catch (error) {
console.error(error);
loadingService.stop();
}
return false;
};
}