fix(ivy): error when using forwardRef in Injectable's useClass (#30532)

Fixes Ivy throwing an error when something is passed in as a `forwardRef` into `@Injectable`'s `useClass` option. The error was being thrown, because we were trying to get the provider factory off of the wrapper function, rather than the value itself.

This PR resolves FW-1335.

PR Close #30532
This commit is contained in:
Kristiyan Kostadinov
2019-05-18 10:10:19 +02:00
committed by Andrew Kushnir
parent 4da805243a
commit 40a0666651
6 changed files with 80 additions and 7 deletions

View File

@ -57,11 +57,11 @@ export function forwardRef(forwardRefFn: ForwardRefFn): Type<any> {
* @publicApi
*/
export function resolveForwardRef<T>(type: T): T {
const fn: any = type;
if (typeof fn === 'function' && fn.hasOwnProperty(__forward_ref__) &&
fn.__forward_ref__ === forwardRef) {
return fn();
} else {
return type;
}
return isForwardRef(type) ? type() : type;
}
/** Checks whether a function is wrapped by a `forwardRef`. */
export function isForwardRef(fn: any): fn is() => any {
return typeof fn === 'function' && fn.hasOwnProperty(__forward_ref__) &&
fn.__forward_ref__ === forwardRef;
}

View File

@ -7,6 +7,7 @@
*/
import {Type} from '../../interface/type';
import {isForwardRef, resolveForwardRef} from '../forward_ref';
import {ɵɵinject} from '../injector_compatibility';
import {getInjectableDef, getInjectorDef, ɵɵdefineInjectable, ɵɵdefineInjector} from '../interface/defs';
@ -26,6 +27,14 @@ export const angularCoreDiEnv: {[name: string]: Function} = {
function getFactoryOf<T>(type: Type<any>): ((type?: Type<T>) => T)|null {
const typeAny = type as any;
if (isForwardRef(type)) {
return (() => {
const factory = getFactoryOf<T>(resolveForwardRef(typeAny));
return factory ? factory() : null;
}) as any;
}
const def = getInjectableDef<T>(typeAny) || getInjectorDef<T>(typeAny);
if (!def || def.factory === undefined) {
return null;