blister
Advanced tools
Comparing version 0.11.1 to 0.12.0
@@ -15,17 +15,4 @@ (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){ | ||
var objectHasProp = Object.prototype.hasOwnProperty; | ||
/** | ||
* Indicates if the given object has the given propery name as own | ||
* @private | ||
* @param {Object} object | ||
* @param {string} name | ||
* @return {boolean} | ||
*/ | ||
function hasOwnProp(obj, name) { | ||
return objectHasProp.call(obj, name); | ||
} | ||
/** | ||
* @private | ||
* @param {*} id | ||
@@ -51,3 +38,3 @@ * @throws {TypeError} If the passed id is not a string | ||
function BlisterContainer() { | ||
this._deps = {}; | ||
this._deps = Object.create(null); | ||
} | ||
@@ -71,4 +58,3 @@ | ||
checkId(id); | ||
return hasOwnProp(this._deps, id); | ||
return id in this._deps; | ||
}, | ||
@@ -85,7 +71,7 @@ | ||
if (!hasOwnProp(this._deps, id)) { | ||
if (!this.has(id)) { | ||
throw new UnregisteredDependencyError('Cannot get unregistered dependency ' + id); | ||
} | ||
return this._deps[id](); | ||
return this._deps[id].call(this); | ||
}, | ||
@@ -191,2 +177,14 @@ | ||
return this; | ||
}, | ||
/** | ||
* Creates a new context for the current dependency injection container. | ||
* A context 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; | ||
} | ||
@@ -317,5 +315,5 @@ | ||
if (originalWrapper) { | ||
return value.call(container, originalWrapper(), container); | ||
return value.call(this, originalWrapper(), this); | ||
} | ||
return value.call(container, container); | ||
return value.call(this, this); | ||
}; | ||
@@ -322,0 +320,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 n;n="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,n.Blister=e()}}(function(){return function e(n,r,t){function o(s,u){if(!r[s]){if(!n[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=r[s]={exports:{}};n[s][0].call(d.exports,function(e){var r=n[s][1][e];return o(r?r:e)},d,d.exports,e,n,r,t)}return r[s].exports}for(var i="function"==typeof require&&require,s=0;s<t.length;s++)o(t[s]);return o}({1:[function(e,n){"use strict";function r(e,n){return l.call(e,n)}function t(e){if("string"!=typeof e)throw new TypeError("The dependency id must be a string: "+e)}function o(){this._deps={}}var i=e("./wrappers"),s=e("./errors"),u=s.IllegalExtensionError,c=s.UnregisteredDependencyError,f=s.UnregisteredExtendedDependencyError,d="value",p="singleton",a="factory",l=Object.prototype.hasOwnProperty;o.IllegalExtensionError=u,o.UnregisteredDependencyError=c,o.UnregisteredExtendedDependencyError=f,o.prototype={constructor:o,has:function(e){return t(e),r(this._deps,e)},get:function(e){if(t(e),!r(this._deps,e))throw new c("Cannot get unregistered dependency "+e);return this._deps[e]()},value:function(e,n){return this._set(e,n,d,!1)},factory:function(e,n){return this._set(e,n,a,!1)},service:function(e,n){return this._set(e,n,p,!1)},extend:function(e,n){return this._set(e,n,void 0,!0)},_set:function(e,n,r,o){t(e);var s=o?this._deps[e]:void 0;if(o){if(!s)throw new f;r=s.type,r===d&&(r=p)}if("function"!=typeof n&&(o||r!==d))throw new TypeError("The argument must be a function: "+n);return this._deps[e]=i.create(r,n,this,s),this},register:function(e){return e.register(this),this}},n.exports=o},{"./errors":2,"./wrappers":3}],2:[function(e,n){"use strict";function r(){}function t(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"}r.prototype=Error.prototype,t.prototype=new r,t.constructor=t,o.prototype=new r,o.constructor=o,i.prototype=new r,i.constructor=i,n.exports={IllegalExtensionError:t,UnregisteredDependencyError:o,UnregisteredExtendedDependencyError:i}},{}],3:[function(e,n){"use strict";var r={value:function(e){return function(){return e}},factory:function(e,n,r){return function(){return r?e.call(n,r(),n):e.call(n,n)}},singleton:function(e,n,r){var t,o=!1;return function(){return o||(t=r?e.call(n,r(),n):e.call(n,n),o=!0,e=null),t}},create:function(e,n,r,t){var o=this[e](n,r,t);return o.type=e,o}};n.exports=r},{}]},{},[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},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 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)}); | ||
//# sourceMappingURL=blister.min.js.map |
{ | ||
"name": "blister", | ||
"version": "0.11.1", | ||
"version": "0.12.0", | ||
"author": "Rubén Norte <rubennorte@gmail.com>", | ||
@@ -21,3 +21,4 @@ "description": "Minimalist dependency injection container for JavaScript", | ||
"config", | ||
"IoC" | ||
"IoC", | ||
"context" | ||
], | ||
@@ -24,0 +25,0 @@ "homepage": "https://github.com/rubennorte/blister", |
@@ -138,2 +138,63 @@ # Blister | ||
#### Creating new contexts | ||
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. | ||
Example: | ||
```javascript | ||
var container = new Blister(); | ||
container.service('logger', function() { | ||
return 'system logger'; | ||
}); | ||
var context = container.createContext(); | ||
context.get('logger'); // 'system logger' | ||
context.service('logger', function() { | ||
return 'request logger'; | ||
}); | ||
context.get('logger'); // 'request logger' | ||
``` | ||
__IMPORTANT__: | ||
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'; | ||
}); | ||
container.value('scope', 'system'); | ||
var context = container.createContext(); | ||
context.value('scope', 'request'); | ||
context.get('logger'); // 'system logger'; | ||
container.get('logger'); // 'system logger'; | ||
``` | ||
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). | ||
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'; | ||
``` | ||
## Documentation | ||
@@ -140,0 +201,0 @@ |
@@ -14,17 +14,4 @@ 'use strict'; | ||
var objectHasProp = Object.prototype.hasOwnProperty; | ||
/** | ||
* Indicates if the given object has the given propery name as own | ||
* @private | ||
* @param {Object} object | ||
* @param {string} name | ||
* @return {boolean} | ||
*/ | ||
function hasOwnProp(obj, name) { | ||
return objectHasProp.call(obj, name); | ||
} | ||
/** | ||
* @private | ||
* @param {*} id | ||
@@ -50,3 +37,3 @@ * @throws {TypeError} If the passed id is not a string | ||
function BlisterContainer() { | ||
this._deps = {}; | ||
this._deps = Object.create(null); | ||
} | ||
@@ -70,4 +57,3 @@ | ||
checkId(id); | ||
return hasOwnProp(this._deps, id); | ||
return id in this._deps; | ||
}, | ||
@@ -84,7 +70,7 @@ | ||
if (!hasOwnProp(this._deps, id)) { | ||
if (!this.has(id)) { | ||
throw new UnregisteredDependencyError('Cannot get unregistered dependency ' + id); | ||
} | ||
return this._deps[id](); | ||
return this._deps[id].call(this); | ||
}, | ||
@@ -190,2 +176,14 @@ | ||
return this; | ||
}, | ||
/** | ||
* Creates a new context for the current dependency injection container. | ||
* A context 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; | ||
} | ||
@@ -192,0 +190,0 @@ |
@@ -32,5 +32,5 @@ 'use strict'; | ||
if (originalWrapper) { | ||
return value.call(container, originalWrapper(), container); | ||
return value.call(this, originalWrapper(), this); | ||
} | ||
return value.call(container, container); | ||
return value.call(this, this); | ||
}; | ||
@@ -37,0 +37,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
63883
223