blister
Advanced tools
Comparing version 0.14.0 to 1.0.0
@@ -38,3 +38,2 @@ (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); | ||
} | ||
@@ -66,8 +65,3 @@ | ||
keys: function() { | ||
var keys = []; | ||
/* eslint guard-for-in: 0 */ | ||
for (var i in this._deps) { | ||
keys.push(i); | ||
} | ||
return keys; | ||
return Object.keys(this._deps); | ||
}, | ||
@@ -178,3 +172,3 @@ | ||
this._deps[id] = wrappers.create(type, value, originalWrapper); | ||
this._deps[id] = wrappers.create(type, value, this, originalWrapper); | ||
return this; | ||
@@ -195,28 +189,2 @@ }, | ||
return this; | ||
}, | ||
/** | ||
* 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} | ||
*/ | ||
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; | ||
} | ||
@@ -318,15 +286,3 @@ | ||
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 | ||
@@ -353,9 +309,10 @@ * container | ||
* @param {Function} value The factory function | ||
* @param {BlisterContainer} container | ||
* @param {Function} [originalWrapper] | ||
* @return {Function} | ||
*/ | ||
factory: function wrapFactory(value, originalWrapper) { | ||
factory: function wrapFactory(value, container, originalWrapper) { | ||
return function() { | ||
if (originalWrapper) { | ||
return value.call(this, originalWrapper.call(this), this); | ||
return value.call(this, originalWrapper(), this); | ||
} | ||
@@ -373,15 +330,17 @@ return value.call(this, this); | ||
*/ | ||
singleton: function wrapSingleton(value, originalWrapper) { | ||
var serviceId = createServiceId(); | ||
singleton: function wrapSingleton(value, container, originalWrapper) { | ||
var cached = false; | ||
var cachedValue; | ||
return function() { | ||
var service = this._cache[serviceId]; | ||
if (!service) { | ||
if (!cached) { | ||
if (originalWrapper) { | ||
service = value.call(this, originalWrapper.call(this), this); | ||
cachedValue = value.call(container, originalWrapper(), container); | ||
} else { | ||
service = value.call(this, this); | ||
cachedValue = value.call(container, container); | ||
} | ||
this._cache[serviceId] = service; | ||
cached = true; | ||
/* eslint no-param-reassign: 0 */ | ||
value = null; | ||
} | ||
return service; | ||
return cachedValue; | ||
}; | ||
@@ -388,0 +347,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 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)}); | ||
!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},keys:function(){return Object.keys(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}},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)}); | ||
//# sourceMappingURL=blister.min.js.map |
{ | ||
"name": "blister", | ||
"version": "0.14.0", | ||
"version": "1.0.0", | ||
"author": "Rubén Norte <rubennorte@gmail.com>", | ||
@@ -37,18 +37,19 @@ "description": "Minimalist dependency injection container for JavaScript", | ||
"scripts": { | ||
"lint": "eslint src test/unit/specs", | ||
"build": "npm run build:js", | ||
"build:js": "npm run browserify:js && npm run uglify:js", | ||
"browserify:js": "browserify src/blister.js --standalone Blister --debug | exorcist dist/blister.js.map > dist/blister.js", | ||
"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", | ||
"build:js": "npm run browserify:js && npm run uglify:js", | ||
"build": "npm run build:js", | ||
"pretest": "npm run lint", | ||
"lint": "eslint src test/unit/specs", | ||
"test": "npm run build:tests && npm run karma:ci", | ||
"build:tests": "npm run browserify:tests", | ||
"browserify:tests": "browserify test/unit/index.js -o test/unit/gen/unit-test-bundle.js --debug", | ||
"karma:ci": "karma start test/unit/config/karma-ci.conf.js", | ||
"develop": "npm-run-all --parallel watchify:tests karma:dev", | ||
"watchify:tests": "watchify test/unit/index.js -o test/unit/gen/unit-test-bundle.js --verbose", | ||
"build:tests": "npm run browserify:tests", | ||
"pretest": "npm run lint", | ||
"karma:ci": "karma start test/unit/config/karma-ci.conf.js", | ||
"test": "npm run build:tests && npm run karma:ci", | ||
"develop": "parallelshell \"npm run watchify:tests\" \"karma start test/unit/config/karma-dev.conf.js\"", | ||
"karma:dev": "karma start test/unit/config/karma-dev.conf.js", | ||
"release": "mversion -m \"New version: %s\"", | ||
"prepare-release": "npm test && npm run build", | ||
"release": "mversion -m \"New version: %s\"", | ||
"doc:js": "jsdoc src --destination doc/js --readme README.md --verbose", | ||
"doc": "npm run doc:js" | ||
"doc": "npm run doc:js", | ||
"doc:js": "jsdoc src --destination doc/js --readme README.md --verbose" | ||
}, | ||
@@ -58,8 +59,9 @@ "dependencies": {}, | ||
"browserify": "^9.0.3", | ||
"watchify": "^2.4.0", | ||
"exorcist": "^0.3.0", | ||
"uglifyjs": "^2.4.10", | ||
"eslint": "^1.9.0", | ||
"eslint-config-airbnb": "^1.0.0", | ||
"exorcist": "^0.3.0", | ||
"karma": "^0.13.15", | ||
"jasmine-core": "^2.3.4", | ||
"jsdoc": "^3.4.0", | ||
"karma": "^0.13.15", | ||
"karma-jasmine": "^0.3.6", | ||
@@ -70,8 +72,7 @@ "karma-junit-reporter": "^0.3.8", | ||
"karma-verbose-reporter": "0.0.3", | ||
"mversion": "^1.10.1", | ||
"parallelshell": "^2.0.0", | ||
"phantomjs": "^1.9.18", | ||
"phantomjs-polyfill": "0.0.1", | ||
"uglifyjs": "^2.4.10", | ||
"watchify": "^2.4.0" | ||
"jsdoc": "^3.4.0", | ||
"npm-run-all": "^1.3.3", | ||
"mversion": "^1.10.1" | ||
}, | ||
@@ -78,0 +79,0 @@ "engines": { |
@@ -7,3 +7,3 @@ # Blister | ||
The package is available as a UMD module: compatible with AMD, CommonJS and exposing a global variable `Blister` in `dist/blister.min.js` (1.2 KB minified and gzipped). | ||
The package is available as a UMD module: compatible with AMD, CommonJS and exposing a global variable `Blister` in `dist/blister.min.js` (1.3 KB minified and gzipped). | ||
@@ -153,46 +153,2 @@ It can be installed via npm (for both Node.js and browserify/webpack), Bower or downloading it from the repository: | ||
#### 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 scope-specific dependencies, we can create a new scope from a container, which is just a container that inherits dependencies from the original one. | ||
Example: | ||
```javascript | ||
var container = new Blister(); | ||
container.service('logger', function() { | ||
return 'system logger'; | ||
}); | ||
var scope = container.createScope(); | ||
scope.get('logger'); // 'system logger' | ||
scope.service('logger', function() { | ||
return 'request logger'; | ||
}); | ||
scope.get('logger'); // 'request logger' | ||
``` | ||
Example with requests: | ||
```javascript | ||
var container = new Blister(); | ||
container.service('logger', function(c) { | ||
return 'log from user ' + c.get('request').username; | ||
}); | ||
var johnRequestScope = container.createScope(); | ||
johnRequestScope.value('request', {username: 'John'}); | ||
var paulRequestScope = container.createScope(); | ||
paulRequestScope.value('request', {username: 'Paul'}); | ||
johnRequestScope.get('logger'); // 'log from user John' | ||
paulRequestScope.get('logger'); // 'log from user Paul' | ||
// Or shorter... | ||
container.withScope({ request: { username: 'Laura' } }).get('logger'); // 'log from user Laura' | ||
``` | ||
## Documentation | ||
@@ -199,0 +155,0 @@ |
@@ -37,3 +37,2 @@ 'use strict'; | ||
this._deps = Object.create(null); | ||
this._cache = Object.create(null); | ||
} | ||
@@ -65,8 +64,3 @@ | ||
keys: function() { | ||
var keys = []; | ||
/* eslint guard-for-in: 0 */ | ||
for (var i in this._deps) { | ||
keys.push(i); | ||
} | ||
return keys; | ||
return Object.keys(this._deps); | ||
}, | ||
@@ -177,3 +171,3 @@ | ||
this._deps[id] = wrappers.create(type, value, originalWrapper); | ||
this._deps[id] = wrappers.create(type, value, this, originalWrapper); | ||
return this; | ||
@@ -194,28 +188,2 @@ }, | ||
return this; | ||
}, | ||
/** | ||
* 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} | ||
*/ | ||
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; | ||
} | ||
@@ -222,0 +190,0 @@ |
'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 | ||
@@ -37,9 +25,10 @@ * container | ||
* @param {Function} value The factory function | ||
* @param {BlisterContainer} container | ||
* @param {Function} [originalWrapper] | ||
* @return {Function} | ||
*/ | ||
factory: function wrapFactory(value, originalWrapper) { | ||
factory: function wrapFactory(value, container, originalWrapper) { | ||
return function() { | ||
if (originalWrapper) { | ||
return value.call(this, originalWrapper.call(this), this); | ||
return value.call(this, originalWrapper(), this); | ||
} | ||
@@ -57,15 +46,17 @@ return value.call(this, this); | ||
*/ | ||
singleton: function wrapSingleton(value, originalWrapper) { | ||
var serviceId = createServiceId(); | ||
singleton: function wrapSingleton(value, container, originalWrapper) { | ||
var cached = false; | ||
var cachedValue; | ||
return function() { | ||
var service = this._cache[serviceId]; | ||
if (!service) { | ||
if (!cached) { | ||
if (originalWrapper) { | ||
service = value.call(this, originalWrapper.call(this), this); | ||
cachedValue = value.call(container, originalWrapper(), container); | ||
} else { | ||
service = value.call(this, this); | ||
cachedValue = value.call(container, container); | ||
} | ||
this._cache[serviceId] = service; | ||
cached = true; | ||
/* eslint no-param-reassign: 0 */ | ||
value = null; | ||
} | ||
return service; | ||
return cachedValue; | ||
}; | ||
@@ -72,0 +63,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1
58972
643
176