refactor(ivy): generate ngFactoryDef for injectables (#32433)
With #31953 we moved the factories for components, directives and pipes into a new field called `ngFactoryDef`, however I decided not to do it for injectables, because they needed some extra logic. These changes set up the `ngFactoryDef` for injectables as well. For reference, the extra logic mentioned above is that for injectables we have two code paths: 1. For injectables that don't configure how they should be instantiated, we create a `factory` that proxies to `ngFactoryDef`: ``` // Source @Injectable() class Service {} // Output class Service { static ngInjectableDef = defineInjectable({ factory: () => Service.ngFactoryFn(), }); static ngFactoryFn: (t) => new (t || Service)(); } ``` 2. For injectables that do configure how they're created, we keep the `ngFactoryDef` and generate the factory based on the metadata: ``` // Source @Injectable({ useValue: DEFAULT_IMPL, }) class Service {} // Output export class Service { static ngInjectableDef = defineInjectable({ factory: () => DEFAULT_IMPL, }); static ngFactoryFn: (t) => new (t || Service)(); } ``` PR Close #32433
This commit is contained in:
@ -753,11 +753,11 @@ export function getBaseDef<T>(type: any): ɵɵBaseDef<T>|null {
|
||||
export function getFactoryDef<T>(type: any, throwNotFound: true): FactoryFn<T>;
|
||||
export function getFactoryDef<T>(type: any): FactoryFn<T>|null;
|
||||
export function getFactoryDef<T>(type: any, throwNotFound?: boolean): FactoryFn<T>|null {
|
||||
const factoryFn = type[NG_FACTORY_DEF] || null;
|
||||
if (!factoryFn && throwNotFound === true && ngDevMode) {
|
||||
const hasFactoryDef = type.hasOwnProperty(NG_FACTORY_DEF);
|
||||
if (!hasFactoryDef && throwNotFound === true && ngDevMode) {
|
||||
throw new Error(`Type ${stringify(type)} does not have 'ngFactoryDef' property.`);
|
||||
}
|
||||
return factoryFn;
|
||||
return hasFactoryDef ? type[NG_FACTORY_DEF] : null;
|
||||
}
|
||||
|
||||
export function getNgModuleDef<T>(type: any, throwNotFound: true): NgModuleDef<T>;
|
||||
|
Reference in New Issue
Block a user