MCPcopy Create free account
hub / github.com/angular/angular / ɵɵgetInheritedFactory

Function ɵɵgetInheritedFactory

packages/core/src/render3/di.ts:921–952  ·  view source on GitHub ↗
(
  type: Type<any> | AbstractType<any>,
)

Source from the content-addressed store, hash-verified

919 * @codeGenApi
920 */
921export function ɵɵgetInheritedFactory<T>(
922 type: Type<any> | AbstractType<any>,
923): (type: Type<T>) => T {
924 return noSideEffects(() => {
925 const ownConstructor = type.prototype.constructor;
926 const ownFactory = ownConstructor[NG_FACTORY_DEF] || getFactoryOf(ownConstructor);
927 const objectPrototype = Object.prototype;
928 let parent = Object.getPrototypeOf(type.prototype).constructor;
929
930 // Go up the prototype until we hit `Object`.
931 while (parent && parent !== objectPrototype) {
932 const factory = parent[NG_FACTORY_DEF] || getFactoryOf(parent);
933
934 // If we hit something that has a factory and the factory isn't the same as the type,
935 // we've found the inherited factory. Note the check that the factory isn't the type's
936 // own factory is redundant in most cases, but if the user has custom decorators on the
937 // class, this lookup will start one level down in the prototype chain, causing us to
938 // find the own factory first and potentially triggering an infinite loop downstream.
939 if (factory && factory !== ownFactory) {
940 return factory;
941 }
942
943 parent = Object.getPrototypeOf(parent);
944 }
945
946 // There is no factory defined. Either this was improper usage of inheritance
947 // (no Angular decorator on the superclass) or there is no constructor at all
948 // in the inheritance chain. Since the two cases cannot be distinguished, the
949 // latter has to be assumed.
950 return (t: Type<T>) => new t();
951 });
952}
953
954function getFactoryOf<T>(type: Type<any>): ((type?: Type<T>) => T | null) | null {
955 if (isForwardRef(type)) {

Callers 2

SayHelloPipeClass · 0.90
providers_spec.tsFile · 0.85

Calls 2

noSideEffectsFunction · 0.90
getFactoryOfFunction · 0.85

Tested by

no test coverage detected