Skip to content

Commit

Permalink
refactor(router): organize deprecated loadChildren code (#47586)
Browse files Browse the repository at this point in the history
This commit breaks out the code in @angular/router needed to support the
deprecated `loadChildren` signature into a separate file, which reduces the
opportunity for conflicts when patching that behavior in g3.

PR Close #47586
  • Loading branch information
alxhub authored and AndrewKushnir committed Oct 4, 2022
1 parent 2919463 commit a2a066d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
2 changes: 1 addition & 1 deletion goldens/public-api/router/index.md
Expand Up @@ -356,7 +356,7 @@ export interface IsActiveMatchOptions {
}

// @public
export type LoadChildren = LoadChildrenCallback;
export type LoadChildren = LoadChildrenCallback | ɵDeprecatedLoadChildren;

// @public
export type LoadChildrenCallback = () => Type<any> | NgModuleFactory<any> | Routes | Observable<Type<any> | Routes> | Promise<NgModuleFactory<any> | Type<any> | Routes>;
Expand Down
26 changes: 26 additions & 0 deletions packages/router/src/deprecated_load_children.ts
@@ -0,0 +1,26 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {Injector, NgModuleFactory} from '@angular/core';
import {Observable} from 'rxjs';

// This file exists to support the legacy `loadChildren: string` behavior being patched back into
// Angular.

/**
* Deprecated `loadChildren` value types.
*
* @publicApi
* @deprecated represents the deprecated type side of `LoadChildren`.
*/
export type DeprecatedLoadChildren = never;

export function deprecatedLoadChildrenString(
injector: Injector, loadChildren: unknown): Observable<NgModuleFactory<any>>|null {
return null;
}
3 changes: 2 additions & 1 deletion packages/router/src/models.ts
Expand Up @@ -9,6 +9,7 @@
import {EnvironmentInjector, ImportedNgModuleProviders, InjectionToken, NgModuleFactory, Provider, ProviderToken, Type} from '@angular/core';
import {Observable} from 'rxjs';

import {DeprecatedLoadChildren} from './deprecated_load_children';
import {ActivatedRouteSnapshot, RouterStateSnapshot} from './router_state';
import {UrlSegment, UrlSegmentGroup, UrlTree} from './url_tree';

Expand Down Expand Up @@ -125,7 +126,7 @@ export type LoadChildrenCallback = () => Type<any>|NgModuleFactory<any>|Routes|
* @see `LoadChildrenCallback`
* @publicApi
*/
export type LoadChildren = LoadChildrenCallback;
export type LoadChildren = LoadChildrenCallback|DeprecatedLoadChildren;

/**
*
Expand Down
1 change: 1 addition & 0 deletions packages/router/src/private_export.ts
Expand Up @@ -8,6 +8,7 @@


export {ɵEmptyOutletComponent} from './components/empty_outlet';
export {DeprecatedLoadChildren as ɵDeprecatedLoadChildren} from './deprecated_load_children';
export {withPreloading as ɵwithPreloading} from './provide_router';
export {assignExtraOptionsToRouter as ɵassignExtraOptionsToRouter, RestoredState as ɵRestoredState} from './router';
export {ROUTER_PROVIDERS as ɵROUTER_PROVIDERS} from './router_module';
Expand Down
10 changes: 8 additions & 2 deletions packages/router/src/router_config_loader.ts
Expand Up @@ -10,7 +10,8 @@ import {Compiler, EnvironmentInjector, Injectable, InjectFlags, InjectionToken,
import {ConnectableObservable, from, Observable, of, Subject} from 'rxjs';
import {catchError, finalize, map, mergeMap, refCount, tap} from 'rxjs/operators';

import {LoadChildren, LoadedRouterConfig, Route, Routes} from './models';
import {deprecatedLoadChildrenString} from './deprecated_load_children';
import {LoadChildren, LoadChildrenCallback, LoadedRouterConfig, Route, Routes} from './models';
import {flatten, wrapIntoObservable} from './utils/collection';
import {assertStandalone, standardizeConfig, validateConfig} from './utils/config';

Expand Down Expand Up @@ -122,7 +123,12 @@ export class RouterConfigLoader {

private loadModuleFactoryOrRoutes(loadChildren: LoadChildren):
Observable<NgModuleFactory<any>|Routes> {
return wrapIntoObservable(loadChildren()).pipe(mergeMap((t) => {
const deprecatedResult = deprecatedLoadChildrenString(this.injector, loadChildren);
if (deprecatedResult) {
return deprecatedResult;
}

return wrapIntoObservable((loadChildren as LoadChildrenCallback)()).pipe(mergeMap((t) => {
if (t instanceof NgModuleFactory || Array.isArray(t)) {
return of(t);
} else {
Expand Down

0 comments on commit a2a066d

Please sign in to comment.