Skip to content

Commit b1c2a8a

Browse files
arturovtatscott
authored andcommitted
refactor(router): replace APP_INITIALIZER (angular#60719)
The `APP_INITIALIZER` is deprecated. Replaced with `provideAppInitializer`. PR Close angular#60719
1 parent b8d9f95 commit b1c2a8a

File tree

2 files changed

+34
-47
lines changed

2 files changed

+34
-47
lines changed

goldens/public-api/router/index.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ export interface RouterFeature<FeatureKind extends RouterFeatureKind> {
789789
// (undocumented)
790790
ɵkind: FeatureKind;
791791
// (undocumented)
792-
ɵproviders: Provider[];
792+
ɵproviders: Array<Provider | EnvironmentProviders>;
793793
}
794794

795795
// @public

packages/router/src/provide_router.ts

Lines changed: 33 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
} from '@angular/common';
1515
import {
1616
APP_BOOTSTRAP_LISTENER,
17-
APP_INITIALIZER,
1817
ApplicationRef,
1918
ComponentRef,
2019
ENVIRONMENT_INITIALIZER,
@@ -24,6 +23,7 @@ import {
2423
Injector,
2524
makeEnvironmentProviders,
2625
NgZone,
26+
provideAppInitializer,
2727
Provider,
2828
runInInjectionContext,
2929
Type,
@@ -109,15 +109,15 @@ export function rootRoute(router: Router): ActivatedRoute {
109109
*/
110110
export interface RouterFeature<FeatureKind extends RouterFeatureKind> {
111111
ɵkind: FeatureKind;
112-
ɵproviders: Provider[];
112+
ɵproviders: Array<Provider | EnvironmentProviders>;
113113
}
114114

115115
/**
116116
* Helper function to create an object that represents a Router feature.
117117
*/
118118
function routerFeature<FeatureKind extends RouterFeatureKind>(
119119
kind: FeatureKind,
120-
providers: Provider[],
120+
providers: Array<Provider | EnvironmentProviders>,
121121
): RouterFeature<FeatureKind> {
122122
return {ɵkind: kind, ɵproviders: providers};
123123
}
@@ -348,40 +348,34 @@ export type InitialNavigationFeature =
348348
export function withEnabledBlockingInitialNavigation(): EnabledBlockingInitialNavigationFeature {
349349
const providers = [
350350
{provide: INITIAL_NAVIGATION, useValue: InitialNavigation.EnabledBlocking},
351-
{
352-
provide: APP_INITIALIZER,
353-
multi: true,
354-
deps: [Injector],
355-
useFactory: (injector: Injector) => {
356-
const locationInitialized: Promise<any> = injector.get(
357-
LOCATION_INITIALIZED,
358-
Promise.resolve(),
359-
);
360-
361-
return () => {
362-
return locationInitialized.then(() => {
363-
return new Promise((resolve) => {
364-
const router = injector.get(Router);
365-
const bootstrapDone = injector.get(BOOTSTRAP_DONE);
366-
afterNextNavigation(router, () => {
367-
// Unblock APP_INITIALIZER in case the initial navigation was canceled or errored
368-
// without a redirect.
369-
resolve(true);
370-
});
371-
372-
injector.get(NavigationTransitions).afterPreactivation = () => {
373-
// Unblock APP_INITIALIZER once we get to `afterPreactivation`. At this point, we
374-
// assume activation will complete successfully (even though this is not
375-
// guaranteed).
376-
resolve(true);
377-
return bootstrapDone.closed ? of(void 0) : bootstrapDone;
378-
};
379-
router.initialNavigation();
380-
});
351+
provideAppInitializer(() => {
352+
const injector = inject(Injector);
353+
const locationInitialized: Promise<any> = injector.get(
354+
LOCATION_INITIALIZED,
355+
Promise.resolve(),
356+
);
357+
358+
return locationInitialized.then(() => {
359+
return new Promise((resolve) => {
360+
const router = injector.get(Router);
361+
const bootstrapDone = injector.get(BOOTSTRAP_DONE);
362+
afterNextNavigation(router, () => {
363+
// Unblock APP_INITIALIZER in case the initial navigation was canceled or errored
364+
// without a redirect.
365+
resolve(true);
381366
});
382-
};
383-
},
384-
},
367+
368+
injector.get(NavigationTransitions).afterPreactivation = () => {
369+
// Unblock APP_INITIALIZER once we get to `afterPreactivation`. At this point, we
370+
// assume activation will complete successfully (even though this is not
371+
// guaranteed).
372+
resolve(true);
373+
return bootstrapDone.closed ? of(void 0) : bootstrapDone;
374+
};
375+
router.initialNavigation();
376+
});
377+
});
378+
}),
385379
];
386380
return routerFeature(RouterFeatureKind.EnabledBlockingInitialNavigationFeature, providers);
387381
}
@@ -426,16 +420,9 @@ export type DisabledInitialNavigationFeature =
426420
*/
427421
export function withDisabledInitialNavigation(): DisabledInitialNavigationFeature {
428422
const providers = [
429-
{
430-
provide: APP_INITIALIZER,
431-
multi: true,
432-
useFactory: () => {
433-
const router = inject(Router);
434-
return () => {
435-
router.setUpLocationChangeListener();
436-
};
437-
},
438-
},
423+
provideAppInitializer(() => {
424+
inject(Router).setUpLocationChangeListener();
425+
}),
439426
{provide: INITIAL_NAVIGATION, useValue: InitialNavigation.Disabled},
440427
];
441428
return routerFeature(RouterFeatureKind.DisabledInitialNavigationFeature, providers);

0 commit comments

Comments
 (0)