Skip to content

Commit

Permalink
Merge pull request #97 from remojansen/master
Browse files Browse the repository at this point in the history
Implements Value, Constructor, Factory and Provider injections
  • Loading branch information
remojansen committed Mar 7, 2016
2 parents 305df5d + f26683f commit 9274479
Show file tree
Hide file tree
Showing 30 changed files with 793 additions and 102 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ typings

dist

src/*.js
src/**/*.js
test/*.js
test/**/*.js
type_definitions/*.js
11 changes: 5 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
language: node_js
node_js:
- stable
- 4.2.3
- 4.2.0
- 4.1.2
- 4.1.0
- 4.0.0
- 5.4.1
- 5.4.0
- 5.3.0
- 5.2.0
- 5.1.1
- '0.12'
- '0.10'
before_install:
- npm install -g typings
- typings install
20 changes: 10 additions & 10 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var gulp = require("gulp"),
buffer = require("vinyl-buffer"),
tslint = require("gulp-tslint"),
tsc = require("gulp-typescript"),
coveralls = require('gulp-coveralls'),
coveralls = require("gulp-coveralls"),
uglify = require("gulp-uglify"),
typedoc = require("gulp-typedoc"),
rename = require("gulp-rename"),
Expand Down Expand Up @@ -46,7 +46,7 @@ gulp.task("build-source", function() {
"node_modules/reflect-metadata/reflect-metadata.d.ts"
])
.pipe(tsc(tsProject))
.on('error', function (err) {
.on("error", function (err) {
process.exit(1);
})
.js.pipe(gulp.dest("src/"));
Expand All @@ -61,7 +61,7 @@ gulp.task("build-test", function() {
"node_modules/reflect-metadata/reflect-metadata.d.ts"
])
.pipe(tsc(tsTestProject))
.on('error', function (err) {
.on("error", function (err) {
process.exit(1);
})
.js.pipe(gulp.dest("test/"));
Expand All @@ -72,7 +72,7 @@ var tsTypeDefinitionsProject = tsc.createProject("tsconfig.json");
gulp.task("build-type-definitions", function() {
return gulp.src("type_definitions/**/*.ts")
.pipe(tsc(tsTypeDefinitionsProject))
.on('error', function (err) {
.on("error", function (err) {
process.exit(1);
})
.js.pipe(gulp.dest("type_definitions/"));
Expand Down Expand Up @@ -114,7 +114,7 @@ gulp.task("document", function () {
gulp.task("bundle", function () {

var b = browserify({
standalone : 'inversify',
standalone : "inversify",
entries: "src/inversify.js",
debug: true
});
Expand All @@ -131,15 +131,15 @@ gulp.task("bundle", function () {
//******************************************************************************
gulp.task("mocha", function() {
return gulp.src([
'node_modules/reflect-metadata/Reflect.js',
'test/**/*.test.js'
"node_modules/reflect-metadata/Reflect.js",
"test/**/*.test.js"
])
.pipe(mocha({ui: 'bdd'}))
.pipe(mocha({ui: "bdd"}))
.pipe(istanbul.writeReports());
});

gulp.task("istanbul:hook", function() {
return gulp.src(['src/**/*.js'])
return gulp.src(["src/**/*.js"])
// Covering files
.pipe(istanbul())
// Force `require` to return covered files
Expand Down Expand Up @@ -168,7 +168,7 @@ gulp.task("compress", function() {
return gulp.src("bundled/src/inversify.js")
.pipe(uglify({ preserveComments : false }))
.pipe(rename({
extname: '.min.js'
extname: ".min.js"
}))
.pipe(gulp.dest("dist/"))
});
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "inversify",
"version": "2.0.0-alpha.1",
"version": "2.0.0-alpha.2",
"description": "A lightweight IoC container written in TypeScript.",
"main": "dist/inversify.js",
"typings": "type_definitions/inversify-npm.d.ts",
Expand Down Expand Up @@ -32,10 +32,9 @@
},
"homepage": "http://inversify.io",
"engines": {},
"dependencies": {
"reflect-metadata": "^0.1.3"
},
"dependencies": {},
"devDependencies": {
"bluebird": "^3.3.3",
"browserify": "^13.0.0",
"chai": "^3.4.1",
"gulp": "^3.9.0",
Expand All @@ -50,6 +49,7 @@
"gulp-uglify": "^1.5.1",
"istanbul": "^0.4.2",
"mocha": "^2.3.4",
"reflect-metadata": "^0.1.3",
"run-sequence": "^1.1.5",
"sinon": "^1.17.3",
"tslint": "^3.2.2",
Expand Down
2 changes: 0 additions & 2 deletions src/activation/decorator_utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
///<reference path="../interfaces/interfaces.d.ts" />

import "reflect-metadata";

function tagParameter(target: any, targetKey: string, index: number, metadata: IMetadata) {
let metadataKey = "inversify:tagged";
let paramsMetadata: Object = null;
Expand Down
11 changes: 7 additions & 4 deletions src/bindings/binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Binding<T> implements IBinding<T> {
public runtimeIdentifier: string;

// The constructor of a class which must implement T
public implementationType: { new(): T; };
public implementationType: INewable<T>;

// Cache used to allow singleton scope and BindingType.Value bindings
public cache: T;
Expand All @@ -28,15 +28,18 @@ class Binding<T> implements IBinding<T> {
public type: BindingType;

// A factory method used in BindingType.Factory bindings
public factory: (context) => T;
public factory: IFactoryCreator<T>;

// An async factory method used in BindingType.Provider bindings
public provider: IProviderCreator<T>;

constructor(runtimeIdentifier: string) {
this.runtimeIdentifier = runtimeIdentifier;
this.type = BindingType.Instance;
this.scope = BindingScope.Transient;
this.type = BindingType.Invalid;
this.implementationType = null;
this.cache = null;
this.factory = null;
this.scope = BindingScope.Transient;
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/bindings/binding_type.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
///<reference path="../interfaces/interfaces.d.ts" />

enum BindingType {
Invalid,
Instance,
Value,
Constructor,
Factory
Factory,
Provider
}

export default BindingType;
3 changes: 2 additions & 1 deletion src/constants/error_msgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export const AMBIGUOUS_MATCH = "Ambiguous match found for service:";
export const CANNOT_UNBIND = "Could not unbind service:";
export const NOT_REGISTERED = "No bindigns found for service:";
export const CIRCULAR_DEPENDENCY = "Circular dependency found between services:";
export const NOT_IMPLEMENTED = "Sorry, this feature is not fully implemented yet";
export const NOT_IMPLEMENTED = "Sorry, this feature is not fully implemented yet.";
export const INVALID_BINDING_TYPE = "Invalid binding type:";
7 changes: 5 additions & 2 deletions src/interfaces/bindings/binding.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/// <reference path="../interfaces.d.ts" />

interface IBinding<T> {
runtimeIdentifier: string;
implementationType: { new(): T; };
factory: (context) => T;
implementationType: INewable<T>;
factory: IFactoryCreator<any>;
provider: IProviderCreator<any>;
cache: T;
scope: number; // BindingScope
type: number; // BindingType
Expand Down
5 changes: 5 additions & 0 deletions src/interfaces/bindings/factory.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference path="../interfaces.d.ts" />

interface IFactory<T> extends Function {
(): T;
}
5 changes: 5 additions & 0 deletions src/interfaces/bindings/factory_creator.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference path="../interfaces.d.ts" />

interface IFactoryCreator<T> extends Function {
(context: IContext): IFactory<T>;
}
5 changes: 5 additions & 0 deletions src/interfaces/bindings/newable.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference path="../interfaces.d.ts" />

interface INewable<T> {
new(...args: any[]): T;
}
5 changes: 5 additions & 0 deletions src/interfaces/bindings/provider.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference path="../interfaces.d.ts" />

interface IProvider<T> extends Function {
(): Promise<T>;
}
5 changes: 5 additions & 0 deletions src/interfaces/bindings/provider_creator.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference path="../interfaces.d.ts" />

interface IProviderCreator<T> extends Function {
(context: IContext): IProvider<T>;
}
22 changes: 17 additions & 5 deletions src/interfaces/interfaces.d.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@

// KERNEL
/// <reference path="./kernel/kernel.d.ts" />
/// <reference path="./kernel/kernel_module.d.ts" />
/// <reference path="./kernel/kernel_options.d.ts" />
/// <reference path="./kernel/key_value_pair.d.ts" />
/// <reference path="./kernel/lookup.d.ts" />

// PLANNING
/// <reference path="./planning/planner.d.ts" />
/// <reference path="./planning/plan.d.ts" />
/// <reference path="./planning/request.d.ts" />
/// <reference path="./planning/context.d.ts" />
/// <reference path="./planning/target.d.ts" />
/// <reference path="./planning/queryable_string.d.ts" />

// RESOLUTION
/// <reference path="./resolution/resolver.d.ts" />

// BINDINGS
/// <reference path="./bindings/binding.d.ts" />
/// <reference path="./bindings/constraint.d.ts" />
/// <reference path="./bindings/factory.d.ts" />
/// <reference path="./bindings/provider.d.ts" />
/// <reference path="./bindings/factory_creator.d.ts" />
/// <reference path="./bindings/provider_creator.d.ts" />
/// <reference path="./bindings/newable.d.ts" />

// ACTIVATION
/// <reference path="./activation/metadata.d.ts" />

/// <reference path="./planning/request.d.ts" />
/// <reference path="./planning/context.d.ts" />
/// <reference path="./planning/target.d.ts" />
/// <reference path="./planning/queryable_string.d.ts" />

// MIDDLEWARE
/// <reference path="./middleware/middleware.d.ts" />

// SYNTAX
/// <reference path="./syntax/binding_when_syntax.d.ts" />
/// <reference path="./syntax/binding_to_syntax.d.ts" />
/// <reference path="./syntax/binding_in_syntax.d.ts" />
5 changes: 3 additions & 2 deletions src/interfaces/syntax/binding_to_syntax.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
interface IBindingToSyntax<T> {
to(constructor: { new(...args: any[]): T; }): IBindingInSyntax<T>;
toValue(value: T): IBindingWhenSyntax<T>;
toConstructor(constructor: { new(...args: any[]): T; }): IBindingWhenSyntax<T>;
toFactory(factory: (context) => T): IBindingWhenSyntax<T>;
toConstructor<T2>(constructor: INewable<T2>): IBindingWhenSyntax<T>;
toFactory<T2>(factory: IFactoryCreator<T2>): IBindingWhenSyntax<T>;
toProvider<T2>(provider: IProviderCreator<T2>): IBindingWhenSyntax<T>;
}
13 changes: 0 additions & 13 deletions src/inversify.js

This file was deleted.

17 changes: 10 additions & 7 deletions src/planning/planner.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
///<reference path="../interfaces/interfaces.d.ts" />

import "reflect-metadata";
import Plan from "./plan";
import Context from "./context";
import Request from "./request";
import Target from "./target";
import * as METADATA_KEY from "../constants/metadata_keys";
import * as ERROR_MSGS from "../constants/error_msgs";
import BindingType from "../bindings/binding_type";

class Planner implements IPlanner {

Expand Down Expand Up @@ -58,16 +58,19 @@ class Planner implements IPlanner {

} else {

// TODO 2.0.0-alpha.2 handle value, factory, etc here
// Use the only active binding to create a child request
let binding = bindings[0];

let childRequest = parentRequest.addChildRequest(target.service.value(), binding, target);

let subDependencies = this._getDependencies(binding.implementationType);
// Only try to plan sub-dependencies when binding type is BindingType.Instance
if (binding.type === BindingType.Instance) {

subDependencies.forEach((d, index) => {
this._createSubRequest(childRequest, d);
});
// Create child requests for sub-dependencies if any
let subDependencies = this._getDependencies(binding.implementationType);
subDependencies.forEach((d, index) => {
this._createSubRequest(childRequest, d);
});
}
}
} catch (error) {
if (error instanceof RangeError) {
Expand Down
Loading

0 comments on commit 9274479

Please sign in to comment.