MCPcopy
hub / github.com/angular/angular / ɵɵgetInheritedFactory

Function ɵɵgetInheritedFactory

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

Source from the content-addressed store, hash-verified

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

Used in the wild real call sites across dependent graphs

searching dependent graphs…