Skip to content

Commit

Permalink
Implements #633 and #576 (#719)
Browse files Browse the repository at this point in the history
* Implements 633 and 576

* Preparing release
  • Loading branch information
remojansen authored Dec 17, 2017
1 parent 7897f45 commit 581c31f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "inversify",
"version": "4.7.0",
"version": "4.8.0",
"description": "A powerful and lightweight inversion of control container for JavaScript and Node.js apps powered by TypeScript.",
"main": "lib/inversify.js",
"jsnext:main": "es/inversify.js",
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ namespace interfaces {
guid: string;
container: Container;
plan: Plan;
currentRequest: Request;
addPlan(plan: Plan): void;
setCurrentRequest(request: Request): void;
}

export interface ReflectResult {
Expand Down
9 changes: 8 additions & 1 deletion src/planning/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@ class Context implements interfaces.Context {
public guid: string;
public container: interfaces.Container;
public plan: interfaces.Plan;
public currentRequest: interfaces.Request;

public constructor(container: interfaces.Container) {
public constructor(
container: interfaces.Container) {
this.guid = guid();
this.container = container;
}

public addPlan(plan: interfaces.Plan) {
this.plan = plan;
}

public setCurrentRequest(currentRequest: interfaces.Request) {
this.currentRequest = currentRequest;
}

}

export { Context };
2 changes: 2 additions & 0 deletions src/resolution/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const invokeFactory = (
const _resolveRequest = (requestScope: interfaces.RequestScope) =>
(request: interfaces.Request): any => {

request.parentContext.setCurrentRequest(request);

const bindings = request.bindings;
const childRequests = request.childRequests;

Expand Down
36 changes: 36 additions & 0 deletions test/bugs/issue_633.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { expect } from "chai";
import { Container, injectable } from "../../src/inversify";

describe("Issue 633", () => {

it("Should expose metadata through context", () => {

@injectable()
class Logger {
public named: string;
public constructor(named: string) {
this.named = named;
}
}

const container = new Container();

const TYPE = {
Logger: Symbol.for("Logger")
};

container.bind<Logger>(TYPE.Logger).toDynamicValue((context) => {
const namedMetadata = context.currentRequest.target.getNamedTag();
const named = namedMetadata ? namedMetadata.value : "default";
return new Logger(named);
});

const logger1 = container.getNamed<Logger>(TYPE.Logger, "Name1");
const logger2 = container.getNamed<Logger>(TYPE.Logger, "Name2");

expect(logger1.named).to.eq("Name1");
expect(logger2.named).to.eq("Name2");

});

});

0 comments on commit 581c31f

Please sign in to comment.