Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* Remove default exports

* Upgraded tslint

* Implemented #408

* Implementes #419

* fixes #405

* release 3.0.0-beta.2
  • Loading branch information
remojansen authored Nov 25, 2016
1 parent 573a951 commit 757340d
Show file tree
Hide file tree
Showing 23 changed files with 134 additions and 150 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ dts
lib
temp
es
amd

type_definitions/inversify/*.js

Expand Down
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"test/**/*.js": true,
"**/es": true,
"**/lib": true,
"**/amd": true,
"**/dts": true,
"**/temp": true,
"**/coverage": true,
Expand Down
28 changes: 24 additions & 4 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ gulp.task("build-lib", function() {
.js.pipe(gulp.dest("lib/"));
});

var tsAmdProject = tsc.createProject("tsconfig.json", { module : "amd", typescript: require("typescript") });

gulp.task("build-amd", function() {
return gulp.src([
"src/**/*.ts"
])
.pipe(tsAmdProject())
.on("error", function (err) {
process.exit(1);
})
.js.pipe(gulp.dest("amd/"));
});

var tsEsProject = tsc.createProject("tsconfig.json", { module : "es2015", typescript: require("typescript") });

gulp.task("build-es", function() {
Expand Down Expand Up @@ -185,10 +198,17 @@ if (process.env.APPVEYOR) {
//* DEFAULT
//******************************************************************************
gulp.task("build", function(cb) {
runSequence(
"lint",
["build-src", "build-es", "build-lib", "build-dts"], // tests + build es and lib
"build-test", cb);
runSequence(
"lint",
[
"build-src",
"build-es",
"build-lib",
"build-amd",
"build-dts"
],
"build-test", cb
);
});

gulp.task("default", function (cb) {
Expand Down
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": "3.0.0-beta.1",
"version": "3.0.0-beta.2",
"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: 1 addition & 1 deletion src/constants/error_msgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const NULL_ARGUMENT = "NULL argument";
export const KEY_NOT_FOUND = "Key Not Found";
export const AMBIGUOUS_MATCH = "Ambiguous match found for serviceIdentifier:";
export const CANNOT_UNBIND = "Could not unbind serviceIdentifier:";
export const NOT_REGISTERED = "No bindings found for serviceIdentifier:";
export const NOT_REGISTERED = "No matching bindings found for serviceIdentifier:";
export const MISSING_INJECTABLE_ANNOTATION = "Missing required @injectable annotation in:";
export const MISSING_INJECT_ANNOTATION = "Missing required @inject or @multiInject annotation in:";
export const CIRCULAR_DEPENDENCY = "Circular dependency found:";
Expand Down
24 changes: 19 additions & 5 deletions src/container/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ class Container implements interfaces.Container {
this._middleware = snapshot.middleware;
}

public createChild(): Container {
let child = new Container();
child.parent = this;
return child;
}

public set parent (container: interfaces.Container) {
this._parentContainer = container;
}
Expand All @@ -164,11 +170,11 @@ class Container implements interfaces.Container {
// The runtime identifier must be associated with only one binding
// use getAll when the runtime identifier is associated with multiple bindings
public get<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>): T {
return this._get<T>(false, TargetType.Variable, serviceIdentifier) as T;
return this._get<T>(false, false, TargetType.Variable, serviceIdentifier) as T;
}

public getTagged<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>, key: string, value: any): T {
return this._get<T>(false, TargetType.Variable, serviceIdentifier, key, value) as T;
return this._get<T>(false, false, TargetType.Variable, serviceIdentifier, key, value) as T;
}

public getNamed<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>, named: string): T {
Expand All @@ -178,11 +184,11 @@ class Container implements interfaces.Container {
// Resolves a dependency by its runtime identifier
// The runtime identifier can be associated with one or multiple bindings
public getAll<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>): T[] {
return this._get<T>(true, TargetType.Variable, serviceIdentifier) as T[];
return this._get<T>(true, true, TargetType.Variable, serviceIdentifier) as T[];
}

public getAllTagged<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>, key: string, value: any): T[] {
return this._get<T>(true, TargetType.Variable, serviceIdentifier, key, value) as T[];
return this._get<T>(false, true, TargetType.Variable, serviceIdentifier, key, value) as T[];
}

public getAllNamed<T>(serviceIdentifier: interfaces.ServiceIdentifier<T>, named: string): T[] {
Expand All @@ -193,6 +199,7 @@ class Container implements interfaces.Container {
// delegates resolution to _middleware if available
// otherwise it delegates resoltion to _planAndResolve
private _get<T>(
avoidConstraints: boolean,
isMultiInject: boolean,
targetType: TargetType,
serviceIdentifier: interfaces.ServiceIdentifier<any>,
Expand All @@ -203,6 +210,7 @@ class Container implements interfaces.Container {
let result: (T|T[]) = null;

let args: interfaces.NextArgs = {
avoidConstraints: avoidConstraints,
contextInterceptor: (context: interfaces.Context) => { return context; },
isMultiInject: isMultiInject,
key: key,
Expand All @@ -229,7 +237,13 @@ class Container implements interfaces.Container {
private _planAndResolve<T>(): (args: interfaces.NextArgs) => (T|T[]) {
return (args: interfaces.NextArgs) => {
let context = plan(
this, args.isMultiInject, args.targetType, args.serviceIdentifier, args.key, args.value
this,
args.isMultiInject,
args.targetType,
args.serviceIdentifier,
args.key,
args.value,
args.avoidConstraints
);
let result = resolve<T>(args.contextInterceptor(context));
return result;
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ namespace interfaces {
}

export interface NextArgs {
avoidConstraints: boolean;
contextInterceptor?: (contexts: Context) => Context;
isMultiInject: boolean;
targetType: number;
Expand Down Expand Up @@ -147,6 +148,7 @@ namespace interfaces {
applyMiddleware(...middleware: Middleware[]): void;
snapshot(): void;
restore(): void;
createChild(): Container;
}

export interface Bind extends Function {
Expand Down
17 changes: 10 additions & 7 deletions src/planning/planner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function _createTarget(
}

function _getActiveBindings(
avoidConstraints: boolean,
context: interfaces.Context,
parentRequest: interfaces.Request,
target: interfaces.Target
Expand All @@ -48,8 +49,8 @@ function _getActiveBindings(
let bindings = getBindings<any>(context.container, target.serviceIdentifier);
let activeBindings: interfaces.Binding<any>[] = [];

// multiple bindings available but not a multi-injection
if (bindings.length > 1) {
// multiple bindings available
if (bindings.length > 1 && avoidConstraints === false) {

// apply constraints if available to reduce the number of active bindings
activeBindings = bindings.filter((binding) => {
Expand Down Expand Up @@ -113,6 +114,7 @@ function _validateActiveBindingCount(
}

function _createSubRequests(
avoidConstraints: boolean,
serviceIdentifier: interfaces.ServiceIdentifier<any>,
context: interfaces.Context,
parentRequest: interfaces.Request,
Expand All @@ -126,7 +128,7 @@ function _createSubRequests(

if (parentRequest === null) {

activeBindings = _getActiveBindings(context, null, target);
activeBindings = _getActiveBindings(avoidConstraints, context, null, target);

childRequest = new Request(
serviceIdentifier,
Expand All @@ -140,7 +142,7 @@ function _createSubRequests(
context.addPlan(plan);

} else {
activeBindings = _getActiveBindings(context, parentRequest, target);
activeBindings = _getActiveBindings(avoidConstraints, context, parentRequest, target);
childRequest = parentRequest.addChildRequest(target.serviceIdentifier, activeBindings, target);
}

Expand All @@ -159,7 +161,7 @@ function _createSubRequests(
let dependencies = getDependencies(binding.implementationType);

dependencies.forEach((dependency: interfaces.Target) => {
_createSubRequests(dependency.serviceIdentifier, context, subChildRequest, dependency);
_createSubRequests(false, dependency.serviceIdentifier, context, subChildRequest, dependency);
});

}
Expand Down Expand Up @@ -203,12 +205,13 @@ function plan(
targetType: TargetType,
serviceIdentifier: interfaces.ServiceIdentifier<any>,
key?: string,
value?: any
value?: any,
avoidConstraints = false
): interfaces.Context {

let context = new Context(container);
let target = _createTarget(isMultiInject, targetType, serviceIdentifier, "", key, value);
_createSubRequests(serviceIdentifier, context, null, target);
_createSubRequests(avoidConstraints, serviceIdentifier, context, null, target);
return context;

}
Expand Down
15 changes: 0 additions & 15 deletions test/annotation/inject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,6 @@ interface Shuriken {}
class Katana implements Katana {}
class Shuriken implements Shuriken {}

class WarriorWithoutDecorator {

private _primaryWeapon: Katana;
private _secondaryWeapon: Shuriken;

constructor(
primary: Katana,
secondary: Shuriken
) {

this._primaryWeapon = primary;
this._secondaryWeapon = secondary;
}
}

class DecoratedWarrior {

private _primaryWeapon: Katana;
Expand Down
16 changes: 0 additions & 16 deletions test/annotation/multi_inject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,6 @@ import * as METADATA_KEY from "../../src/constants/metadata_keys";
import * as ERROR_MSGS from "../../src/constants/error_msgs";

interface Weapon {}
class Katana implements Weapon {}
class Shuriken implements Weapon {}

class WarriorWithoutDecorator {

private _primaryWeapon: Weapon;
private _secondaryWeapon: Weapon;

constructor(
weapons: Weapon[]
) {

this._primaryWeapon = weapons[0];
this._secondaryWeapon = weapons[1];
}
}

class DecoratedWarrior {

Expand Down
2 changes: 0 additions & 2 deletions test/annotation/named.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import * as METADATA_KEY from "../../src/constants/metadata_keys";
import * as ERROR_MSGS from "../../src/constants/error_msgs";

interface Weapon {}
class Katana implements Weapon {}
class Shuriken implements Weapon {}

class NamedWarrior {

Expand Down
16 changes: 0 additions & 16 deletions test/annotation/tagged.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,6 @@ import * as METADATA_KEY from "../../src/constants/metadata_keys";
import * as ERRORS_MSGS from "../../src/constants/error_msgs";

interface Weapon {}
class Katana implements Weapon {}
class Shuriken implements Weapon {}

class UnTaggedWarrior {

private _primaryWeapon: Weapon;
private _secondaryWeapon: Weapon;

constructor(
primary: Weapon,
secondary: Weapon) {

this._primaryWeapon = primary;
this._secondaryWeapon = secondary;
}
}

class TaggedWarrior {

Expand Down
57 changes: 57 additions & 0 deletions test/bugs/bugs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
multiInject
} from "../../src/inversify";
import * as ERROR_MSGS from "../../src/constants/error_msgs";
import * as METADATA_KEY from "../../src/constants/metadata_keys";

describe("Bugs", () => {

Expand Down Expand Up @@ -422,4 +423,60 @@ describe("Bugs", () => {

});

it("Should be able to get all the available bindings for a service identifier", () => {

const controllerId = "SomeControllerID";
const tagA = "A";
const tagB = "B";

interface Controller {
name: string;
}

let container = new Container();

@injectable()
class AppController implements Controller {
public name: string;
public constructor() {
this.name = "AppController";
}
}

@injectable()
class AppController2 implements Controller {
public name: string;
public constructor() {
this.name = "AppController2";
}
}

container.bind(controllerId).to(AppController).whenTargetNamed(tagA);
container.bind(controllerId).to(AppController2).whenTargetNamed(tagB);

function wrongNamedBinding() { container.getAllNamed<Controller>(controllerId, "Wrong"); }
expect(wrongNamedBinding).to.throw();

let appControllerNamedRight = container.getAllNamed<Controller>(controllerId, tagA);
expect(appControllerNamedRight.length).to.eql(1, "getAllNamed");
expect(appControllerNamedRight[0].name).to.eql("AppController");

function wrongTaggedBinding() { container.getAllTagged<Controller>(controllerId, "Wrong", "Wrong"); }
expect(wrongTaggedBinding).to.throw();

let appControllerTaggedRight = container.getAllTagged<Controller>(controllerId, METADATA_KEY.NAMED_TAG, tagB);
expect(appControllerTaggedRight.length).to.eql(1, "getAllTagged");
expect(appControllerTaggedRight[0].name).to.eql("AppController2");

let getAppController = () => {
let matches = container.getAll<Controller>(controllerId);
expect(matches.length).to.eql(2);
expect(matches[0].name).to.eql("AppController");
expect(matches[1].name).to.eql("AppController2");
};

expect(getAppController).not.to.throw();

});

});
6 changes: 6 additions & 0 deletions test/container/container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,5 +550,11 @@ describe("Container", () => {

});

it("Should be able create a child containers", () => {
let parent = new Container();
let child = parent.createChild();
expect(child.parent.guid).to.eql(parent.guid);
});


});
2 changes: 0 additions & 2 deletions test/inversify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2653,8 +2653,6 @@ describe("InversifyJS", () => {

it("Should display a error when injecting into an abstract class", () => {

interface Weapon { }

@injectable()
class Soldier extends Stubs.BaseSoldier { }

Expand Down
Loading

0 comments on commit 757340d

Please sign in to comment.