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

Function ɵɵgetInheritedFactory

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

Source from the content-addressed store, hash-verified

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