UNPKG

@rxap/life-cycle

Version:

Provides an Angular service and guard to manage application lifecycle. It allows to execute code when the application is stable and ready. Includes a guard to prevent route activation until the app is ready.

75 lines (69 loc) 3.33 kB
import * as i0 from '@angular/core'; import { ApplicationRef, Injectable, Inject } from '@angular/core'; import { isPromiseLike } from '@rxap/utilities'; import { BehaviorSubject, firstValueFrom, isObservable, of } from 'rxjs'; import { tap, filter, take, switchMap } from 'rxjs/operators'; class LifeCycleService { static { this.hooks = new Map(); } constructor(appRef) { this.appRef = appRef; this.isReady$ = new BehaviorSubject(false); LifeCycleService.AddHook('stable', firstValueFrom(appRef.isStable.pipe(tap(isStable => console.log('app state:', isStable ? 'stable' : 'unstable')), filter(Boolean), take(1), tap(() => console.log('app is ready'))))); Promise.all(Array.from(LifeCycleService.hooks.values()).map(hook => hook.promise)) .then(() => this.isReady$.next(true)); } static AddHook(name, promise) { LifeCycleService.hooks.set(name, { promise }); } whenReady(thenOrFunction) { return this.isReady$.pipe(filter(Boolean), take(1), switchMap(() => { if (typeof thenOrFunction === 'function') { const then = thenOrFunction(); if (isPromiseLike(then) || isObservable(then)) { return then; } else { return of(then); } } else { return thenOrFunction; } })); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.1.3", ngImport: i0, type: LifeCycleService, deps: [{ token: ApplicationRef }], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.1.3", ngImport: i0, type: LifeCycleService, providedIn: 'root' }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.1.3", ngImport: i0, type: LifeCycleService, decorators: [{ type: Injectable, args: [{ providedIn: 'root', }] }], ctorParameters: () => [{ type: i0.ApplicationRef, decorators: [{ type: Inject, args: [ApplicationRef] }] }] }); class IsAppReadyGuard { constructor(lifecycle) { this.lifecycle = lifecycle; } canActivate(next, state) { console.debug('[IsAppReadyGuard] can activate', state.url); return this.lifecycle.whenReady(() => true); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.1.3", ngImport: i0, type: IsAppReadyGuard, deps: [{ token: LifeCycleService }], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.1.3", ngImport: i0, type: IsAppReadyGuard, providedIn: 'root' }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.1.3", ngImport: i0, type: IsAppReadyGuard, decorators: [{ type: Injectable, args: [{ providedIn: 'root', }] }], ctorParameters: () => [{ type: LifeCycleService }] }); // region // endregion /** * Generated bundle index. Do not edit. */ export { IsAppReadyGuard, LifeCycleService }; //# sourceMappingURL=rxap-life-cycle.mjs.map