parent
f76ce84ae1
commit
2fd4c372d5
|
@ -357,20 +357,19 @@ function providerToRecord(provider: SingleProvider): Record<any> {
|
||||||
* @param provider provider to convert to factory
|
* @param provider provider to convert to factory
|
||||||
*/
|
*/
|
||||||
export function providerToFactory(provider: SingleProvider): () => any {
|
export function providerToFactory(provider: SingleProvider): () => any {
|
||||||
let token = resolveForwardRef(provider);
|
|
||||||
let factory: (() => any)|undefined = undefined;
|
let factory: (() => any)|undefined = undefined;
|
||||||
if (isTypeProvider(provider)) {
|
if (isTypeProvider(provider)) {
|
||||||
return injectableDefFactory(provider);
|
return injectableDefFactory(resolveForwardRef(provider));
|
||||||
} else {
|
} else {
|
||||||
token = resolveForwardRef(provider.provide);
|
|
||||||
if (isValueProvider(provider)) {
|
if (isValueProvider(provider)) {
|
||||||
factory = () => provider.useValue;
|
factory = () => resolveForwardRef(provider.useValue);
|
||||||
} else if (isExistingProvider(provider)) {
|
} else if (isExistingProvider(provider)) {
|
||||||
factory = () => inject(provider.useExisting);
|
factory = () => inject(resolveForwardRef(provider.useExisting));
|
||||||
} else if (isFactoryProvider(provider)) {
|
} else if (isFactoryProvider(provider)) {
|
||||||
factory = () => provider.useFactory(...injectArgs(provider.deps || []));
|
factory = () => provider.useFactory(...injectArgs(provider.deps || []));
|
||||||
} else {
|
} else {
|
||||||
const classRef = (provider as StaticClassProvider | ClassProvider).useClass || token;
|
const classRef = resolveForwardRef(
|
||||||
|
(provider as StaticClassProvider | ClassProvider).useClass || provider.provide);
|
||||||
if (hasDeps(provider)) {
|
if (hasDeps(provider)) {
|
||||||
factory = () => new (classRef)(...injectArgs(provider.deps));
|
factory = () => new (classRef)(...injectArgs(provider.deps));
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -708,7 +708,8 @@ describe('providers', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('forwardRef', (done) => {
|
describe('forwardRef', () => {
|
||||||
|
it('forwardRef resolves later', (done) => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
expectProvidersScenario({
|
expectProvidersScenario({
|
||||||
parent: {
|
parent: {
|
||||||
|
@ -719,11 +720,44 @@ describe('providers', () => {
|
||||||
});
|
});
|
||||||
done();
|
done();
|
||||||
}, 0);
|
}, 0);
|
||||||
|
});
|
||||||
|
|
||||||
class ForLater {}
|
class ForLater {}
|
||||||
|
|
||||||
|
// The following test that forwardRefs are called, so we don't search for an anon fn
|
||||||
|
it('ValueProvider wrapped in forwardRef', () => {
|
||||||
|
expectProvidersScenario({
|
||||||
|
parent: {
|
||||||
|
providers:
|
||||||
|
[{provide: GREETER, useValue: forwardRef(() => { return {greet: 'Value'}; })}],
|
||||||
|
componentAssertion: () => { expect(directiveInject(GREETER).greet).toEqual('Value'); }
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('ClassProvider wrapped in forwardRef', () => {
|
||||||
|
expectProvidersScenario({
|
||||||
|
parent: {
|
||||||
|
providers: [{provide: GREETER, useClass: forwardRef(() => GreeterClass)}],
|
||||||
|
componentAssertion: () => { expect(directiveInject(GREETER).greet).toEqual('Class'); }
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('ExistingProvider wrapped in forwardRef', () => {
|
||||||
|
expectProvidersScenario({
|
||||||
|
parent: {
|
||||||
|
providers:
|
||||||
|
[GreeterClass, {provide: GREETER, useExisting: forwardRef(() => GreeterClass)}],
|
||||||
|
componentAssertion: () => { expect(directiveInject(GREETER).greet).toEqual('Class'); }
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* All tests below assume this structure:
|
* All tests below assume this structure:
|
||||||
* ```
|
* ```
|
||||||
|
|
Loading…
Reference in New Issue