Skip to content

Commit

Permalink
fixed issue #53, version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
Umed Khudoiberdiev committed Feb 27, 2018
1 parent 6876e60 commit 79b5531
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typedi",
"version": "0.7.0",
"version": "0.7.1",
"description": "Dependency injection for TypeScript",
"license": "MIT",
"readmeFilename": "README.md",
Expand Down
15 changes: 13 additions & 2 deletions src/ContainerInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,19 @@ export class ContainerInstance {

const globalContainer = Container.of(undefined);
let service = globalContainer.findService(identifier);
if ((!service || service.global !== true) && globalContainer !== this)
service = this.findService(identifier);
let scopedService = this.findService(identifier);

if (service && service.global === true)
return this.getServiceValue(identifier, service);

if (scopedService)
return this.getServiceValue(identifier, scopedService);

if (service && this !== globalContainer) {
const clonedService = Object.assign({}, service);
clonedService.value = undefined;
return this.getServiceValue(identifier, clonedService);
}

return this.getServiceValue(identifier, service);
}
Expand Down
4 changes: 2 additions & 2 deletions src/decorators/Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ export function Service<T, K extends keyof T>(optionsOrServiceName?: ServiceOpti
if (typeof optionsOrServiceName === "string" || optionsOrServiceName instanceof Token) {
service.id = optionsOrServiceName;
service.multiple = (optionsOrServiceName as ServiceOptions<T, K>).multiple;
service.global = true;
service.global = (optionsOrServiceName as ServiceOptions<T, K>).global || false;
service.transient = (optionsOrServiceName as ServiceOptions<T, K>).transient;

} else if (optionsOrServiceName) { // ServiceOptions
service.id = (optionsOrServiceName as ServiceOptions<T, K>).id;
service.factory = (optionsOrServiceName as ServiceOptions<T, K>).factory;
service.multiple = (optionsOrServiceName as ServiceOptions<T, K>).multiple;
service.global = true;
service.global = (optionsOrServiceName as ServiceOptions<T, K>).global || false;
service.transient = (optionsOrServiceName as ServiceOptions<T, K>).transient;
}

Expand Down
51 changes: 51 additions & 0 deletions test/github-issues/53/issue-53.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import "reflect-metadata";
import {Container} from "../../../src/Container";
import {Service} from "../../../src/decorators/Service";
import {Token} from "../../../src";

describe("github issues > #53 Token-based services are cached in the Global container even when fetched via a subcontainer", function() {

beforeEach(() => Container.reset());

it("should work properly", function() {

@Service()
class QuestionRepository {
userName: string;

save() {
// console.log(`saving question. author is ${this.userName}`);
}

}

const QuestionController = new Token<QuestionControllerImpl>("QCImpl");

@Service({ id: QuestionController })
class QuestionControllerImpl {

constructor(protected questionRepository: QuestionRepository) {
}

save(name: string) {
if (name)
this.questionRepository.userName = name;
this.questionRepository.save();
}
}

const request1 = { param: "Timber" };
const controller1 = Container.of(request1).get(QuestionController);
controller1.save("Timber");
Container.reset(request1);

const request2 = { param: "Guest" };
const controller2 = Container.of(request2).get(QuestionController);
controller2.save("");
Container.reset(request2);

controller1.should.not.be.equal(controller2);
controller1.should.not.be.equal(Container.get(QuestionController));
});

});

0 comments on commit 79b5531

Please sign in to comment.