blister
Advanced tools
Comparing version 0.10.0 to 0.11.0
@@ -8,3 +8,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 IllegalExtensionError = errors.IllegalExtensionError; | ||
var MissingExtendedDependencyError = errors.MissingExtendedDependencyError; | ||
var UnregisteredDependencyError = errors.UnregisteredDependencyError; | ||
var UnregisteredExtendedDependencyError = errors.UnregisteredExtendedDependencyError; | ||
@@ -15,3 +16,27 @@ var VALUE = 'value'; | ||
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 | ||
* @throws {TypeError} If the passed id is not a string | ||
*/ | ||
function checkId(id) { | ||
if (typeof id !== 'string') { | ||
throw new TypeError('The dependency id must be a string: ' + id); | ||
} | ||
} | ||
/** | ||
* Dependency injection container constructor | ||
@@ -31,3 +56,4 @@ * | ||
BlisterContainer.IllegalExtensionError = IllegalExtensionError; | ||
BlisterContainer.MissingExtendedDependencyError = MissingExtendedDependencyError; | ||
BlisterContainer.UnregisteredDependencyError = UnregisteredDependencyError; | ||
BlisterContainer.UnregisteredExtendedDependencyError = UnregisteredExtendedDependencyError; | ||
@@ -39,2 +65,14 @@ BlisterContainer.prototype = { | ||
/** | ||
* Indicates if there is a registered dependency with the given id | ||
* @param {string} id | ||
* @return {boolean} | ||
* @throws {TypeError} If the id is not a string | ||
*/ | ||
has: function(id) { | ||
checkId(id); | ||
return hasOwnProp(this._deps, id); | ||
}, | ||
/** | ||
* Returns the dependency set with the given id, | ||
@@ -46,4 +84,9 @@ * or undefined if it is not present | ||
get: function(id) { | ||
var wrapper = this._deps[id]; | ||
return wrapper && wrapper(); | ||
checkId(id); | ||
if (!hasOwnProp(this._deps, id)) { | ||
throw new UnregisteredDependencyError('Cannot get unregistered dependency ' + id); | ||
} | ||
return this._deps[id](); | ||
}, | ||
@@ -75,11 +118,11 @@ | ||
/** | ||
* Registers the given singleton function with the specified id | ||
* Registers the given service function with the specified id | ||
* @param {string} id | ||
* @param {Function} singletonFn | ||
* @param {Function} serviceFn | ||
* @return {BlisterContainer} this | ||
* @throws {TypeError} If id is not a string | ||
* @throws {TypeError} If singletonFn is not a function | ||
* @throws {TypeError} If serviceFn is not a function | ||
*/ | ||
singleton: function(id, singletonFn) { | ||
return this._set(id, singletonFn, SINGLETON, false); | ||
service: function(id, serviceFn) { | ||
return this._set(id, serviceFn, SINGLETON, false); | ||
}, | ||
@@ -89,3 +132,3 @@ | ||
* Extends a previously defined dependency with the same type: | ||
* factory or singleton | ||
* service or factory | ||
* @param {string} id | ||
@@ -96,6 +139,4 @@ * @param {Function} definition | ||
* @throws {TypeError} If definition is not a function | ||
* @throws {MissingExtendedDependencyError} If there was not a previously | ||
* @throws {UnregisteredExtendedDependencyError} If there was not a previously | ||
* defined dependency with that id | ||
* @throws {IllegalExtensionError} If trying to extend a dependency | ||
* registered as value | ||
*/ | ||
@@ -117,7 +158,10 @@ extend: function(id, definition) { | ||
* @return {BlisterContainer} The container itself | ||
* @throws {TypeError} If the id is not a string | ||
* @throws {TypeError} If value is not a function when the type is | ||
* 'SINGLETON' or 'FACTORY' | ||
* @throws {UnregisteredExtendedDependencyError} When trying to extend an | ||
* unregistered dependency | ||
*/ | ||
_set: function(id, value, type, isExtension) { | ||
if (typeof id !== 'string') { | ||
throw new TypeError('The dependency id must be a string: ' + id); | ||
} | ||
checkId(id); | ||
@@ -127,17 +171,15 @@ var originalWrapper = isExtension ? this._deps[id] : undefined; | ||
if (!originalWrapper) { | ||
throw new MissingExtendedDependencyError(); | ||
throw new UnregisteredExtendedDependencyError(); | ||
} | ||
type = originalWrapper.type; | ||
if (type === VALUE) { | ||
type = SINGLETON; | ||
} | ||
} | ||
if (typeof value !== 'function' && type !== VALUE) { | ||
throw new TypeError( | ||
'The argument must be a function: ' + | ||
value); | ||
if (typeof value !== 'function' && (isExtension || type !== VALUE)) { | ||
throw new TypeError('The argument must be a function: ' + value); | ||
} | ||
if (type === VALUE && isExtension) { | ||
throw new IllegalExtensionError(); | ||
} | ||
this._deps[id] = wrappers.create(type, value, this, originalWrapper); | ||
@@ -217,16 +259,31 @@ return this; | ||
* @extends {Error} | ||
* @param {string} [message='Cannot get an unregistered dependency'] | ||
* @private | ||
*/ | ||
function UnregisteredDependencyError(message) { | ||
this.name = 'UnregisteredDependencyError'; | ||
this.message = message || 'Cannot get an unregistered dependency'; | ||
} | ||
UnregisteredDependencyError.prototype = new ErrorWrapper(); | ||
UnregisteredDependencyError.constructor = UnregisteredDependencyError; | ||
/** | ||
* @class | ||
* @extends {Error} | ||
* @param {string} [message='Cannot extend a dependency not previously set'] | ||
* @private | ||
*/ | ||
function MissingExtendedDependencyError(message) { | ||
this.name = 'MissingExtendedDependencyError'; | ||
function UnregisteredExtendedDependencyError(message) { | ||
this.name = 'UnregisteredExtendedDependencyError'; | ||
this.message = message || 'Cannot extend a dependency not previously set'; | ||
} | ||
MissingExtendedDependencyError.prototype = new ErrorWrapper(); | ||
MissingExtendedDependencyError.constructor = MissingExtendedDependencyError; | ||
UnregisteredExtendedDependencyError.prototype = new ErrorWrapper(); | ||
UnregisteredExtendedDependencyError.constructor = UnregisteredExtendedDependencyError; | ||
module.exports = { | ||
IllegalExtensionError: IllegalExtensionError, | ||
MissingExtendedDependencyError: MissingExtendedDependencyError | ||
UnregisteredDependencyError: UnregisteredDependencyError, | ||
UnregisteredExtendedDependencyError: UnregisteredExtendedDependencyError | ||
}; | ||
@@ -266,3 +323,3 @@ | ||
if (originalWrapper) { | ||
return value.call(container, container, originalWrapper()); | ||
return value.call(container, originalWrapper(), container); | ||
} | ||
@@ -287,3 +344,3 @@ return value.call(container, container); | ||
if (originalWrapper) { | ||
cachedValue = value.call(container, container, originalWrapper()); | ||
cachedValue = value.call(container, originalWrapper(), container); | ||
} else { | ||
@@ -290,0 +347,0 @@ cachedValue = value.call(container, container); |
@@ -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,t,r){function o(s,u){if(!t[s]){if(!n[s]){var f="function"==typeof require&&require;if(!u&&f)return f(s,!0);if(i)return i(s,!0);var c=new Error("Cannot find module '"+s+"'");throw c.code="MODULE_NOT_FOUND",c}var d=t[s]={exports:{}};n[s][0].call(d.exports,function(e){var t=n[s][1][e];return o(t?t:e)},d,d.exports,e,n,t,r)}return t[s].exports}for(var i="function"==typeof require&&require,s=0;s<r.length;s++)o(r[s]);return o}({1:[function(e,n){"use strict";function t(){this._deps={}}var r=e("./wrappers"),o=e("./errors"),i=o.IllegalExtensionError,s=o.MissingExtendedDependencyError,u="value",f="singleton",c="factory";t.IllegalExtensionError=i,t.MissingExtendedDependencyError=s,t.prototype={constructor:t,get:function(e){var n=this._deps[e];return n&&n()},value:function(e,n){return this._set(e,n,u,!1)},factory:function(e,n){return this._set(e,n,c,!1)},singleton:function(e,n){return this._set(e,n,f,!1)},extend:function(e,n){return this._set(e,n,void 0,!0)},_set:function(e,n,t,o){if("string"!=typeof e)throw new TypeError("The dependency id must be a string: "+e);var f=o?this._deps[e]:void 0;if(o){if(!f)throw new s;t=f.type}if("function"!=typeof n&&t!==u)throw new TypeError("The argument must be a function: "+n);if(t===u&&o)throw new i;return this._deps[e]=r.create(t,n,this,f),this},register:function(e){return e.register(this),this}},n.exports=t},{"./errors":2,"./wrappers":3}],2:[function(e,n){"use strict";function t(){}function r(e){this.name="IllegalExtensionError",this.message=e||"Values cannot be extended. Redefine them instead"}function o(e){this.name="MissingExtendedDependencyError",this.message=e||"Cannot extend a dependency not previously set"}t.prototype=Error.prototype,r.prototype=new t,r.constructor=r,o.prototype=new t,o.constructor=o,n.exports={IllegalExtensionError:r,MissingExtendedDependencyError:o}},{}],3:[function(e,n){"use strict";var t={value:function(e){return function(){return e}},factory:function(e,n,t){return function(){return t?e.call(n,n,t()):e.call(n,n)}},singleton:function(e,n,t){var r,o=!1;return function(){return o||(o=!0,r=t?e.call(n,n,t()):e.call(n,n),e=null),r}},create:function(e,n,t,r){var o=this[e](n,t,r);return o.type=e,o}};n.exports=t},{}]},{},[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 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||(o=!0,t=r?e.call(n,r(),n):e.call(n,n),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)}); | ||
//# sourceMappingURL=blister.min.js.map |
{ | ||
"name": "blister", | ||
"version": "0.10.0", | ||
"version": "0.11.0", | ||
"author": "Rubén Norte <rubennorte@gmail.com>", | ||
@@ -15,4 +15,6 @@ "description": "Minimalist dependency injection container for JavaScript", | ||
"provider", | ||
"service", | ||
"singleton", | ||
"factory", | ||
"value", | ||
"configuration", | ||
@@ -19,0 +21,0 @@ "config", |
@@ -41,2 +41,13 @@ # Blister | ||
#### Getting dependencies | ||
Example: | ||
```js | ||
container.value('name', 'Robert'); | ||
container.get('name'); //> 'Robert' | ||
container.has('name'); //> true | ||
``` | ||
#### Setting values | ||
@@ -58,3 +69,3 @@ | ||
#### Setting singletons | ||
#### Setting services | ||
@@ -66,3 +77,3 @@ Dependencies can be registered as a singleton functions. Those functions are executed the first time the associated dependency is requested. The result of the functions is returned and cached for subsequent calls. | ||
```js | ||
container.singleton('host', function(c) { | ||
container.service('host', function(c) { | ||
console.log('called'); | ||
@@ -95,10 +106,8 @@ return c.get('protocol') === 'http://' ? | ||
The extension preserves the type of the original dependency (factory or singleton). | ||
The extension preserves the type of the original dependency (factory or service). | ||
Value dependencies cannot be extended. They must be redefined instead. | ||
Example: | ||
```js | ||
container.singleton('some-service', function() { | ||
container.service('some-service', function() { | ||
return service; | ||
@@ -108,3 +117,3 @@ }); | ||
// after that definition | ||
container.extend('some-service', function(c, service) { | ||
container.extend('some-service', function(service, c) { | ||
service.addLogger(c.get('logger')); | ||
@@ -116,3 +125,3 @@ }); | ||
If the previous dependency is not used in the definition of the extension, it can be replaced using `value`, `factory` or `singleton` instead. | ||
If the previous dependency is not used in the definition of the extension, it can be replaced using `value`, `factory` or `service` instead. | ||
@@ -128,4 +137,4 @@ #### Registering service providers | ||
register: function(container) { | ||
container.set('protocol', 'http://'); | ||
container.set('host', 'example.com'); | ||
container.value('protocol', 'http://'); | ||
container.value('host', 'example.com'); | ||
} | ||
@@ -132,0 +141,0 @@ }; |
@@ -7,3 +7,4 @@ 'use strict'; | ||
var IllegalExtensionError = errors.IllegalExtensionError; | ||
var MissingExtendedDependencyError = errors.MissingExtendedDependencyError; | ||
var UnregisteredDependencyError = errors.UnregisteredDependencyError; | ||
var UnregisteredExtendedDependencyError = errors.UnregisteredExtendedDependencyError; | ||
@@ -14,3 +15,27 @@ var VALUE = 'value'; | ||
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 | ||
* @throws {TypeError} If the passed id is not a string | ||
*/ | ||
function checkId(id) { | ||
if (typeof id !== 'string') { | ||
throw new TypeError('The dependency id must be a string: ' + id); | ||
} | ||
} | ||
/** | ||
* Dependency injection container constructor | ||
@@ -30,3 +55,4 @@ * | ||
BlisterContainer.IllegalExtensionError = IllegalExtensionError; | ||
BlisterContainer.MissingExtendedDependencyError = MissingExtendedDependencyError; | ||
BlisterContainer.UnregisteredDependencyError = UnregisteredDependencyError; | ||
BlisterContainer.UnregisteredExtendedDependencyError = UnregisteredExtendedDependencyError; | ||
@@ -38,2 +64,14 @@ BlisterContainer.prototype = { | ||
/** | ||
* Indicates if there is a registered dependency with the given id | ||
* @param {string} id | ||
* @return {boolean} | ||
* @throws {TypeError} If the id is not a string | ||
*/ | ||
has: function(id) { | ||
checkId(id); | ||
return hasOwnProp(this._deps, id); | ||
}, | ||
/** | ||
* Returns the dependency set with the given id, | ||
@@ -45,4 +83,9 @@ * or undefined if it is not present | ||
get: function(id) { | ||
var wrapper = this._deps[id]; | ||
return wrapper && wrapper(); | ||
checkId(id); | ||
if (!hasOwnProp(this._deps, id)) { | ||
throw new UnregisteredDependencyError('Cannot get unregistered dependency ' + id); | ||
} | ||
return this._deps[id](); | ||
}, | ||
@@ -74,11 +117,11 @@ | ||
/** | ||
* Registers the given singleton function with the specified id | ||
* Registers the given service function with the specified id | ||
* @param {string} id | ||
* @param {Function} singletonFn | ||
* @param {Function} serviceFn | ||
* @return {BlisterContainer} this | ||
* @throws {TypeError} If id is not a string | ||
* @throws {TypeError} If singletonFn is not a function | ||
* @throws {TypeError} If serviceFn is not a function | ||
*/ | ||
singleton: function(id, singletonFn) { | ||
return this._set(id, singletonFn, SINGLETON, false); | ||
service: function(id, serviceFn) { | ||
return this._set(id, serviceFn, SINGLETON, false); | ||
}, | ||
@@ -88,3 +131,3 @@ | ||
* Extends a previously defined dependency with the same type: | ||
* factory or singleton | ||
* service or factory | ||
* @param {string} id | ||
@@ -95,6 +138,4 @@ * @param {Function} definition | ||
* @throws {TypeError} If definition is not a function | ||
* @throws {MissingExtendedDependencyError} If there was not a previously | ||
* @throws {UnregisteredExtendedDependencyError} If there was not a previously | ||
* defined dependency with that id | ||
* @throws {IllegalExtensionError} If trying to extend a dependency | ||
* registered as value | ||
*/ | ||
@@ -116,7 +157,10 @@ extend: function(id, definition) { | ||
* @return {BlisterContainer} The container itself | ||
* @throws {TypeError} If the id is not a string | ||
* @throws {TypeError} If value is not a function when the type is | ||
* 'SINGLETON' or 'FACTORY' | ||
* @throws {UnregisteredExtendedDependencyError} When trying to extend an | ||
* unregistered dependency | ||
*/ | ||
_set: function(id, value, type, isExtension) { | ||
if (typeof id !== 'string') { | ||
throw new TypeError('The dependency id must be a string: ' + id); | ||
} | ||
checkId(id); | ||
@@ -126,17 +170,15 @@ var originalWrapper = isExtension ? this._deps[id] : undefined; | ||
if (!originalWrapper) { | ||
throw new MissingExtendedDependencyError(); | ||
throw new UnregisteredExtendedDependencyError(); | ||
} | ||
type = originalWrapper.type; | ||
if (type === VALUE) { | ||
type = SINGLETON; | ||
} | ||
} | ||
if (typeof value !== 'function' && type !== VALUE) { | ||
throw new TypeError( | ||
'The argument must be a function: ' + | ||
value); | ||
if (typeof value !== 'function' && (isExtension || type !== VALUE)) { | ||
throw new TypeError('The argument must be a function: ' + value); | ||
} | ||
if (type === VALUE && isExtension) { | ||
throw new IllegalExtensionError(); | ||
} | ||
this._deps[id] = wrappers.create(type, value, this, originalWrapper); | ||
@@ -143,0 +185,0 @@ return this; |
@@ -29,16 +29,31 @@ 'use strict'; | ||
* @extends {Error} | ||
* @param {string} [message='Cannot get an unregistered dependency'] | ||
* @private | ||
*/ | ||
function UnregisteredDependencyError(message) { | ||
this.name = 'UnregisteredDependencyError'; | ||
this.message = message || 'Cannot get an unregistered dependency'; | ||
} | ||
UnregisteredDependencyError.prototype = new ErrorWrapper(); | ||
UnregisteredDependencyError.constructor = UnregisteredDependencyError; | ||
/** | ||
* @class | ||
* @extends {Error} | ||
* @param {string} [message='Cannot extend a dependency not previously set'] | ||
* @private | ||
*/ | ||
function MissingExtendedDependencyError(message) { | ||
this.name = 'MissingExtendedDependencyError'; | ||
function UnregisteredExtendedDependencyError(message) { | ||
this.name = 'UnregisteredExtendedDependencyError'; | ||
this.message = message || 'Cannot extend a dependency not previously set'; | ||
} | ||
MissingExtendedDependencyError.prototype = new ErrorWrapper(); | ||
MissingExtendedDependencyError.constructor = MissingExtendedDependencyError; | ||
UnregisteredExtendedDependencyError.prototype = new ErrorWrapper(); | ||
UnregisteredExtendedDependencyError.constructor = UnregisteredExtendedDependencyError; | ||
module.exports = { | ||
IllegalExtensionError: IllegalExtensionError, | ||
MissingExtendedDependencyError: MissingExtendedDependencyError | ||
UnregisteredDependencyError: UnregisteredDependencyError, | ||
UnregisteredExtendedDependencyError: UnregisteredExtendedDependencyError | ||
}; |
@@ -32,3 +32,3 @@ 'use strict'; | ||
if (originalWrapper) { | ||
return value.call(container, container, originalWrapper()); | ||
return value.call(container, originalWrapper(), container); | ||
} | ||
@@ -53,3 +53,3 @@ return value.call(container, container); | ||
if (originalWrapper) { | ||
cachedValue = value.call(container, container, originalWrapper()); | ||
cachedValue = value.call(container, originalWrapper(), container); | ||
} else { | ||
@@ -56,0 +56,0 @@ cachedValue = value.call(container, container); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
58909
639
164