Skip to content

Commit

Permalink
refactor(core): do not publish ɵɵgetFactoryOf() (#41040)
Browse files Browse the repository at this point in the history
Previously, `ɵɵgetFactoryOf()` was "privately" published from
`@angular/core` since in the past it was assumed that this
might be an instruction generated by the compiler.

This is not currently the case, so this commit removes it from
the private exports and renames it to indicate that it is a local
helper function.

PR Close #41040
  • Loading branch information
petebacondarwin authored and AndrewKushnir committed Mar 4, 2021
1 parent 09d50fb commit 94d4dee
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 28 deletions.
1 change: 0 additions & 1 deletion packages/core/src/core_render3_private_export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ export {
ɵɵenableBindings,
ɵɵFactoryDef,
ɵɵgetCurrentView,
ɵɵgetFactoryOf,
ɵɵgetInheritedFactory,
ɵɵhostProperty,
ɵɵi18n,
Expand Down
43 changes: 20 additions & 23 deletions packages/core/src/render3/di.ts
Original file line number Diff line number Diff line change
Expand Up @@ -698,40 +698,19 @@ export class NodeInjector implements Injector {
}
}

/**
* @codeGenApi
*/
export function ɵɵgetFactoryOf<T>(type: Type<any>): FactoryFn<T>|null {
const typeAny = type as any;

if (isForwardRef(type)) {
return (() => {
const factory = ɵɵgetFactoryOf<T>(resolveForwardRef(typeAny));
return factory ? factory() : null;
}) as any;
}

let factory = getFactoryDef<T>(typeAny);
if (factory === null) {
const injectorDef = getInjectorDef<T>(typeAny);
factory = injectorDef && injectorDef.factory;
}
return factory || null;
}

/**
* @codeGenApi
*/
export function ɵɵgetInheritedFactory<T>(type: Type<any>): (type: Type<T>) => T {
return noSideEffects(() => {
const ownConstructor = type.prototype.constructor;
const ownFactory = ownConstructor[NG_FACTORY_DEF] || ɵɵgetFactoryOf(ownConstructor);
const ownFactory = ownConstructor[NG_FACTORY_DEF] || getFactoryOf(ownConstructor);
const objectPrototype = Object.prototype;
let parent = Object.getPrototypeOf(type.prototype).constructor;

// Go up the prototype until we hit `Object`.
while (parent && parent !== objectPrototype) {
const factory = parent[NG_FACTORY_DEF] || ɵɵgetFactoryOf(parent);
const factory = parent[NG_FACTORY_DEF] || getFactoryOf(parent);

// If we hit something that has a factory and the factory isn't the same as the type,
// we've found the inherited factory. Note the check that the factory isn't the type's
Expand All @@ -752,3 +731,21 @@ export function ɵɵgetInheritedFactory<T>(type: Type<any>): (type: Type<T>) =>
return t => new t();
});
}

function getFactoryOf<T>(type: Type<any>): FactoryFn<T>|null {
const typeAny = type as any;

if (isForwardRef(type)) {
return (() => {
const factory = getFactoryOf<T>(resolveForwardRef(typeAny));
return factory ? factory() : null;
}) as any;
}

let factory = getFactoryDef<T>(typeAny);
if (factory === null) {
const injectorDef = getInjectorDef<T>(typeAny);
factory = injectorDef && injectorDef.factory;
}
return factory || null;
}
2 changes: 1 addition & 1 deletion packages/core/src/render3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {getComponent, getDirectives, getHostElement, getRenderedText} from './ut

export {NgModuleType} from '../metadata/ng_module_def';
export {ComponentFactory, ComponentFactoryResolver, ComponentRef, injectComponentFactoryResolver} from './component_ref';
export {ɵɵgetFactoryOf, ɵɵgetInheritedFactory} from './di';
export {ɵɵgetInheritedFactory} from './di';
export {getLocaleId, setLocaleId} from './i18n/i18n_locale_id';
// clang-format off
export {
Expand Down
6 changes: 3 additions & 3 deletions packages/core/test/bundling/forms/bundle.golden_symbols.json
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,9 @@
{
"name": "getFactoryDef"
},
{
"name": "getFactoryOf"
},
{
"name": "getFirstLContainer"
},
Expand Down Expand Up @@ -1721,9 +1724,6 @@
{
"name": "ɵɵelementStart"
},
{
"name": "ɵɵgetFactoryOf"
},
{
"name": "ɵɵgetInheritedFactory"
},
Expand Down

0 comments on commit 94d4dee

Please sign in to comment.