@tsed/di
Advanced tools
Comparing version 5.25.1 to 5.26.0
@@ -43,2 +43,3 @@ import { IProvider } from "../interfaces/IProvider"; | ||
getProviders(type?: ProviderType | string): Provider<any>[]; | ||
addProviders(container: Map<TokenProvider, Provider<any>>): void; | ||
} |
@@ -59,2 +59,9 @@ "use strict"; | ||
} | ||
addProviders(container) { | ||
container.forEach(provider => { | ||
if (!this.hasProvider(provider.provide)) { | ||
this.setProvider(provider.provide, provider.clone()); | ||
} | ||
}); | ||
} | ||
} | ||
@@ -61,0 +68,0 @@ exports.Container = Container; |
@@ -162,3 +162,3 @@ import { Container } from "../class/Container"; | ||
*/ | ||
private _invoke; | ||
private resolve; | ||
/** | ||
@@ -165,0 +165,0 @@ * Create options to invoke a provider or class. |
@@ -61,3 +61,3 @@ "use strict"; | ||
if (!instance) { | ||
instance = /* await */ this.invoke(token); | ||
instance = this.invoke(token); | ||
} | ||
@@ -131,3 +131,3 @@ provider.instance = instance; | ||
else if (!provider || options.rebuild) { | ||
instance = /* await */ this._invoke(token, locals, options); | ||
instance = this.resolve(token, locals, options); | ||
} | ||
@@ -138,3 +138,3 @@ else { | ||
if (!this.has(token)) { | ||
provider.instance = /* await */ this._invoke(token, locals, options); | ||
provider.instance = this.resolve(token, locals, options); | ||
} | ||
@@ -144,7 +144,7 @@ instance = this.get(token); | ||
case interfaces_1.ProviderScope.REQUEST: | ||
instance = /* await */ this._invoke(token, locals, options); | ||
instance = this.resolve(token, locals, options); | ||
locals.set(token, instance); | ||
break; | ||
case interfaces_1.ProviderScope.INSTANCE: | ||
instance = /* await */ this._invoke(provider.provide, locals, options); | ||
instance = this.resolve(provider.provide, locals, options); | ||
break; | ||
@@ -167,11 +167,6 @@ } | ||
// Clone all providers in the container | ||
container.forEach((provider, token) => { | ||
if (!this.hasProvider(token)) { | ||
this.setProvider(token, provider.clone()); | ||
} | ||
}); | ||
this.addProviders(container); | ||
const providers = _super.toArray.call(this); | ||
for (const provider of providers) { | ||
if (!locals.has(provider.provide) && this.scopeOf(provider) === interfaces_1.ProviderScope.SINGLETON) { | ||
/* await */ | ||
this.invoke(provider.provide, locals); | ||
@@ -332,3 +327,3 @@ } | ||
*/ | ||
_invoke(target, locals, options = {}) { | ||
resolve(target, locals, options = {}) { | ||
const { token, deps, construct, isBindable } = this.mapInvokeOptions(target, options); | ||
@@ -349,3 +344,3 @@ const provider = this.getProvider(target); | ||
for (const dependency of deps) { | ||
const service = /* await */ this.invoke(dependency, locals, { parent: token }); | ||
const service = this.invoke(dependency, locals, { parent: token }); | ||
services.push(service); | ||
@@ -352,0 +347,0 @@ } |
{ | ||
"name": "@tsed/di", | ||
"version": "5.25.1", | ||
"version": "5.26.0", | ||
"description": "DI module for Ts.ED Framework", | ||
@@ -11,3 +11,3 @@ "main": "lib/index.js", | ||
"peerDependencies": { | ||
"@tsed/core": "5.25.1" | ||
"@tsed/core": "5.26.0" | ||
}, | ||
@@ -28,3 +28,3 @@ "devDependencies": {}, | ||
"license": "MIT", | ||
"gitHead": "6b3d3ba33f77f61a047d102d7db1c33a8031b130" | ||
"gitHead": "1a3d7061b79bc71c94739c2b501b0cec4e0cb322" | ||
} |
@@ -68,2 +68,10 @@ import {IProvider} from "../interfaces/IProvider"; | ||
} | ||
public addProviders(container: Map<TokenProvider, Provider<any>>) { | ||
container.forEach(provider => { | ||
if (!this.hasProvider(provider.provide)) { | ||
this.setProvider(provider.provide, provider.clone()); | ||
} | ||
}); | ||
} | ||
} |
@@ -87,7 +87,7 @@ import {deepClone, getClass, getClassOrSymbol, isFunction, Metadata, nameOf, prototypeOf, Store} from "@tsed/core"; | ||
*/ | ||
public /* async */ forkProvider(token: TokenProvider, instance?: any): Provider<any> { | ||
public forkProvider(token: TokenProvider, instance?: any): Provider<any> { | ||
const provider = this.addProvider(token).getProvider(token)!; | ||
if (!instance) { | ||
instance = /* await */ this.invoke(token); | ||
instance = this.invoke(token); | ||
} | ||
@@ -160,3 +160,3 @@ | ||
*/ | ||
public /* async */ invoke<T>( | ||
public invoke<T>( | ||
token: TokenProvider, | ||
@@ -172,3 +172,3 @@ locals: Map<TokenProvider, any> = new LocalsContainer(), | ||
} else if (!provider || options.rebuild) { | ||
instance = /* await */ this._invoke(token, locals, options); | ||
instance = this.resolve(token, locals, options); | ||
} else { | ||
@@ -178,3 +178,3 @@ switch (this.scopeOf(provider)) { | ||
if (!this.has(token)) { | ||
provider.instance = /* await */ this._invoke(token, locals, options); | ||
provider.instance = this.resolve(token, locals, options); | ||
} | ||
@@ -186,3 +186,3 @@ | ||
case ProviderScope.REQUEST: | ||
instance = /* await */ this._invoke(token, locals, options); | ||
instance = this.resolve(token, locals, options); | ||
locals.set(token, instance); | ||
@@ -192,3 +192,3 @@ break; | ||
case ProviderScope.INSTANCE: | ||
instance = /* await */ this._invoke(provider.provide, locals, options); | ||
instance = this.resolve(provider.provide, locals, options); | ||
break; | ||
@@ -210,7 +210,3 @@ } | ||
// Clone all providers in the container | ||
container.forEach((provider, token) => { | ||
if (!this.hasProvider(token)) { | ||
this.setProvider(token, provider.clone()); | ||
} | ||
}); | ||
this.addProviders(container); | ||
@@ -221,3 +217,2 @@ const providers = super.toArray(); | ||
if (!locals.has(provider.provide) && this.scopeOf(provider) === ProviderScope.SINGLETON) { | ||
/* await */ | ||
this.invoke(provider.provide, locals); | ||
@@ -411,7 +406,3 @@ } | ||
*/ | ||
private /* async */ _invoke<T>( | ||
target: TokenProvider, | ||
locals: Map<TokenProvider, any>, | ||
options: Partial<IInvokeOptions<T>> = {} | ||
): Promise<T> { | ||
private resolve<T>(target: TokenProvider, locals: Map<TokenProvider, any>, options: Partial<IInvokeOptions<T>> = {}): Promise<T> { | ||
const {token, deps, construct, isBindable} = this.mapInvokeOptions(target, options); | ||
@@ -436,3 +427,3 @@ const provider = this.getProvider(target); | ||
for (const dependency of deps) { | ||
const service = /* await */ this.invoke(dependency, locals, {parent: token}); | ||
const service = this.invoke(dependency, locals, {parent: token}); | ||
services.push(service); | ||
@@ -439,0 +430,0 @@ } |
@@ -72,2 +72,19 @@ import {expect} from "chai"; | ||
}); | ||
describe("addProviders()", () => { | ||
it("should add providers", () => { | ||
class Test { | ||
} | ||
// GIVEN | ||
const container = new Container(); | ||
const childContainer = new Container(); | ||
childContainer.addProvider(Test); | ||
// WHEN | ||
container.addProviders(childContainer); | ||
// THEN | ||
container.getProvider(Test)!.should.instanceof(Provider); | ||
}); | ||
}); | ||
}); |
@@ -92,3 +92,3 @@ import {Store} from "@tsed/core"; | ||
Sinon.spy(injector as any, "_invoke"); | ||
Sinon.spy(injector as any, "resolve"); | ||
Sinon.spy(injector as any, "invoke"); | ||
@@ -102,4 +102,4 @@ Sinon.spy(injector, "get"); | ||
const result1 = await injector.invoke(token, locals); | ||
const result2 = await injector.invoke(token, locals, {rebuild: true}); | ||
const result1: any = await injector.invoke(token, locals); | ||
const result2: any = await injector.invoke(token, locals, {rebuild: true}); | ||
@@ -110,3 +110,3 @@ // THEN | ||
injector.get.should.have.been.calledWithExactly(token); | ||
(injector as any)._invoke.should.have.been.calledWithExactly(token, locals, {rebuild: true}); | ||
(injector as any).resolve.should.have.been.calledWithExactly(token, locals, {rebuild: true}); | ||
(injector as any).invoke.should.have.been.calledWithExactly(InjectorService, locals, { | ||
@@ -131,3 +131,3 @@ parent: token | ||
Sinon.spy(injector as any, "_invoke"); | ||
Sinon.spy(injector as any, "resolve"); | ||
Sinon.spy(injector, "get"); | ||
@@ -140,4 +140,4 @@ Sinon.spy(injector, "getProvider"); | ||
const result1 = await injector.invoke(token, locals); | ||
const result2 = await injector.invoke(token, locals); | ||
const result1: any = await injector.invoke(token, locals); | ||
const result2: any = await injector.invoke(token, locals); | ||
@@ -149,3 +149,3 @@ // THEN | ||
return (injector as any)._invoke.should.not.have.been.called; | ||
return (injector as any).resolve.should.not.have.been.called; | ||
}); | ||
@@ -167,3 +167,3 @@ }); | ||
Sinon.spy(injector as any, "_invoke"); | ||
Sinon.spy(injector as any, "resolve"); | ||
Sinon.spy(injector, "get"); | ||
@@ -176,7 +176,7 @@ Sinon.spy(injector, "getProvider"); | ||
// WHEN REQ1 | ||
const result1 = await injector.invoke(token, locals); | ||
const result2 = await injector.invoke(token, locals); | ||
const result1: any = await injector.invoke(token, locals); | ||
const result2: any = await injector.invoke(token, locals); | ||
// WHEN REQ2 | ||
const result3 = await injector.invoke(token, locals2); | ||
const result3: any = await injector.invoke(token, locals2); | ||
@@ -188,3 +188,3 @@ // THEN | ||
injector.getProvider.should.have.been.calledWithExactly(token); | ||
(injector as any)._invoke.should.have.been.calledWithExactly(token, locals, {}); | ||
(injector as any).resolve.should.have.been.calledWithExactly(token, locals, {}); | ||
locals.get(token).should.eq(result1); | ||
@@ -210,3 +210,3 @@ locals2.get(token).should.eq(result3); | ||
Sinon.spy(injector as any, "_invoke"); | ||
Sinon.spy(injector as any, "resolve"); | ||
Sinon.spy(injector, "get"); | ||
@@ -218,4 +218,4 @@ Sinon.spy(injector, "getProvider"); | ||
// WHEN REQ1 | ||
const result1 = await injector.invoke(token, locals); | ||
const result2 = await injector.invoke(token, locals); | ||
const result1: any = await injector.invoke(token, locals); | ||
const result2: any = await injector.invoke(token, locals); | ||
@@ -226,3 +226,3 @@ // THEN | ||
injector.getProvider.should.have.been.calledWithExactly(token); | ||
(injector as any)._invoke.should.have.been.calledWithExactly(token, locals, {}); | ||
(injector as any).resolve.should.have.been.calledWithExactly(token, locals, {}); | ||
locals.has(token).should.eq(false); | ||
@@ -260,3 +260,3 @@ | ||
// WHEN | ||
const result = await injector.invoke(token); | ||
const result: any = await injector.invoke(token); | ||
@@ -283,3 +283,3 @@ // THEN | ||
// WHEN | ||
const result = await injector.invoke(token); | ||
const result: any = await injector.invoke(token); | ||
@@ -304,3 +304,3 @@ // THEN | ||
// WHEN | ||
const result = await injector.invoke(token); | ||
const result: any = await injector.invoke(token); | ||
@@ -326,3 +326,3 @@ // THEN | ||
// WHEN | ||
const result = await injector.invoke(token); | ||
const result: any = await injector.invoke(token); | ||
@@ -342,3 +342,3 @@ // THEN | ||
// WHEN | ||
const result = await injector.invoke(token); | ||
const result: any = await injector.invoke(token); | ||
@@ -345,0 +345,0 @@ // THEN |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
322115
5541