Skip to content

Commit 79b5531

Browse files
author
Umed Khudoiberdiev
committed
fixed issue #53, version bump
1 parent 6876e60 commit 79b5531

File tree

4 files changed

+67
-5
lines changed

4 files changed

+67
-5
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typedi",
3-
"version": "0.7.0",
3+
"version": "0.7.1",
44
"description": "Dependency injection for TypeScript",
55
"license": "MIT",
66
"readmeFilename": "README.md",

src/ContainerInstance.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,19 @@ export class ContainerInstance {
9494

9595
const globalContainer = Container.of(undefined);
9696
let service = globalContainer.findService(identifier);
97-
if ((!service || service.global !== true) && globalContainer !== this)
98-
service = this.findService(identifier);
97+
let scopedService = this.findService(identifier);
98+
99+
if (service && service.global === true)
100+
return this.getServiceValue(identifier, service);
101+
102+
if (scopedService)
103+
return this.getServiceValue(identifier, scopedService);
104+
105+
if (service && this !== globalContainer) {
106+
const clonedService = Object.assign({}, service);
107+
clonedService.value = undefined;
108+
return this.getServiceValue(identifier, clonedService);
109+
}
99110

100111
return this.getServiceValue(identifier, service);
101112
}

src/decorators/Service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ export function Service<T, K extends keyof T>(optionsOrServiceName?: ServiceOpti
3636
if (typeof optionsOrServiceName === "string" || optionsOrServiceName instanceof Token) {
3737
service.id = optionsOrServiceName;
3838
service.multiple = (optionsOrServiceName as ServiceOptions<T, K>).multiple;
39-
service.global = true;
39+
service.global = (optionsOrServiceName as ServiceOptions<T, K>).global || false;
4040
service.transient = (optionsOrServiceName as ServiceOptions<T, K>).transient;
4141

4242
} else if (optionsOrServiceName) { // ServiceOptions
4343
service.id = (optionsOrServiceName as ServiceOptions<T, K>).id;
4444
service.factory = (optionsOrServiceName as ServiceOptions<T, K>).factory;
4545
service.multiple = (optionsOrServiceName as ServiceOptions<T, K>).multiple;
46-
service.global = true;
46+
service.global = (optionsOrServiceName as ServiceOptions<T, K>).global || false;
4747
service.transient = (optionsOrServiceName as ServiceOptions<T, K>).transient;
4848
}
4949

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import "reflect-metadata";
2+
import {Container} from "../../../src/Container";
3+
import {Service} from "../../../src/decorators/Service";
4+
import {Token} from "../../../src";
5+
6+
describe("github issues > #53 Token-based services are cached in the Global container even when fetched via a subcontainer", function() {
7+
8+
beforeEach(() => Container.reset());
9+
10+
it("should work properly", function() {
11+
12+
@Service()
13+
class QuestionRepository {
14+
userName: string;
15+
16+
save() {
17+
// console.log(`saving question. author is ${this.userName}`);
18+
}
19+
20+
}
21+
22+
const QuestionController = new Token<QuestionControllerImpl>("QCImpl");
23+
24+
@Service({ id: QuestionController })
25+
class QuestionControllerImpl {
26+
27+
constructor(protected questionRepository: QuestionRepository) {
28+
}
29+
30+
save(name: string) {
31+
if (name)
32+
this.questionRepository.userName = name;
33+
this.questionRepository.save();
34+
}
35+
}
36+
37+
const request1 = { param: "Timber" };
38+
const controller1 = Container.of(request1).get(QuestionController);
39+
controller1.save("Timber");
40+
Container.reset(request1);
41+
42+
const request2 = { param: "Guest" };
43+
const controller2 = Container.of(request2).get(QuestionController);
44+
controller2.save("");
45+
Container.reset(request2);
46+
47+
controller1.should.not.be.equal(controller2);
48+
controller1.should.not.be.equal(Container.get(QuestionController));
49+
});
50+
51+
});

0 commit comments

Comments
 (0)