Skip to content

Commit

Permalink
refactor(core): implement runtime ɵɵgetComponentDepsFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
pmvald committed Jul 23, 2023
1 parent 1a45618 commit b42ed01
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 16 deletions.
25 changes: 19 additions & 6 deletions packages/core/src/render3/interfaces/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,21 +517,34 @@ export type PipeTypeList =
export const unusedValueExportToPlacateAjd = 1;

/**
* NgModule scope info as provided by NgModule decorator.
* NgModule scope info as provided by AoT compiler
*
* In full compilation Ivy resolved all the "module with providers" and forward refs the whole array
* if at least one element if forward refed. So we end up with type `Type<any>[]|(() =>
* Type<any>[])`.
*
* In local mode the compiler passes the raw info as they are to the runtimes as it is not possible
* to resolve them any further due to limited info at compile time. So we end up with type
* `RawScopeInfoFromDecorator[]`.
*/
export interface NgModuleScopeInfoFromDecorator {
/** List of components, directives, and pipes declared by this module. */
declarations?: Type<any>[]|(() => Type<any>[]);
declarations?: Type<any>[]|(() => Type<any>[])|RawScopeInfoFromDecorator[];

/** List of modules or `ModuleWithProviders` imported by this module. */
imports?: Type<any>[]|(() => Type<any>[]);
/** List of modules or standalone components imported by this module. */
imports?: Type<any>[]|(() => Type<any>[])|RawScopeInfoFromDecorator[];

/**
* List of modules, `ModuleWithProviders`, components, directives, or pipes exported by this
* List of modules, components, directives, or pipes exported by this
* module.
*/
exports?: Type<any>[]|(() => Type<any>[]);
exports?: Type<any>[]|(() => Type<any>[])|RawScopeInfoFromDecorator[];
}

/**
* The array element type passed to:
* - NgModule's annotation imports/exports/declarations fields
* - standalone component annotation imports field
*/
export type RawScopeInfoFromDecorator =
Type<any>|ModuleWithProviders<any>|(() => Type<any>)|(() => ModuleWithProviders<any>);
14 changes: 8 additions & 6 deletions packages/core/src/render3/local_compilation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
* found in the LICENSE file at https://angular.io/license
*/

import {Type} from '../interface/type';

import {DependencyTypeList, RawScopeInfoFromDecorator} from './interfaces/definition';
import {depsTracker} from './deps_tracker/deps_tracker';
import {ComponentType, DependencyTypeList, RawScopeInfoFromDecorator} from './interfaces/definition';

export function ɵɵgetComponentDepsFactory(
type: Type<any>, rawImports?: RawScopeInfoFromDecorator): () => DependencyTypeList {
// TODO(pmvald): Implement this runtime using deps tracker.
return () => [];
type: ComponentType<any>, rawImports?: RawScopeInfoFromDecorator[]): () => DependencyTypeList {
return () => {
console.log('>>>>> ɵɵgetComponentDepsFactory factory called', type);

return depsTracker.getComponentDependencies(type, rawImports).dependencies;
};
}
30 changes: 26 additions & 4 deletions packages/core/src/render3/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
* found in the LICENSE file at https://angular.io/license
*/

import {isForwardRef, resolveForwardRef} from '../di/forward_ref';
import {Type} from '../interface/type';
import {noSideEffects} from '../util/closure';
import {EMPTY_ARRAY} from '../util/empty';

import {extractDefListOrFactory, getNgModuleDef} from './definition';
import {ComponentDef, ComponentType, NgModuleScopeInfoFromDecorator} from './interfaces/definition';
import {depsTracker} from './deps_tracker/deps_tracker';
import {ComponentDef, ComponentType, NgModuleScopeInfoFromDecorator, RawScopeInfoFromDecorator} from './interfaces/definition';
import {isModuleWithProviders} from './jit/util';

/**
* Generated next to NgModules to monkey-patch directive and pipe references onto a component's
Expand Down Expand Up @@ -43,8 +46,27 @@ export function ɵɵsetComponentScope(
export function ɵɵsetNgModuleScope(type: any, scope: NgModuleScopeInfoFromDecorator): unknown {
return noSideEffects(() => {
const ngModuleDef = getNgModuleDef(type, true);
ngModuleDef.declarations = scope.declarations || EMPTY_ARRAY;
ngModuleDef.imports = scope.imports || EMPTY_ARRAY;
ngModuleDef.exports = scope.exports || EMPTY_ARRAY;
ngModuleDef.declarations = convertToTypeArray(scope.declarations || EMPTY_ARRAY);
ngModuleDef.imports = convertToTypeArray(scope.imports || EMPTY_ARRAY);
ngModuleDef.exports = convertToTypeArray(scope.exports || EMPTY_ARRAY);

depsTracker.registerNgModule(type, scope);
});
}

function convertToTypeArray(values: Type<any>[]|(() => Type<any>[])|
RawScopeInfoFromDecorator[]): Type<any>[]|(() => Type<any>[]) {
if (typeof values === 'function') {
return values;
}

if (values.some(isForwardRef)) {
return () => values.map(resolveForwardRef).map(maybeUnwrapModuleWithProviders);
} else {
return values.map(maybeUnwrapModuleWithProviders);
}
}

function maybeUnwrapModuleWithProviders(value: any): Type<any> {
return isModuleWithProviders(value) ? value.ngModule : value as Type<any>;
}

0 comments on commit b42ed01

Please sign in to comment.