boxed-injector
Advanced tools
Comparing version 0.4.0 to 0.5.0
@@ -32,2 +32,3 @@ 'use strict'; | ||
this._initMiddleware = this._initMiddleware.bind(this); | ||
this.create = this.create.bind(this); | ||
} | ||
@@ -120,2 +121,26 @@ | ||
}, { | ||
key: 'create', | ||
value: function create(name, otherArgs) { | ||
var _this3 = this; | ||
var self = this; | ||
if (arguments.length > 2) { | ||
otherArgs = [].concat(Array.prototype.slice.call(arguments)).slice(1); | ||
} | ||
assert(self.factories[name] || self.instances[name] && typeof self.instances[name].instance === 'function', 'Factory or Service must be registered.'); | ||
if (self.factories[name]) { | ||
var Factory = self.factories[name].factory; | ||
var args = Factory.inject || []; | ||
var resolvedDeps = args.map(function (dependency) { | ||
return _this3.get(dependency); | ||
}); | ||
var merged = [].concat(resolvedDeps, otherArgs); | ||
return new (Function.prototype.bind.apply(Factory, [null].concat(_toConsumableArray(merged))))(); | ||
} | ||
var Instance = self.instances[name].instance; | ||
return new (Function.prototype.bind.apply(Instance, [null].concat(_toConsumableArray(otherArgs))))(); | ||
} | ||
}, { | ||
key: 'get', | ||
@@ -137,2 +162,3 @@ value: function get(name) { | ||
} | ||
var instanceEntity = self.instances[name]; | ||
@@ -150,3 +176,3 @@ if (!isFactory) { | ||
value: function middleware(name, method) { | ||
var _this3 = this; | ||
var _this4 = this; | ||
@@ -176,3 +202,3 @@ if (typeof name === 'function') { | ||
var globals = this.middlewares[this.globalStr].filter(function (x) { | ||
return !(_this3.middlewares[name].before.indexOf(x) !== -1) && !(_this3.middlewares[name].after.indexOf(x) !== -1); | ||
return !(_this4.middlewares[name].before.indexOf(x) !== -1) && !(_this4.middlewares[name].after.indexOf(x) !== -1); | ||
}); | ||
@@ -179,0 +205,0 @@ this.middlewares[name].after = this.middlewares[name].after.concat(globals); |
@@ -1,2 +0,1 @@ | ||
'use strict'; | ||
@@ -170,3 +169,5 @@ | ||
expect(function () { | ||
return injector._applyMiddleware.call(null, [{ name: 'foo' }, 'baz']); | ||
return injector._applyMiddleware.call(null, [{ | ||
name: 'foo' | ||
}, 'baz']); | ||
}).toThrow(Error, 'was expecting an error to be thrown.'); | ||
@@ -203,2 +204,3 @@ }); | ||
injector.register('foo', 'abcd'); | ||
function factory(foo) { | ||
@@ -229,2 +231,50 @@ this.foo = foo; | ||
}); | ||
describe('create', function () { | ||
beforeEach(function () { | ||
function BMW(engine, color, interior) { | ||
this.make = 'BMW'; | ||
this.engine = engine; | ||
this.color = color; | ||
this.interior = interior; | ||
} | ||
BMW.inject = ['Engine']; | ||
injector.register('Engine', 'foo'); | ||
injector.register('Ferarri', BMW); | ||
injector.factory('BMW', BMW); | ||
}); | ||
it('should create an instance of a factory withArgs', function () { | ||
var instance1 = injector.create('BMW', 'black'); | ||
var instance2 = injector.create('BMW', ['white', 'leather']); | ||
expect(instance1.make).toBe('BMW'); | ||
expect(instance1.color).toBe('black'); | ||
expect(instance2.make).toBe('BMW'); | ||
expect(instance2.color).toBe('white'); | ||
expect(instance1.engine).toBe('foo'); | ||
expect(instance2.engine).toBe('foo'); | ||
expect(instance2.interior).toBe('leather'); | ||
}); | ||
it('should create an instance of a factory with multiple args instead of array', function () { | ||
var instance1 = injector.create('BMW', 'black'); | ||
var instance2 = injector.create('BMW', 'white', 'leather'); | ||
expect(instance1.make).toBe('BMW'); | ||
expect(instance1.color).toBe('black'); | ||
expect(instance2.make).toBe('BMW'); | ||
expect(instance2.color).toBe('white'); | ||
expect(instance1.engine).toBe('foo'); | ||
expect(instance2.engine).toBe('foo'); | ||
expect(instance2.interior).toBe('leather'); | ||
}); | ||
it('should create an instance of a service withArgs', function () { | ||
var instance1 = injector.create('Ferarri', ['hemi', 'red']); | ||
expect(instance1.color).toBe('red'); | ||
expect(instance1.engine).toBe('hemi'); | ||
}); | ||
it('dont crash on no arr', function () { | ||
var instance1 = injector.create('BMW'); | ||
expect(instance1.make).toBe('BMW'); | ||
}); | ||
}); | ||
}); |
@@ -1,2 +0,1 @@ | ||
'use strict'; | ||
@@ -15,3 +14,7 @@ | ||
module.exports.get = function () { | ||
if (!_instance) { | ||
console.error('\n Injector has not yet been registered in the service locator. \n Ensure Locator.set(injector); is called prior to Locator.get. \n Beware of sync require calls.\n '); | ||
throw new Error('Injector Not Registered'); | ||
} | ||
return _instance; | ||
}; |
@@ -1,2 +0,1 @@ | ||
'use strict'; | ||
@@ -23,4 +22,7 @@ | ||
it('should throw if you get it without setting', function () { | ||
expect(store.get).toThrow(Error); | ||
}); | ||
it('should expose a global setter', function () { | ||
expect(_typeof(store.get())).toEqual('undefined', 'defaulting to object'); | ||
expect(_typeof(store.set)).toBe('function'); | ||
@@ -27,0 +29,0 @@ store.set(injector); |
{ | ||
"name": "boxed-injector", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "Dependency Injection Tools", | ||
@@ -5,0 +5,0 @@ "homepage": "", |
@@ -1,2 +0,2 @@ | ||
# boxed-injector [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][https://travis-ci.org/giddyinc/boxed-injector] [![Dependency Status][daviddm-image]][daviddm-url] [![Coverage Status](https://coveralls.io/repos/github/giddyinc/boxed-injector/badge.svg?branch=master)](https://coveralls.io/github/giddyinc/boxed-injector?branch=master) | ||
# boxed-injector [![NPM version][npm-image]][npm-url] [![Build Status](https://travis-ci.org/giddyinc/boxed-injector.svg?branch=master)](https://travis-ci.org/giddyinc/boxed-injector) [![Dependency Status][daviddm-image]][daviddm-url] [![Coverage Status](https://coveralls.io/repos/github/giddyinc/boxed-injector/badge.svg?branch=master)](https://coveralls.io/github/giddyinc/boxed-injector?branch=master) | ||
> Dependency Injection Tools | ||
@@ -88,2 +88,13 @@ | ||
``` | ||
## Create | ||
Resolves a factory's dependencies and then instantiates an instance with the given args. Will call the constructor (new) on a registered instance that is a function. | ||
```js | ||
function Engine() {} | ||
function Car(engine, color) { this.engine = engine; this.color = color; } | ||
Car.inject = ['Engine']; | ||
injector.factory('Engine', Engine); | ||
injector.factory('Car', Car); | ||
const car = injector.create('Car', ['blue']); | ||
// { engine, engine, color: 'blue' } | ||
``` | ||
@@ -100,3 +111,3 @@ ## Middleware | ||
depends: ['ThingItsDependentOn', 'OtherThing'], | ||
instance: { thing: {}, other: {} }, //fully instantiated instance, | ||
instance: { thing: {}, other: {} }, // fully instantiated instance, | ||
factory: ExampleService // factory | ||
@@ -111,7 +122,7 @@ } | ||
// will console log before getting any instance from the container | ||
injector.middleware(entity => console.log('before global'); | ||
injector.middleware(entity => console.log('before global')); | ||
// will console log 'baz' before getting baz from the container - will always run after global above | ||
injector.middleware('baz', entity => console.log(entity.name); | ||
injector.middleware('baz', entity => console.log(entity.name)); | ||
// will console log for any instance, but will run after baz and above global is logged | ||
injector.middleware(entity => console.log(`before global again - resolving ${entity.name}`); | ||
injector.middleware(entity => console.log(`before global again - resolving ${entity.name}`)); | ||
@@ -121,5 +132,5 @@ injector.register('baz', result); | ||
// will console log AFTER getting any instance from the container | ||
injector.middleware(() => console.log('after global'); | ||
injector.middleware(() => console.log('after global')); | ||
// will console log 'baz' AFTER getting baz from the container - will always run after global above | ||
injector.middleware('baz', entity => console.log(entity.name); | ||
injector.middleware('baz', entity => console.log(entity.name)); | ||
@@ -126,0 +137,0 @@ injector.get('baz'); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38927
711
160