blister
Advanced tools
Comparing version 0.13.0 to 0.14.0
@@ -38,2 +38,3 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Blister = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
this._deps = Object.create(null); | ||
this._cache = Object.create(null); | ||
} | ||
@@ -61,2 +62,15 @@ | ||
/** | ||
* Returns a list with all the keys available in the container | ||
* @return {string[]} | ||
*/ | ||
keys: function() { | ||
var keys = []; | ||
/* eslint guard-for-in: 0 */ | ||
for (var i in this._deps) { | ||
keys.push(i); | ||
} | ||
return keys; | ||
}, | ||
/** | ||
* Returns the dependency set with the given id, | ||
@@ -153,2 +167,3 @@ * or undefined if it is not present | ||
/* eslint no-param-reassign: 0 */ | ||
type = originalWrapper.type; | ||
@@ -164,3 +179,3 @@ if (type === VALUE) { | ||
this._deps[id] = wrappers.create(type, value, this, originalWrapper); | ||
this._deps[id] = wrappers.create(type, value, originalWrapper); | ||
return this; | ||
@@ -184,11 +199,25 @@ }, | ||
/** | ||
* Creates a new context for the current dependency injection container. | ||
* A context inherits all the dependencies of its parent container and can | ||
* Creates a new scope for the current dependency injection container. | ||
* A scope inherits all the dependencies of its parent container and can | ||
* define its own dependencies that shadow the ones of the container. | ||
* @return {BlisterContainer} | ||
*/ | ||
createContext: function() { | ||
var context = Object.create(BlisterContainer.prototype); | ||
context._deps = Object.create(this._deps); | ||
return context; | ||
createScope: function() { | ||
var scope = new BlisterContainer(); | ||
scope._deps = Object.create(this._deps); | ||
return scope; | ||
}, | ||
/** | ||
* Creates a new scope with the contents of the given object registered as | ||
* value dependencies | ||
* @param {Object.<string,*>} values | ||
* @return {BlisterContainer} | ||
*/ | ||
withScope: function(values) { | ||
var scope = this.createScope(); | ||
Object.keys(values).forEach(function(depId) { | ||
scope.value(depId, values[depId]); | ||
}); | ||
return scope; | ||
} | ||
@@ -290,3 +319,15 @@ | ||
var count = 0; | ||
/** | ||
* Creates a process-wide unique service id | ||
* @private | ||
* @return {string} | ||
*/ | ||
function createServiceId() { | ||
count++; | ||
return 'service-' + count; | ||
} | ||
/** | ||
* Wrapper functions to store the different types of dependencies in the | ||
@@ -313,10 +354,9 @@ * container | ||
* @param {Function} value The factory function | ||
* @param {BlisterContainer} container | ||
* @param {Function} [originalWrapper] | ||
* @return {Function} | ||
*/ | ||
factory: function wrapFactory(value, container, originalWrapper) { | ||
factory: function wrapFactory(value, originalWrapper) { | ||
return function() { | ||
if (originalWrapper) { | ||
return value.call(this, originalWrapper(), this); | ||
return value.call(this, originalWrapper.call(this), this); | ||
} | ||
@@ -334,16 +374,15 @@ return value.call(this, this); | ||
*/ | ||
singleton: function wrapSingleton(value, container, originalWrapper) { | ||
var cached = false; | ||
var cachedValue; | ||
singleton: function wrapSingleton(value, originalWrapper) { | ||
var serviceId = createServiceId(); | ||
return function() { | ||
if (!cached) { | ||
var service = this._cache[serviceId]; | ||
if (!service) { | ||
if (originalWrapper) { | ||
cachedValue = value.call(container, originalWrapper(), container); | ||
service = value.call(this, originalWrapper.call(this), this); | ||
} else { | ||
cachedValue = value.call(container, container); | ||
service = value.call(this, this); | ||
} | ||
cached = true; | ||
value = null; | ||
this._cache[serviceId] = service; | ||
} | ||
return cachedValue; | ||
return service; | ||
}; | ||
@@ -350,0 +389,0 @@ }, |
@@ -1,2 +0,2 @@ | ||
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var t;t="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,t.Blister=e()}}(function(){return function e(t,n,r){function o(s,u){if(!n[s]){if(!t[s]){var c="function"==typeof require&&require;if(!u&&c)return c(s,!0);if(i)return i(s,!0);var f=new Error("Cannot find module '"+s+"'");throw f.code="MODULE_NOT_FOUND",f}var d=n[s]={exports:{}};t[s][0].call(d.exports,function(e){var n=t[s][1][e];return o(n?n:e)},d,d.exports,e,t,n,r)}return n[s].exports}for(var i="function"==typeof require&&require,s=0;s<r.length;s++)o(r[s]);return o}({1:[function(e,t){"use strict";function n(e){if("string"!=typeof e)throw new TypeError("The dependency id must be a string: "+e)}function r(){this._deps=Object.create(null)}var o=e("./wrappers"),i=e("./errors"),s=i.IllegalExtensionError,u=i.UnregisteredDependencyError,c=i.UnregisteredExtendedDependencyError,f="value",d="singleton",p="factory";r.IllegalExtensionError=s,r.UnregisteredDependencyError=u,r.UnregisteredExtendedDependencyError=c,r.prototype={constructor:r,has:function(e){return n(e),e in this._deps},get:function(e){if(n(e),!this.has(e))throw new u("Cannot get unregistered dependency "+e);return this._deps[e].call(this)},value:function(e,t){return this._set(e,t,f,!1)},factory:function(e,t){return this._set(e,t,p,!1)},service:function(e,t){return this._set(e,t,d,!1)},extend:function(e,t){return this._set(e,t,void 0,!0)},_set:function(e,t,r,i){n(e);var s=i?this._deps[e]:void 0;if(i){if(!s)throw new c;r=s.type,r===f&&(r=d)}if("function"!=typeof t&&(i||r!==f))throw new TypeError("The argument must be a function: "+t);return this._deps[e]=o.create(r,t,this,s),this},register:function(e){return"function"==typeof e?e(this):e.register(this),this},createContext:function(){var e=Object.create(r.prototype);return e._deps=Object.create(this._deps),e}},t.exports=r},{"./errors":2,"./wrappers":3}],2:[function(e,t){"use strict";function n(){}function r(e){this.name="IllegalExtensionError",this.message=e||"Values cannot be extended. Redefine them instead"}function o(e){this.name="UnregisteredDependencyError",this.message=e||"Cannot get an unregistered dependency"}function i(e){this.name="UnregisteredExtendedDependencyError",this.message=e||"Cannot extend a dependency not previously set"}n.prototype=Error.prototype,r.prototype=new n,r.constructor=r,o.prototype=new n,o.constructor=o,i.prototype=new n,i.constructor=i,t.exports={IllegalExtensionError:r,UnregisteredDependencyError:o,UnregisteredExtendedDependencyError:i}},{}],3:[function(e,t){"use strict";var n={value:function(e){return function(){return e}},factory:function(e,t,n){return function(){return n?e.call(this,n(),this):e.call(this,this)}},singleton:function(e,t,n){var r,o=!1;return function(){return o||(r=n?e.call(t,n(),t):e.call(t,t),o=!0,e=null),r}},create:function(e,t,n,r){var o=this[e](t,n,r);return o.type=e,o}};t.exports=n},{}]},{},[1])(1)}); | ||
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var t;t="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,t.Blister=e()}}(function(){return function e(t,n,r){function i(s,c){if(!n[s]){if(!t[s]){var u="function"==typeof require&&require;if(!c&&u)return u(s,!0);if(o)return o(s,!0);var f=new Error("Cannot find module '"+s+"'");throw f.code="MODULE_NOT_FOUND",f}var a=n[s]={exports:{}};t[s][0].call(a.exports,function(e){var n=t[s][1][e];return i(n?n:e)},a,a.exports,e,t,n,r)}return n[s].exports}for(var o="function"==typeof require&&require,s=0;s<r.length;s++)i(r[s]);return i}({1:[function(e,t){"use strict";function n(e){if("string"!=typeof e)throw new TypeError("The dependency id must be a string: "+e)}function r(){this._deps=Object.create(null),this._cache=Object.create(null)}var i=e("./wrappers"),o=e("./errors"),s=o.IllegalExtensionError,c=o.UnregisteredDependencyError,u=o.UnregisteredExtendedDependencyError,f="value",a="singleton",d="factory";r.IllegalExtensionError=s,r.UnregisteredDependencyError=c,r.UnregisteredExtendedDependencyError=u,r.prototype={constructor:r,has:function(e){return n(e),e in this._deps},keys:function(){var e=[];for(var t in this._deps)e.push(t);return e},get:function(e){if(n(e),!this.has(e))throw new c("Cannot get unregistered dependency "+e);return this._deps[e].call(this)},value:function(e,t){return this._set(e,t,f,!1)},factory:function(e,t){return this._set(e,t,d,!1)},service:function(e,t){return this._set(e,t,a,!1)},extend:function(e,t){return this._set(e,t,void 0,!0)},_set:function(e,t,r,o){n(e);var s=o?this._deps[e]:void 0;if(o){if(!s)throw new u;r=s.type,r===f&&(r=a)}if("function"!=typeof t&&(o||r!==f))throw new TypeError("The argument must be a function: "+t);return this._deps[e]=i.create(r,t,s),this},register:function(e){return"function"==typeof e?e(this):e.register(this),this},createScope:function(){var e=new r;return e._deps=Object.create(this._deps),e},withScope:function(e){var t=this.createScope();return Object.keys(e).forEach(function(n){t.value(n,e[n])}),t}},t.exports=r},{"./errors":2,"./wrappers":3}],2:[function(e,t){"use strict";function n(){}function r(e){this.name="IllegalExtensionError",this.message=e||"Values cannot be extended. Redefine them instead"}function i(e){this.name="UnregisteredDependencyError",this.message=e||"Cannot get an unregistered dependency"}function o(e){this.name="UnregisteredExtendedDependencyError",this.message=e||"Cannot extend a dependency not previously set"}n.prototype=Error.prototype,r.prototype=new n,r.constructor=r,i.prototype=new n,i.constructor=i,o.prototype=new n,o.constructor=o,t.exports={IllegalExtensionError:r,UnregisteredDependencyError:i,UnregisteredExtendedDependencyError:o}},{}],3:[function(e,t){"use strict";function n(){return r++,"service-"+r}var r=0,i={value:function(e){return function(){return e}},factory:function(e,t){return function(){return t?e.call(this,t.call(this),this):e.call(this,this)}},singleton:function(e,t){var r=n();return function(){var n=this._cache[r];return n||(n=t?e.call(this,t.call(this),this):e.call(this,this),this._cache[r]=n),n}},create:function(e,t,n,r){var i=this[e](t,n,r);return i.type=e,i}};t.exports=i},{}]},{},[1])(1)}); | ||
//# sourceMappingURL=blister.min.js.map |
{ | ||
"name": "blister", | ||
"version": "0.13.0", | ||
"version": "0.14.0", | ||
"author": "Rubén Norte <rubennorte@gmail.com>", | ||
@@ -37,5 +37,3 @@ "description": "Minimalist dependency injection container for JavaScript", | ||
"scripts": { | ||
"lint:errors": "jshint src test --reporter node_modules/jshint-stylish/stylish.js --verbose", | ||
"lint:style": "jscs src test --verbose", | ||
"lint": "npm run lint:errors && npm run lint:style", | ||
"lint": "eslint src test/unit/specs", | ||
"browserify:js": "browserify src/blister.js --standalone Blister --debug | exorcist dist/blister.js.map > dist/blister.js", | ||
@@ -60,20 +58,16 @@ "uglify:js": "uglifyjs dist/blister.js -o dist/blister.min.js --compress --mangle --in-source-map dist/blister.js.map --source-map dist/blister.min.js.map --source-map-url blister.min.js.map", | ||
"browserify": "^9.0.3", | ||
"eslint": "^1.9.0", | ||
"eslint-config-airbnb": "^1.0.0", | ||
"exorcist": "^0.3.0", | ||
"jasmine-core": "^2.2.0", | ||
"jscs": "^1.12.0", | ||
"jsdoc": "^3.3.0-beta3", | ||
"jshint": "^2.6.3", | ||
"jshint-stylish": "^1.0.1", | ||
"karma": "^0.12.31", | ||
"karma-chrome-launcher": "^0.1.7", | ||
"karma-cli": "0.0.4", | ||
"karma-firefox-launcher": "^0.1.4", | ||
"karma-ie-launcher": "^0.1.5", | ||
"karma-jasmine": "^0.3.5", | ||
"karma-junit-reporter": "^0.2.2", | ||
"karma-phantomjs-launcher": "^0.1.4", | ||
"karma-sourcemap-loader": "^0.3.4", | ||
"karma-verbose-reporter": "0.0.2", | ||
"mversion": "^1.10.0", | ||
"parallelshell": "^1.1.1", | ||
"jasmine-core": "^2.3.4", | ||
"jsdoc": "^3.4.0", | ||
"karma": "^0.13.15", | ||
"karma-jasmine": "^0.3.6", | ||
"karma-junit-reporter": "^0.3.8", | ||
"karma-phantomjs-launcher": "^0.2.1", | ||
"karma-sourcemap-loader": "^0.3.6", | ||
"karma-verbose-reporter": "0.0.3", | ||
"mversion": "^1.10.1", | ||
"parallelshell": "^2.0.0", | ||
"phantomjs": "^1.9.18", | ||
"phantomjs-polyfill": "0.0.1", | ||
@@ -80,0 +74,0 @@ "uglifyjs": "^2.4.10", |
@@ -50,2 +50,4 @@ # Blister | ||
container.has('name'); //> true | ||
container.keys(); //> ['name'] | ||
``` | ||
@@ -151,7 +153,7 @@ | ||
#### Creating new contexts | ||
#### Creating new scopes | ||
Some scenarios may require different instances of a DI container. For example, we may want to have different DI containers to handle different requests of a server (having specific dependencies for the current request). | ||
In order to be able to define global dependencies and context-specific dependencies, we can create a new context from a container, which is just a container that inherits dependencies from the original one. | ||
In order to be able to define global dependencies and scope-specific dependencies, we can create a new scope from a container, which is just a container that inherits dependencies from the original one. | ||
@@ -166,47 +168,30 @@ Example: | ||
var context = container.createContext(); | ||
context.get('logger'); // 'system logger' | ||
var scope = container.createScope(); | ||
scope.get('logger'); // 'system logger' | ||
context.service('logger', function() { | ||
scope.service('logger', function() { | ||
return 'request logger'; | ||
}); | ||
context.get('logger'); // 'request logger' | ||
scope.get('logger'); // 'request logger' | ||
``` | ||
__IMPORTANT__: | ||
Example with requests: | ||
If you have dependencies in a container that should use dependencies from a sub-context when accessing through it, define them as factories instead of as services. | ||
All the dependencies of a given service are fetched from the context where the service is defined: | ||
```javascript | ||
var container = new Blister(); | ||
container.service('logger', function(c) { | ||
return c.get('scope') + ' logger'; | ||
return 'log from user ' + c.get('request').username; | ||
}); | ||
container.value('scope', 'system'); | ||
var context = container.createContext(); | ||
context.value('scope', 'request'); | ||
var johnRequestScope = container.createScope(); | ||
johnRequestScope.value('request', {username: 'John'}); | ||
context.get('logger'); // 'system logger'; | ||
container.get('logger'); // 'system logger'; | ||
``` | ||
var paulRequestScope = container.createScope(); | ||
paulRequestScope.value('request', {username: 'Paul'}); | ||
This is because services are cached in the scope where they are defined (to make them a system-wide singleton as expected), so accessing a dependency of the context would make the service inconsistent in the scope of the container (as it would use a dependency of a specifc sub-context). | ||
johnRequestScope.get('logger'); // 'log from user John' | ||
paulRequestScope.get('logger'); // 'log from user Paul' | ||
This problem does not exist in factories, because they do not have this kind of side-effects (they are not cached): | ||
```javascript | ||
var container = new Blister(); | ||
container.factory('logger', function(c) { | ||
return c.get('scope') + ' logger'; | ||
}); | ||
container.value('scope', 'system'); | ||
var context = container.createContext(); | ||
context.value('scope', 'request'); | ||
context.get('logger'); // 'request logger'; | ||
container.get('logger'); // 'system logger'; | ||
// Or shorter... | ||
container.withScope({ request: { username: 'Laura' } }).get('logger'); // 'log from user Laura' | ||
``` | ||
@@ -213,0 +198,0 @@ |
@@ -37,2 +37,3 @@ 'use strict'; | ||
this._deps = Object.create(null); | ||
this._cache = Object.create(null); | ||
} | ||
@@ -60,2 +61,15 @@ | ||
/** | ||
* Returns a list with all the keys available in the container | ||
* @return {string[]} | ||
*/ | ||
keys: function() { | ||
var keys = []; | ||
/* eslint guard-for-in: 0 */ | ||
for (var i in this._deps) { | ||
keys.push(i); | ||
} | ||
return keys; | ||
}, | ||
/** | ||
* Returns the dependency set with the given id, | ||
@@ -152,2 +166,3 @@ * or undefined if it is not present | ||
/* eslint no-param-reassign: 0 */ | ||
type = originalWrapper.type; | ||
@@ -163,3 +178,3 @@ if (type === VALUE) { | ||
this._deps[id] = wrappers.create(type, value, this, originalWrapper); | ||
this._deps[id] = wrappers.create(type, value, originalWrapper); | ||
return this; | ||
@@ -183,11 +198,25 @@ }, | ||
/** | ||
* Creates a new context for the current dependency injection container. | ||
* A context inherits all the dependencies of its parent container and can | ||
* Creates a new scope for the current dependency injection container. | ||
* A scope inherits all the dependencies of its parent container and can | ||
* define its own dependencies that shadow the ones of the container. | ||
* @return {BlisterContainer} | ||
*/ | ||
createContext: function() { | ||
var context = Object.create(BlisterContainer.prototype); | ||
context._deps = Object.create(this._deps); | ||
return context; | ||
createScope: function() { | ||
var scope = new BlisterContainer(); | ||
scope._deps = Object.create(this._deps); | ||
return scope; | ||
}, | ||
/** | ||
* Creates a new scope with the contents of the given object registered as | ||
* value dependencies | ||
* @param {Object.<string,*>} values | ||
* @return {BlisterContainer} | ||
*/ | ||
withScope: function(values) { | ||
var scope = this.createScope(); | ||
Object.keys(values).forEach(function(depId) { | ||
scope.value(depId, values[depId]); | ||
}); | ||
return scope; | ||
} | ||
@@ -194,0 +223,0 @@ |
@@ -0,0 +0,0 @@ 'use strict'; |
'use strict'; | ||
var count = 0; | ||
/** | ||
* Creates a process-wide unique service id | ||
* @private | ||
* @return {string} | ||
*/ | ||
function createServiceId() { | ||
count++; | ||
return 'service-' + count; | ||
} | ||
/** | ||
* Wrapper functions to store the different types of dependencies in the | ||
@@ -25,10 +37,9 @@ * container | ||
* @param {Function} value The factory function | ||
* @param {BlisterContainer} container | ||
* @param {Function} [originalWrapper] | ||
* @return {Function} | ||
*/ | ||
factory: function wrapFactory(value, container, originalWrapper) { | ||
factory: function wrapFactory(value, originalWrapper) { | ||
return function() { | ||
if (originalWrapper) { | ||
return value.call(this, originalWrapper(), this); | ||
return value.call(this, originalWrapper.call(this), this); | ||
} | ||
@@ -46,16 +57,15 @@ return value.call(this, this); | ||
*/ | ||
singleton: function wrapSingleton(value, container, originalWrapper) { | ||
var cached = false; | ||
var cachedValue; | ||
singleton: function wrapSingleton(value, originalWrapper) { | ||
var serviceId = createServiceId(); | ||
return function() { | ||
if (!cached) { | ||
var service = this._cache[serviceId]; | ||
if (!service) { | ||
if (originalWrapper) { | ||
cachedValue = value.call(container, originalWrapper(), container); | ||
service = value.call(this, originalWrapper.call(this), this); | ||
} else { | ||
cachedValue = value.call(container, container); | ||
service = value.call(this, this); | ||
} | ||
cached = true; | ||
value = null; | ||
this._cache[serviceId] = service; | ||
} | ||
return cachedValue; | ||
return service; | ||
}; | ||
@@ -62,0 +72,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
65566
18
718
220