Comparing version 0.0.5 to 1.0.0
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.iniettore=e()}}(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);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.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(_dereq_,module,exports){ | ||
'use strict' | ||
var generateType = _dereq_('./generateType'); | ||
var mod$1 = _dereq_('./signals');var ACQUIRE = mod$1.ACQUIRE;var RELEASE = mod$1.RELEASE;var DISPOSE = mod$1.DISPOSE; | ||
var generateMask = _dereq_('./utils').generateMask; | ||
var mod$4 = _dereq_('./signals');var ACQUIRE = mod$4.ACQUIRE;var RELEASE = mod$4.RELEASE;var DISPOSE = mod$4.DISPOSE; | ||
var PROVIDER = _dereq_('./options').PROVIDER; | ||
var resolvers = _dereq_('./resolvers'); | ||
var VALUE = _dereq_('./options').VALUE; | ||
var log = _dereq_('./log'); | ||
var createContext = _dereq_('./createContext'); | ||
var ALIAS_IDX = 0 | ||
var VALUE_IDX = 1 | ||
var TYPE_IDX = 2 | ||
var DEPS_IDX = 3 | ||
var CONTAINER_ALIAS = '$container' | ||
function noop() {} | ||
function Container(conf, mappings) { | ||
this.$Container_resolvers = resolvers | ||
this.$Container_mappings = mappings || {} | ||
this.$Container_resolving = {} | ||
this.$Container_pending = [] | ||
function Container(conf, logger, mappings, signalRelease) { | ||
var context | ||
if (typeof conf !== 'function') { | ||
throw new Error('Invalid container creation, missing contribution function') | ||
} | ||
this.$Container0 = mappings || {} | ||
this.$Container1 = resolvers | ||
this.$Container2 = logger | ||
this.$Container3 = {} | ||
this.$Container4 = [] | ||
this.$Container5 = {} | ||
this.$Container6 = signalRelease || noop | ||
this.bind(CONTAINER_ALIAS, this).as(VALUE).done() | ||
if (typeof conf === 'function') { | ||
conf(this) | ||
if (this.$Container_pending.length) { | ||
this.$Container_done() | ||
} | ||
} | ||
context = this.$Container7 = createContext(this.$Container8.bind(this)) | ||
context.map(CONTAINER_ALIAS).to(this).as(VALUE) | ||
context.flush() | ||
conf(context) | ||
context.flush() | ||
} | ||
Container.prototype.bind=function(alias, value) { | ||
this.$Container_pending = [alias, value] | ||
return { | ||
as: function() {var flags=Array.prototype.slice.call(arguments,0); | ||
this.$Container_pending[TYPE_IDX] = generateType(flags) | ||
return { | ||
bind: function() {var args=Array.prototype.slice.call(arguments,0); | ||
this.$Container_done() | ||
return this.bind.apply(this, args) | ||
}.bind(this), | ||
inject: function() {var deps=Array.prototype.slice.call(arguments,0); | ||
this.$Container_pending[DEPS_IDX] = deps | ||
return { | ||
bind: function() {var args=Array.prototype.slice.call(arguments,0); | ||
this.$Container_done() | ||
return this.bind.apply(this, args) | ||
}.bind(this), | ||
done: function() {return this.$Container_done();}.bind(this) | ||
} | ||
}.bind(this), | ||
done: function() {return this.$Container_done();}.bind(this) | ||
} | ||
}.bind(this) | ||
} | ||
}; | ||
Container.prototype.get=function(alias, transients) { | ||
return this.$Container2.log(("resolving '" + alias + "'"), function() { | ||
if (this.$Container3[alias]) { throw new Error(("Circular dependency detected while resolving '" + alias + "'")) } | ||
if (!(alias in this.$Container0)) { throw new Error(("'" + alias + "' is not available. Has it ever been registered?.")) } | ||
var value, error | ||
log(("Resolving " + alias)) | ||
if (this.$Container_resolving[alias]) { throw new Error(("Circular dependency detected while resolving '" + alias + "'")) } | ||
if (!(alias in this.$Container_mappings)) { throw new Error(("'" + alias + "' is not available. Has it ever been registered?.")) } | ||
this.$Container_resolving[alias] = true | ||
try { | ||
value = this.$Container_mappings[alias](ACQUIRE) | ||
} catch(err) { | ||
err.message = ("Failed while resolving '" + alias + "' due to:\n\t" + err.message) | ||
throw err | ||
} | ||
this.$Container_resolving[alias] = false | ||
log.done() | ||
return value | ||
this.$Container3[alias] = true | ||
try { | ||
return this.$Container0[alias](ACQUIRE) | ||
} catch(err) { | ||
err.message = ("Failed while resolving '" + alias + "' due to:\n\t" + err.message) | ||
throw err | ||
} finally { | ||
this.$Container3[alias] = false | ||
} | ||
}.bind(this)) | ||
}; | ||
@@ -95,10 +58,12 @@ | ||
get: function(alias) { | ||
var context = this.$Container7 | ||
var dep | ||
for (dep in transientsDeps) { | ||
this.bind(dep, transientsDeps[dep]).as(VALUE).done() | ||
context.map(dep).to(transientsDeps[dep]).as(VALUE) | ||
} | ||
context.flush() | ||
this.get(alias) | ||
for (dep in transientsDeps) { | ||
this.$Container_unbind(dep) | ||
this.$Container9(dep) | ||
} | ||
@@ -111,5 +76,5 @@ }.bind(this) | ||
try { | ||
this.$Container_mappings[alias](RELEASE) | ||
this.$Container0[alias](RELEASE) | ||
} catch(err) { | ||
err.message = ("Failed while disposing '" + alias + "' due to:\n\t" + err.message) | ||
err.message = ("Failed while releasing '" + alias + "' due to:\n\t" + err.message) | ||
throw err | ||
@@ -120,22 +85,39 @@ } | ||
Container.prototype.createChild=function(conf) { | ||
return new Container(conf, Object.create(this.$Container_mappings)) | ||
var id = Object.keys(this.$Container5).length + 1 | ||
var child = new Container(conf, this.$Container2, Object.create(this.$Container0), this.$Containera.bind(this, id)) | ||
this.$Container5[id] = child | ||
return child | ||
}; | ||
Container.prototype.createBlueprint=function(alias, blueprint) { | ||
return { | ||
exports: function(mapping) { | ||
return { | ||
done: function() {return this.bind(alias, function() {return this.createChild(blueprint).get(mapping);}.bind(this)).as(PROVIDER).done();}.bind(this) | ||
} | ||
}.bind(this), | ||
done: function() {return this.bind(alias, function() {return this.createChild(blueprint);}.bind(this)).as(PROVIDER).done();}.bind(this) | ||
Container.prototype.dispose=function() { | ||
this.$Containerb() | ||
this.$Containerc() | ||
this.$Container6() | ||
this.$Container6 = noop | ||
}; | ||
Container.prototype.$Containerb=function() { | ||
var children = this.$Container5 | ||
var id | ||
for (id in children) { | ||
/* istanbul ignore else */ | ||
if (children.hasOwnProperty(id)) { | ||
children[id].dispose() | ||
this.$Containera(id) | ||
} | ||
} | ||
}; | ||
Container.prototype.dispose=function() { | ||
Container.prototype.$Containera=function(id) { | ||
delete this.$Container5[id] | ||
}; | ||
var mappings = this.$Container_mappings | ||
Container.prototype.$Containerc=function() { | ||
var mappings = this.$Container0 | ||
var alias | ||
for (alias in mappings) { | ||
/* istanbul ignore else */ | ||
if (mappings.hasOwnProperty(alias)) { | ||
@@ -148,2 +130,3 @@ try { | ||
} | ||
delete mappings[alias] | ||
} | ||
@@ -153,20 +136,16 @@ } | ||
Container.prototype.$Container_done=function() { | ||
Container.prototype.$Container8=function(alias, value, type, deps) { | ||
if ( !(type in this.$Container1) ) { | ||
throw new Error('Invalid flags combination. See documentation for valid flags combinations.') | ||
} | ||
this.$Container0[alias] = this.$Container1[type].call(null, value, this.$Containerd.bind(this, deps), this.$Containere.bind(this, deps)) | ||
var pending = this.$Container_pending | ||
var deps = pending[DEPS_IDX] ? pending[DEPS_IDX] : [] | ||
var resolver = this.$Container_resolvers[ pending[TYPE_IDX] ](pending[VALUE_IDX], this.$Container_resolve.bind(this, deps), this.$Container_release.bind(this, deps)) | ||
this.$Container_mappings[pending[ALIAS_IDX]] = resolver | ||
this.$Container_pending = [] | ||
return this | ||
}; | ||
Container.prototype.$Container_unbind=function(alias) { | ||
this.$Container_mappings[alias](DISPOSE) | ||
delete this.$Container_mappings[alias] | ||
Container.prototype.$Container9=function(alias) { | ||
this.$Container0[alias](DISPOSE) | ||
delete this.$Container0[alias] | ||
}; | ||
Container.prototype.$Container_release=function(deps) { | ||
Container.prototype.$Containere=function(deps) { | ||
return deps.forEach(function(dep) { | ||
@@ -177,3 +156,3 @@ this.release(dep) | ||
Container.prototype.$Container_resolve=function(deps) { | ||
Container.prototype.$Containerd=function(deps) { | ||
return deps.map(function(dep) { | ||
@@ -187,19 +166,158 @@ return this.get(dep) | ||
},{"./generateType":2,"./log":5,"./options":6,"./resolvers":7,"./signals":8}],2:[function(_dereq_,module,exports){ | ||
},{"./createContext":3,"./options":6,"./resolvers":7,"./signals":8,"./utils":10}],2:[function(_dereq_,module,exports){ | ||
'use strict' | ||
module.exports = function generateType (flags) { | ||
return flags.reduce(function(prev, curr) {return prev | curr;}, 0) | ||
function Logger(options) { | ||
this.$Logger0 = options | ||
this.$Logger1 = 0 | ||
this.$Logger2 = {} | ||
} | ||
Logger.prototype.log=function(message, func) { | ||
var result | ||
if ("production" !== 'production' && this.$Logger0.debug) { | ||
this.$Logger3(message) | ||
result = func() | ||
this.$Logger4(message) | ||
return result | ||
} | ||
return func() | ||
}; | ||
Logger.prototype.$Logger3=function(message) { | ||
var indentation = this.$Logger5() | ||
var now = new Date() | ||
this.$Logger2[message] = now | ||
console.log(("[" + now.toISOString() + "] " + indentation + "Starting " + message + "...")) | ||
}; | ||
Logger.prototype.$Logger4=function(message) { | ||
var indentation = this.$Logger6() | ||
var now = new Date() | ||
var elapsed = now - this.$Logger2[message] | ||
console.log(("[" + now.toISOString() + "] " + indentation + "Finished " + message + " after " + elapsed + " ms")) | ||
}; | ||
Logger.prototype.$Logger5=function() { | ||
return Array(++this.$Logger1).join(' ') | ||
}; | ||
Logger.prototype.$Logger6=function() { | ||
return Array(this.$Logger1--).join(' ') | ||
}; | ||
module.exports = Logger | ||
},{}],3:[function(_dereq_,module,exports){ | ||
'use strict'; | ||
var generateMask = _dereq_('./utils').generateMask; | ||
var mod$8 = _dereq_('./options');var BLUEPRINT = mod$8.BLUEPRINT;var PROVIDER = mod$8.PROVIDER; | ||
var ALIAS_IDX = 0 | ||
var VALUE_IDX = 1 | ||
var TYPE_IDX = 2 | ||
var DEPS_IDX = 3 | ||
function createChildContainerFactory(conf) { | ||
return function (container) { | ||
return container.createChild(conf) | ||
} | ||
} | ||
},{}],3:[function(_dereq_,module,exports){ | ||
function createExporter(containerFactory, exportAlias) { | ||
return function (container) { | ||
return containerFactory(container).get(exportAlias) | ||
} | ||
} | ||
module.exports = function createContext(contribute) { | ||
var pending = [] | ||
var context = { | ||
map: function(alias) { | ||
context.flush() | ||
pending.push(alias) | ||
return { | ||
to: function(value) { | ||
pending.push(value) | ||
return { | ||
as: function() {var flags=Array.prototype.slice.call(arguments,0); | ||
if (flags.length === 1 && flags[0] === BLUEPRINT) { | ||
// test if VALUE is a function | ||
flags = [PROVIDER] | ||
pending[VALUE_IDX] = createChildContainerFactory(pending[VALUE_IDX]) | ||
pending.push(generateMask(flags)) | ||
pending.push(['$container']) | ||
return { | ||
exports: function(alias) { | ||
pending[VALUE_IDX] = createExporter(pending[VALUE_IDX], alias) | ||
return { | ||
map: context.map | ||
} | ||
}, | ||
map: context.map | ||
} | ||
} | ||
pending.push(generateMask(flags)) | ||
return { | ||
map: context.map, | ||
injecting: function() {var deps=Array.prototype.slice.call(arguments,0); | ||
pending.push(deps) | ||
return { | ||
map: context.map | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
flush: function() { | ||
var deps = pending[DEPS_IDX] || [] | ||
if (pending.length > 2) { | ||
contribute(pending[ALIAS_IDX], pending[VALUE_IDX], pending[TYPE_IDX], deps) | ||
pending = [] | ||
} | ||
} | ||
} | ||
return context | ||
} | ||
},{"./options":6,"./utils":10}],4:[function(_dereq_,module,exports){ | ||
'use strict' | ||
var Container = _dereq_('./Container'); | ||
var Logger = _dereq_('./Logger'); | ||
function create(conf) { | ||
return new Container(conf) | ||
function create(conf, options) { | ||
var logger | ||
options = options || {} | ||
logger = new Logger(options) | ||
return new Container(conf, logger) | ||
} module.exports.create = create; | ||
},{"./Container":1}],4:[function(_dereq_,module,exports){ | ||
},{"./Container":1,"./Logger":2}],5:[function(_dereq_,module,exports){ | ||
'use strict' | ||
@@ -220,22 +338,2 @@ | ||
},{}],5:[function(_dereq_,module,exports){ | ||
'use strict' | ||
var count = 0 | ||
var indentation, message | ||
function log(msg) { | ||
count++ | ||
message = msg | ||
indentation = Array(count * 2).join(' ') | ||
// console.log(`${indentation}${message}: BEGIN`) | ||
} | ||
log.done = function () { | ||
// console.log(`${indentation}${message}: DONE`)npm | ||
count-- | ||
} | ||
module.exports = log | ||
},{}],6:[function(_dereq_,module,exports){ | ||
@@ -250,3 +348,6 @@ 'use strict' | ||
'FUNCTION', | ||
'INSTANCE' | ||
'INSTANCE', | ||
'PERSISTENT', | ||
'TRANSIENT', | ||
'BLUEPRINT' | ||
].reduce(function (options, flag, idx) { | ||
@@ -260,40 +361,45 @@ options[flag] = Math.pow(2, idx) | ||
var mod$4 = _dereq_('./utils'); | ||
var leftCurryTwice = mod$4.leftCurryTwice; | ||
var identity = mod$4.identity; | ||
var compose = mod$4.compose; | ||
var partial = mod$4.partial; | ||
var mod$6 = _dereq_('./utils'); | ||
var leftCurryTwice = mod$6.leftCurryTwice; | ||
var identity = mod$6.identity; | ||
var compose = mod$6.compose; | ||
var partial = mod$6.partial; | ||
var resolveDeps = mod$4.resolveDeps; | ||
var invoke = mod$4.invoke; | ||
var resolveDeps = mod$6.resolveDeps; | ||
var invoke = mod$6.invoke; | ||
var instanciate = mod$4.instanciate; | ||
var singletonify = mod$4.singletonify; | ||
var instanciate = mod$6.instanciate; | ||
var singletonify = mod$6.singletonify; | ||
var generateMask = mod$6.generateMask; | ||
var generateType = _dereq_('./generateType'); | ||
var mod$5 = _dereq_('./options'); | ||
var VALUE = mod$5.VALUE; | ||
var PROVIDER = mod$5.PROVIDER; | ||
var CONSTRUCTOR = mod$5.CONSTRUCTOR; | ||
var SINGLETON = mod$5.SINGLETON; | ||
var FUNCTION = mod$5.FUNCTION; | ||
var INSTANCE = mod$5.INSTANCE; | ||
var mod$7 = _dereq_('./options'); | ||
var VALUE = mod$7.VALUE; | ||
var PROVIDER = mod$7.PROVIDER; | ||
var CONSTRUCTOR = mod$7.CONSTRUCTOR; | ||
var SINGLETON = mod$7.SINGLETON; | ||
var FUNCTION = mod$7.FUNCTION; | ||
var INSTANCE = mod$7.INSTANCE; | ||
var TRANSIENT = mod$7.TRANSIENT; | ||
var PERSISTENT = mod$7.PERSISTENT; | ||
var resolvers = {} | ||
resolvers[ generateType([VALUE]) ] = compose(leftCurryTwice, resolveDeps)(identity) | ||
resolvers[ generateType([FUNCTION]) ] = compose(leftCurryTwice, resolveDeps)(partial) | ||
resolvers[ generateType([CONSTRUCTOR]) ] = compose(leftCurryTwice, resolveDeps)(instanciate) | ||
resolvers[ generateType([CONSTRUCTOR, SINGLETON]) ] = singletonify(instanciate) | ||
resolvers[ generateType([PROVIDER]) ] = compose(leftCurryTwice, resolveDeps)(invoke) | ||
resolvers[ generateType([SINGLETON, PROVIDER]) ] = singletonify(invoke) | ||
resolvers[ generateMask([VALUE]) ] = compose(leftCurryTwice, resolveDeps)(identity) | ||
resolvers[ generateMask([FUNCTION]) ] = compose(leftCurryTwice, resolveDeps)(partial) | ||
resolvers[ generateMask([CONSTRUCTOR]) ] = compose(leftCurryTwice, resolveDeps)(instanciate) | ||
resolvers[ generateMask([TRANSIENT, CONSTRUCTOR, SINGLETON]) ] = singletonify(instanciate) | ||
resolvers[ generateMask([PERSISTENT, CONSTRUCTOR, SINGLETON]) ] = singletonify(instanciate, true) | ||
resolvers[ generateMask([PROVIDER]) ] = compose(leftCurryTwice, resolveDeps)(invoke) | ||
resolvers[ generateMask([TRANSIENT, SINGLETON, PROVIDER]) ] = singletonify(invoke) | ||
resolvers[ generateMask([PERSISTENT, SINGLETON, PROVIDER]) ] = singletonify(invoke, true) | ||
// aliases | ||
resolvers[ generateType([INSTANCE]) ] = resolvers[ generateType([VALUE]) ] | ||
resolvers[ generateMask([INSTANCE]) ] = resolvers[ generateMask([VALUE]) ] | ||
module.exports = resolvers | ||
},{"./generateType":2,"./options":6,"./utils":10}],8:[function(_dereq_,module,exports){ | ||
},{"./options":6,"./utils":10}],8:[function(_dereq_,module,exports){ | ||
'use strict' | ||
@@ -310,6 +416,8 @@ | ||
var mod$7 = _dereq_('./signals');var ACQUIRE = mod$7.ACQUIRE;var RELEASE = mod$7.RELEASE;var DISPOSE = mod$7.DISPOSE; | ||
var mod$9 = _dereq_('./signals');var ACQUIRE = mod$9.ACQUIRE;var RELEASE = mod$9.RELEASE;var DISPOSE = mod$9.DISPOSE; | ||
module.exports = function singletonify(create) { | ||
module.exports = function singletonify(create, persistent) { | ||
persistent = persistent || false | ||
return function (value, resolveDeps, releaseDeps) { | ||
@@ -339,3 +447,3 @@ | ||
count-- | ||
if (count <= 0) { | ||
if (count == 0 && !persistent) { | ||
releaseDeps() | ||
@@ -347,4 +455,6 @@ dispose() | ||
handlers[DISPOSE] = function (value) { | ||
releaseDeps() | ||
dispose() | ||
if (instance) { | ||
releaseDeps() | ||
dispose() | ||
} | ||
} | ||
@@ -402,6 +512,10 @@ | ||
function generateMask(flags) { | ||
return flags.reduce(function(prev, curr) {return prev | curr;}, 0) | ||
} module.exports.generateMask = generateMask; | ||
module.exports.instanciate = instanciate;module.exports.singletonify = singletonify; | ||
},{"./instanciate":4,"./singletonify":9}]},{},[3]) | ||
(3) | ||
},{"./instanciate":5,"./singletonify":9}]},{},[4]) | ||
(4) | ||
}); |
@@ -1,1 +0,1 @@ | ||
!function(n){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=n();else if("function"==typeof define&&define.amd)define([],n);else{var t;"undefined"!=typeof window?t=window:"undefined"!=typeof global?t=global:"undefined"!=typeof self&&(t=self),t.iniettore=n()}}(function(){return function n(t,e,i){function r(s,a){if(!e[s]){if(!t[s]){var u="function"==typeof require&&require;if(!a&&u)return u(s,!0);if(o)return o(s,!0);throw new Error("Cannot find module '"+s+"'")}var p=e[s]={exports:{}};t[s][0].call(p.exports,function(n){var e=t[s][1][n];return r(e?e:n)},p,p.exports,n,t,e,i)}return e[s].exports}for(var o="function"==typeof require&&require,s=0;s<i.length;s++)r(i[s]);return r}({1:[function(n,t){"use strict";function e(n,t){this.$Container_resolvers=p,this.$Container_mappings=t||{},this.$Container_resolving={},this.$Container_pending=[],this.bind(g,this).as(c).done(),"function"==typeof n&&(n(this),this.$Container_pending.length&&this.$Container_done())}var i=n("./generateType"),r=n("./signals"),o=r.ACQUIRE,s=r.RELEASE,a=r.DISPOSE,u=n("./options").PROVIDER,p=n("./resolvers"),c=n("./options").VALUE,f=n("./log"),l=0,d=1,h=2,y=3,g="$container";e.prototype.bind=function(n,t){return this.$Container_pending=[n,t],{as:function(){var n=Array.prototype.slice.call(arguments,0);return this.$Container_pending[h]=i(n),{bind:function(){var n=Array.prototype.slice.call(arguments,0);return this.$Container_done(),this.bind.apply(this,n)}.bind(this),inject:function(){var n=Array.prototype.slice.call(arguments,0);return this.$Container_pending[y]=n,{bind:function(){var n=Array.prototype.slice.call(arguments,0);return this.$Container_done(),this.bind.apply(this,n)}.bind(this),done:function(){return this.$Container_done()}.bind(this)}}.bind(this),done:function(){return this.$Container_done()}.bind(this)}}.bind(this)}},e.prototype.get=function(n){var t;if(f("Resolving "+n),this.$Container_resolving[n])throw new Error("Circular dependency detected while resolving '"+n+"'");if(!(n in this.$Container_mappings))throw new Error("'"+n+"' is not available. Has it ever been registered?.");this.$Container_resolving[n]=!0;try{t=this.$Container_mappings[n](o)}catch(e){throw e.message="Failed while resolving '"+n+"' due to:\n "+e.message,e}return this.$Container_resolving[n]=!1,f.done(),t},e.prototype.using=function(n){return{get:function(t){var e;for(e in n)this.bind(e,n[e]).as(c).done();this.get(t);for(e in n)this.$Container_unbind(e)}.bind(this)}},e.prototype.release=function(n){try{this.$Container_mappings[n](s)}catch(t){throw t.message="Failed while disposing '"+n+"' due to:\n "+t.message,t}},e.prototype.createChild=function(n){return new e(n,Object.create(this.$Container_mappings))},e.prototype.createBlueprint=function(n,t){return{exports:function(e){return{done:function(){return this.bind(n,function(){return this.createChild(t).get(e)}.bind(this)).as(u).done()}.bind(this)}}.bind(this),done:function(){return this.bind(n,function(){return this.createChild(t)}.bind(this)).as(u).done()}.bind(this)}},e.prototype.dispose=function(){var n,t=this.$Container_mappings;for(n in t)if(t.hasOwnProperty(n))try{t[n](a)}catch(e){throw e.message="Failed while disposing '"+n+"' due to:\n "+e.message,e}},e.prototype.$Container_done=function(){var n=this.$Container_pending,t=n[y]?n[y]:[],e=this.$Container_resolvers[n[h]](n[d],this.$Container_resolve.bind(this,t),this.$Container_release.bind(this,t));return this.$Container_mappings[n[l]]=e,this.$Container_pending=[],this},e.prototype.$Container_unbind=function(n){this.$Container_mappings[n](a),delete this.$Container_mappings[n]},e.prototype.$Container_release=function(n){return n.forEach(function(n){this.release(n)}.bind(this))},e.prototype.$Container_resolve=function(n){return n.map(function(n){return this.get(n)}.bind(this))},t.exports=e},{"./generateType":2,"./log":5,"./options":6,"./resolvers":7,"./signals":8}],2:[function(n,t){"use strict";t.exports=function(n){return n.reduce(function(n,t){return n|t},0)}},{}],3:[function(n,t){"use strict";function e(n){return new i(n)}var i=n("./Container");t.exports.create=e},{"./Container":1}],4:[function(n,t){"use strict";t.exports=function(n,t){var e,i;return e=function(){},e.prototype=n.prototype,i=new e,n.apply(i,t),i}},{}],5:[function(n,t){"use strict";function e(n){o++,r=n,i=Array(2*o).join(" ")}var i,r,o=0;e.done=function(){o--},t.exports=e},{}],6:[function(n,t){"use strict";t.exports=["VALUE","PROVIDER","CONSTRUCTOR","SINGLETON","FUNCTION","INSTANCE"].reduce(function(n,t,e){return n[t]=Math.pow(2,e),n},{})},{}],7:[function(n,t){"use strict";var e=n("./utils"),i=e.leftCurryTwice,r=e.identity,o=e.compose,s=e.partial,a=e.resolveDeps,u=e.invoke,p=e.instanciate,c=e.singletonify,f=n("./generateType"),l=n("./options"),d=l.VALUE,h=l.PROVIDER,y=l.CONSTRUCTOR,g=l.SINGLETON,C=l.FUNCTION,v=l.INSTANCE,$={};$[f([d])]=o(i,a)(r),$[f([C])]=o(i,a)(s),$[f([y])]=o(i,a)(p),$[f([y,g])]=c(p),$[f([h])]=o(i,a)(u),$[f([g,h])]=c(u),$[f([v])]=$[f([d])],t.exports=$},{"./generateType":2,"./options":6,"./utils":10}],8:[function(n,t){"use strict";t.exports={ACQUIRE:"ACQUIRE",RELEASE:"RELEASE",DISPOSE:"DISPOSE"}},{}],9:[function(n,t){"use strict";var e=n("./signals"),i=e.ACQUIRE,r=e.RELEASE,o=e.DISPOSE;t.exports=function(n){return function(t,e,s){function a(){"function"==typeof u.dispose&&u.dispose(),u=void 0,c=0}var u,p={},c=0;return p[i]=function(t){return"undefined"==typeof u&&(u=n.call(this,t,e())),c++,u},p[r]=function(){c--,0>=c&&(s(),a())},p[o]=function(){s(),a()},function(n){return p[n].call(this,t)}}}},{"./signals":8}],10:[function(n,t){"use strict";function e(n){return n}function i(n){return function(){var t=Array.prototype.slice.call(arguments,0);return function(){var e=Array.prototype.slice.call(arguments,0);return n.apply(null,t.concat(e))}}}function r(){var n=Array.prototype.slice.call(arguments,0);return function(){var t,e=Array.prototype.slice.call(arguments,0);for(t=n.length-1;t>=0;t--)e=[n[t].apply(this,e)];return e[0]}}function o(n){return function(t,e){return n.call(this,t,e())}}function s(n,t){return n.apply(null,t)}function a(n,t){return function(){var e=Array.prototype.slice.call(arguments,0);return n.apply(this,t.concat(e))}}var u=n("./instanciate"),p=n("./singletonify");t.exports.identity=e,t.exports.leftCurryTwice=i,t.exports.compose=r,t.exports.resolveDeps=o,t.exports.invoke=s,t.exports.partial=a,t.exports.instanciate=u,t.exports.singletonify=p},{"./instanciate":4,"./singletonify":9}]},{},[3])(3)}); | ||
!function(t){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var n;"undefined"!=typeof window?n=window:"undefined"!=typeof global?n=global:"undefined"!=typeof self&&(n=self),n.iniettore=t()}}(function(){return function t(n,e,r){function i(s,a){if(!e[s]){if(!n[s]){var u="function"==typeof require&&require;if(!a&&u)return u(s,!0);if(o)return o(s,!0);throw new Error("Cannot find module '"+s+"'")}var c=e[s]={exports:{}};n[s][0].call(c.exports,function(t){var e=n[s][1][t];return i(e?e:t)},c,c.exports,t,n,e,r)}return e[s].exports}for(var o="function"==typeof require&&require,s=0;s<r.length;s++)i(r[s]);return i}({1:[function(t,n){"use strict";function e(){}function r(t,n,r,i){var o;if("function"!=typeof t)throw new Error("Invalid container creation, missing contribution function");this.$Container0=r||{},this.$Container1=u,this.$Container2=n,this.$Container3={},this.$Container4=[],this.$Container5={},this.$Container6=i||e,o=this.$Container7=p(this.$Container8.bind(this)),o.map(f).to(this).as(c),o.flush(),t(o),o.flush()}var i=(t("./utils").generateMask,t("./signals")),o=i.ACQUIRE,s=i.RELEASE,a=i.DISPOSE,u=(t("./options").PROVIDER,t("./resolvers")),c=t("./options").VALUE,p=t("./createContext"),f="$container";r.prototype.get=function(t){return this.$Container2.log("resolving '"+t+"'",function(){if(this.$Container3[t])throw new Error("Circular dependency detected while resolving '"+t+"'");if(!(t in this.$Container0))throw new Error("'"+t+"' is not available. Has it ever been registered?.");this.$Container3[t]=!0;try{return this.$Container0[t](o)}catch(n){throw n.message="Failed while resolving '"+t+"' due to:\n "+n.message,n}finally{this.$Container3[t]=!1}}.bind(this))},r.prototype.using=function(t){return{get:function(n){var e,r=this.$Container7;for(e in t)r.map(e).to(t[e]).as(c);r.flush(),this.get(n);for(e in t)this.$Container9(e)}.bind(this)}},r.prototype.release=function(t){try{this.$Container0[t](s)}catch(n){throw n.message="Failed while releasing '"+t+"' due to:\n "+n.message,n}},r.prototype.createChild=function(t){var n=Object.keys(this.$Container5).length+1,e=new r(t,this.$Container2,Object.create(this.$Container0),this.$Containera.bind(this,n));return this.$Container5[n]=e,e},r.prototype.dispose=function(){this.$Containerb(),this.$Containerc(),this.$Container6(),this.$Container6=e},r.prototype.$Containerb=function(){var t,n=this.$Container5;for(t in n)n.hasOwnProperty(t)&&(n[t].dispose(),this.$Containera(t))},r.prototype.$Containera=function(t){delete this.$Container5[t]},r.prototype.$Containerc=function(){var t,n=this.$Container0;for(t in n)if(n.hasOwnProperty(t)){try{n[t](a)}catch(e){throw e.message="Failed while disposing '"+t+"' due to:\n "+e.message,e}delete n[t]}},r.prototype.$Container8=function(t,n,e,r){if(!(e in this.$Container1))throw new Error("Invalid flags combination. See documentation for valid flags combinations.");this.$Container0[t]=this.$Container1[e].call(null,n,this.$Containerd.bind(this,r),this.$Containere.bind(this,r))},r.prototype.$Container9=function(t){this.$Container0[t](a),delete this.$Container0[t]},r.prototype.$Containere=function(t){return t.forEach(function(t){this.release(t)}.bind(this))},r.prototype.$Containerd=function(t){return t.map(function(t){return this.get(t)}.bind(this))},n.exports=r},{"./createContext":3,"./options":6,"./resolvers":7,"./signals":8,"./utils":10}],2:[function(t,n){"use strict";function e(t){this.$Logger0=t,this.$Logger1=0,this.$Logger2={}}e.prototype.log=function(t,n){return n()},e.prototype.$Logger3=function(t){var n=this.$Logger5(),e=new Date;this.$Logger2[t]=e,console.log("["+e.toISOString()+"] "+n+"Starting "+t+"...")},e.prototype.$Logger4=function(t){var n=this.$Logger6(),e=new Date,r=e-this.$Logger2[t];console.log("["+e.toISOString()+"] "+n+"Finished "+t+" after "+r+" ms")},e.prototype.$Logger5=function(){return Array(++this.$Logger1).join(" ")},e.prototype.$Logger6=function(){return Array(this.$Logger1--).join(" ")},n.exports=e},{}],3:[function(t,n){"use strict";function e(t){return function(n){return n.createChild(t)}}function r(t,n){return function(e){return t(e).get(n)}}var i=t("./utils").generateMask,o=t("./options"),s=o.BLUEPRINT,a=o.PROVIDER,u=0,c=1,p=2,f=3;n.exports=function(t){var n=[],o={map:function(t){return o.flush(),n.push(t),{to:function(t){return n.push(t),{as:function(){var t=Array.prototype.slice.call(arguments,0);return 1===t.length&&t[0]===s?(t=[a],n[c]=e(n[c]),n.push(i(t)),n.push(["$container"]),{exports:function(t){return n[c]=r(n[c],t),{map:o.map}},map:o.map}):(n.push(i(t)),{map:o.map,injecting:function(){var t=Array.prototype.slice.call(arguments,0);return n.push(t),{map:o.map}}})}}}}},flush:function(){var e=n[f]||[];n.length>2&&(t(n[u],n[c],n[p],e),n=[])}};return o}},{"./options":6,"./utils":10}],4:[function(t,n){"use strict";function e(t,n){var e;return n=n||{},e=new i(n),new r(t,e)}var r=t("./Container"),i=t("./Logger");n.exports.create=e},{"./Container":1,"./Logger":2}],5:[function(t,n){"use strict";n.exports=function(t,n){var e,r;return e=function(){},e.prototype=t.prototype,r=new e,t.apply(r,n),r}},{}],6:[function(t,n){"use strict";n.exports=["VALUE","PROVIDER","CONSTRUCTOR","SINGLETON","FUNCTION","INSTANCE","PERSISTENT","TRANSIENT","BLUEPRINT"].reduce(function(t,n,e){return t[n]=Math.pow(2,e),t},{})},{}],7:[function(t,n){"use strict";var e=t("./utils"),r=e.leftCurryTwice,i=e.identity,o=e.compose,s=e.partial,a=e.resolveDeps,u=e.invoke,c=e.instanciate,p=e.singletonify,f=e.generateMask,l=t("./options"),h=l.VALUE,g=l.PROVIDER,y=l.CONSTRUCTOR,C=l.SINGLETON,d=l.FUNCTION,$=l.INSTANCE,v=l.TRANSIENT,E=l.PERSISTENT,m={};m[f([h])]=o(r,a)(i),m[f([d])]=o(r,a)(s),m[f([y])]=o(r,a)(c),m[f([v,y,C])]=p(c),m[f([E,y,C])]=p(c,!0),m[f([g])]=o(r,a)(u),m[f([v,C,g])]=p(u),m[f([E,C,g])]=p(u,!0),m[f([$])]=m[f([h])],n.exports=m},{"./options":6,"./utils":10}],8:[function(t,n){"use strict";n.exports={ACQUIRE:"ACQUIRE",RELEASE:"RELEASE",DISPOSE:"DISPOSE"}},{}],9:[function(t,n){"use strict";var e=t("./signals"),r=e.ACQUIRE,i=e.RELEASE,o=e.DISPOSE;n.exports=function(t,n){return n=n||!1,function(e,s,a){function u(){"function"==typeof c.dispose&&c.dispose(),c=void 0,f=0}var c,p={},f=0;return p[r]=function(n){return"undefined"==typeof c&&(c=t.call(this,n,s())),f++,c},p[i]=function(){f--,0!=f||n||(a(),u())},p[o]=function(){c&&(a(),u())},function(t){return p[t].call(this,e)}}}},{"./signals":8}],10:[function(t,n){"use strict";function e(t){return t}function r(t){return function(){var n=Array.prototype.slice.call(arguments,0);return function(){var e=Array.prototype.slice.call(arguments,0);return t.apply(null,n.concat(e))}}}function i(){var t=Array.prototype.slice.call(arguments,0);return function(){var n,e=Array.prototype.slice.call(arguments,0);for(n=t.length-1;n>=0;n--)e=[t[n].apply(this,e)];return e[0]}}function o(t){return function(n,e){return t.call(this,n,e())}}function s(t,n){return t.apply(null,n)}function a(t,n){return function(){var e=Array.prototype.slice.call(arguments,0);return t.apply(this,n.concat(e))}}function u(t){return t.reduce(function(t,n){return t|n},0)}var c=t("./instanciate"),p=t("./singletonify");n.exports.identity=e,n.exports.leftCurryTwice=r,n.exports.compose=i,n.exports.resolveDeps=o,n.exports.invoke=s,n.exports.partial=a,n.exports.generateMask=u,n.exports.instanciate=c,n.exports.singletonify=p},{"./instanciate":5,"./singletonify":9}]},{},[4])(4)}); |
'use strict' | ||
var generateType = require('./generateType'); | ||
var generateMask = require('./utils').generateMask; | ||
var mod$0 = require('./signals');var ACQUIRE = mod$0.ACQUIRE;var RELEASE = mod$0.RELEASE;var DISPOSE = mod$0.DISPOSE; | ||
@@ -8,83 +8,46 @@ var PROVIDER = require('./options').PROVIDER; | ||
var VALUE = require('./options').VALUE; | ||
var log = require('./log'); | ||
var createContext = require('./createContext'); | ||
var ALIAS_IDX = 0 | ||
var VALUE_IDX = 1 | ||
var TYPE_IDX = 2 | ||
var DEPS_IDX = 3 | ||
var CONTAINER_ALIAS = '$container' | ||
function noop() {} | ||
function Container(conf, mappings) { | ||
function Container(conf, logger, mappings, signalRelease) { | ||
var context | ||
if (typeof conf !== 'function') { | ||
throw new Error('Invalid container creation, missing contribution function') | ||
} | ||
this.$Container_mappings = mappings || {} | ||
this.$Container_resolvers = resolvers | ||
this.$Container_mappings = mappings || {} | ||
this.$Container_logger = logger | ||
this.$Container_resolving = {} | ||
this.$Container_pending = [] | ||
this.$Container_children = {} | ||
this.$Container_signalRelease = signalRelease || noop | ||
this.bind(CONTAINER_ALIAS, this).as(VALUE).done() | ||
if (typeof conf === 'function') { | ||
conf(this) | ||
if (this.$Container_pending.length) { | ||
this.$Container_done() | ||
} | ||
} | ||
context = this.$Container_context = createContext(this.$Container_contribute.bind(this)) | ||
context.map(CONTAINER_ALIAS).to(this).as(VALUE) | ||
context.flush() | ||
conf(context) | ||
context.flush() | ||
} | ||
Container.prototype.bind=function(alias, value) { | ||
this.$Container_pending = [alias, value] | ||
return { | ||
as: function() {var flags=Array.prototype.slice.call(arguments,0); | ||
this.$Container_pending[TYPE_IDX] = generateType(flags) | ||
return { | ||
bind: function() {var args=Array.prototype.slice.call(arguments,0); | ||
this.$Container_done() | ||
return this.bind.apply(this, args) | ||
}.bind(this), | ||
inject: function() {var deps=Array.prototype.slice.call(arguments,0); | ||
this.$Container_pending[DEPS_IDX] = deps | ||
return { | ||
bind: function() {var args=Array.prototype.slice.call(arguments,0); | ||
this.$Container_done() | ||
return this.bind.apply(this, args) | ||
}.bind(this), | ||
done: function() {return this.$Container_done();}.bind(this) | ||
} | ||
}.bind(this), | ||
done: function() {return this.$Container_done();}.bind(this) | ||
} | ||
}.bind(this) | ||
} | ||
}; | ||
Container.prototype.get=function(alias, transients) { | ||
return this.$Container_logger.log(("resolving '" + alias + "'"), function() { | ||
if (this.$Container_resolving[alias]) { throw new Error(("Circular dependency detected while resolving '" + alias + "'")) } | ||
if (!(alias in this.$Container_mappings)) { throw new Error(("'" + alias + "' is not available. Has it ever been registered?.")) } | ||
var value, error | ||
log(("Resolving " + alias)) | ||
if (this.$Container_resolving[alias]) { throw new Error(("Circular dependency detected while resolving '" + alias + "'")) } | ||
if (!(alias in this.$Container_mappings)) { throw new Error(("'" + alias + "' is not available. Has it ever been registered?.")) } | ||
this.$Container_resolving[alias] = true | ||
try { | ||
value = this.$Container_mappings[alias](ACQUIRE) | ||
} catch(err) { | ||
err.message = ("Failed while resolving '" + alias + "' due to:\n\t" + err.message) | ||
throw err | ||
} | ||
this.$Container_resolving[alias] = false | ||
log.done() | ||
return value | ||
this.$Container_resolving[alias] = true | ||
try { | ||
return this.$Container_mappings[alias](ACQUIRE) | ||
} catch(err) { | ||
err.message = ("Failed while resolving '" + alias + "' due to:\n\t" + err.message) | ||
throw err | ||
} finally { | ||
this.$Container_resolving[alias] = false | ||
} | ||
}.bind(this)) | ||
}; | ||
@@ -95,7 +58,9 @@ | ||
get: function(alias) { | ||
var context = this.$Container_context | ||
var dep | ||
for (dep in transientsDeps) { | ||
this.bind(dep, transientsDeps[dep]).as(VALUE).done() | ||
context.map(dep).to(transientsDeps[dep]).as(VALUE) | ||
} | ||
context.flush() | ||
this.get(alias) | ||
@@ -113,3 +78,3 @@ for (dep in transientsDeps) { | ||
} catch(err) { | ||
err.message = ("Failed while disposing '" + alias + "' due to:\n\t" + err.message) | ||
err.message = ("Failed while releasing '" + alias + "' due to:\n\t" + err.message) | ||
throw err | ||
@@ -120,18 +85,34 @@ } | ||
Container.prototype.createChild=function(conf) { | ||
return new Container(conf, Object.create(this.$Container_mappings)) | ||
var id = Object.keys(this.$Container_children).length + 1 | ||
var child = new Container(conf, this.$Container_logger, Object.create(this.$Container_mappings), this.$Container_releaseChild.bind(this, id)) | ||
this.$Container_children[id] = child | ||
return child | ||
}; | ||
Container.prototype.createBlueprint=function(alias, blueprint) { | ||
return { | ||
exports: function(mapping) { | ||
return { | ||
done: function() {return this.bind(alias, function() {return this.createChild(blueprint).get(mapping);}.bind(this)).as(PROVIDER).done();}.bind(this) | ||
} | ||
}.bind(this), | ||
done: function() {return this.bind(alias, function() {return this.createChild(blueprint);}.bind(this)).as(PROVIDER).done();}.bind(this) | ||
Container.prototype.dispose=function() { | ||
this.$Container_disposeChildren() | ||
this.$Container_disposeInstances() | ||
this.$Container_signalRelease() | ||
this.$Container_signalRelease = noop | ||
}; | ||
Container.prototype.$Container_disposeChildren=function() { | ||
var children = this.$Container_children | ||
var id | ||
for (id in children) { | ||
/* istanbul ignore else */ | ||
if (children.hasOwnProperty(id)) { | ||
children[id].dispose() | ||
this.$Container_releaseChild(id) | ||
} | ||
} | ||
}; | ||
Container.prototype.dispose=function() { | ||
Container.prototype.$Container_releaseChild=function(id) { | ||
delete this.$Container_children[id] | ||
}; | ||
Container.prototype.$Container_disposeInstances=function() { | ||
var mappings = this.$Container_mappings | ||
@@ -141,2 +122,3 @@ var alias | ||
for (alias in mappings) { | ||
/* istanbul ignore else */ | ||
if (mappings.hasOwnProperty(alias)) { | ||
@@ -149,2 +131,3 @@ try { | ||
} | ||
delete mappings[alias] | ||
} | ||
@@ -154,12 +137,8 @@ } | ||
Container.prototype.$Container_done=function() { | ||
Container.prototype.$Container_contribute=function(alias, value, type, deps) { | ||
if ( !(type in this.$Container_resolvers) ) { | ||
throw new Error('Invalid flags combination. See documentation for valid flags combinations.') | ||
} | ||
this.$Container_mappings[alias] = this.$Container_resolvers[type].call(null, value, this.$Container_resolve.bind(this, deps), this.$Container_release.bind(this, deps)) | ||
var pending = this.$Container_pending | ||
var deps = pending[DEPS_IDX] ? pending[DEPS_IDX] : [] | ||
var resolver = this.$Container_resolvers[ pending[TYPE_IDX] ](pending[VALUE_IDX], this.$Container_resolve.bind(this, deps), this.$Container_release.bind(this, deps)) | ||
this.$Container_mappings[pending[ALIAS_IDX]] = resolver | ||
this.$Container_pending = [] | ||
return this | ||
}; | ||
@@ -166,0 +145,0 @@ |
'use strict' | ||
var Container = require('./Container'); | ||
var Logger = require('./Logger'); | ||
function create(conf) { | ||
return new Container(conf) | ||
function create(conf, options) { | ||
var logger | ||
options = options || {} | ||
logger = new Logger(options) | ||
return new Container(conf, logger) | ||
} module.exports.create = create; |
@@ -9,3 +9,6 @@ 'use strict' | ||
'FUNCTION', | ||
'INSTANCE' | ||
'INSTANCE', | ||
'PERSISTENT', | ||
'TRANSIENT', | ||
'BLUEPRINT' | ||
].reduce(function (options, flag, idx) { | ||
@@ -12,0 +15,0 @@ options[flag] = Math.pow(2, idx) |
@@ -15,4 +15,5 @@ 'use strict' | ||
var generateMask = mod$2.generateMask; | ||
var generateType = require('./generateType'); | ||
var mod$3 = require('./options'); | ||
@@ -25,2 +26,4 @@ var VALUE = mod$3.VALUE; | ||
var INSTANCE = mod$3.INSTANCE; | ||
var TRANSIENT = mod$3.TRANSIENT; | ||
var PERSISTENT = mod$3.PERSISTENT; | ||
@@ -30,12 +33,14 @@ | ||
resolvers[ generateType([VALUE]) ] = compose(leftCurryTwice, resolveDeps)(identity) | ||
resolvers[ generateType([FUNCTION]) ] = compose(leftCurryTwice, resolveDeps)(partial) | ||
resolvers[ generateType([CONSTRUCTOR]) ] = compose(leftCurryTwice, resolveDeps)(instanciate) | ||
resolvers[ generateType([CONSTRUCTOR, SINGLETON]) ] = singletonify(instanciate) | ||
resolvers[ generateType([PROVIDER]) ] = compose(leftCurryTwice, resolveDeps)(invoke) | ||
resolvers[ generateType([SINGLETON, PROVIDER]) ] = singletonify(invoke) | ||
resolvers[ generateMask([VALUE]) ] = compose(leftCurryTwice, resolveDeps)(identity) | ||
resolvers[ generateMask([FUNCTION]) ] = compose(leftCurryTwice, resolveDeps)(partial) | ||
resolvers[ generateMask([CONSTRUCTOR]) ] = compose(leftCurryTwice, resolveDeps)(instanciate) | ||
resolvers[ generateMask([TRANSIENT, CONSTRUCTOR, SINGLETON]) ] = singletonify(instanciate) | ||
resolvers[ generateMask([PERSISTENT, CONSTRUCTOR, SINGLETON]) ] = singletonify(instanciate, true) | ||
resolvers[ generateMask([PROVIDER]) ] = compose(leftCurryTwice, resolveDeps)(invoke) | ||
resolvers[ generateMask([TRANSIENT, SINGLETON, PROVIDER]) ] = singletonify(invoke) | ||
resolvers[ generateMask([PERSISTENT, SINGLETON, PROVIDER]) ] = singletonify(invoke, true) | ||
// aliases | ||
resolvers[ generateType([INSTANCE]) ] = resolvers[ generateType([VALUE]) ] | ||
resolvers[ generateMask([INSTANCE]) ] = resolvers[ generateMask([VALUE]) ] | ||
module.exports = resolvers |
'use strict' | ||
var mod$6 = require('./signals');var ACQUIRE = mod$6.ACQUIRE;var RELEASE = mod$6.RELEASE;var DISPOSE = mod$6.DISPOSE; | ||
var mod$5 = require('./signals');var ACQUIRE = mod$5.ACQUIRE;var RELEASE = mod$5.RELEASE;var DISPOSE = mod$5.DISPOSE; | ||
module.exports = function singletonify(create) { | ||
module.exports = function singletonify(create, persistent) { | ||
persistent = persistent || false | ||
return function (value, resolveDeps, releaseDeps) { | ||
@@ -31,3 +33,3 @@ | ||
count-- | ||
if (count <= 0) { | ||
if (count == 0 && !persistent) { | ||
releaseDeps() | ||
@@ -39,4 +41,6 @@ dispose() | ||
handlers[DISPOSE] = function (value) { | ||
releaseDeps() | ||
dispose() | ||
if (instance) { | ||
releaseDeps() | ||
dispose() | ||
} | ||
} | ||
@@ -43,0 +47,0 @@ |
@@ -44,2 +44,6 @@ 'use strict' | ||
function generateMask(flags) { | ||
return flags.reduce(function(prev, curr) {return prev | curr;}, 0) | ||
} module.exports.generateMask = generateMask; | ||
module.exports.instanciate = instanciate;module.exports.singletonify = singletonify; |
{ | ||
"name": "iniettore", | ||
"version": "0.0.5", | ||
"version": "1.0.0", | ||
"description": "A light and simple IoC container", | ||
@@ -31,46 +31,46 @@ "main": "lib/iniettore.js", | ||
"devDependencies": { | ||
"browserify": "~4.1.11", | ||
"browserify-istanbul": "~0.1.0", | ||
"chai": "~1.9.1", | ||
"envify": "~2.0.0", | ||
"es6-module-jstransform": "~0.1.4", | ||
"glob": "~4.0.2", | ||
"gulp": "~3.8.0", | ||
"gulp-browserify": "~0.5.0", | ||
"gulp-cached": "0.0.3", | ||
"gulp-coveralls": "~0.1.2", | ||
"gulp-debug": "~0.3.0", | ||
"gulp-filter": "~0.4.1", | ||
"gulp-foreach": "0.0.1", | ||
"gulp-jstransform": "~0.1.0", | ||
"gulp-karma": "0.0.4", | ||
"gulp-mocha": "~0.4.1", | ||
"tmp": "0.0.23", | ||
"es6-module-jstransform": "~0.1.4", | ||
"gulp-jstransform": "~0.1.0", | ||
"gulp-rename": "~1.2.0", | ||
"gulp-filter": "~0.4.1", | ||
"sinon": "~1.10.2", | ||
"chai": "~1.9.1", | ||
"sinon-chai": "~2.5.0", | ||
"gulp-cached": "0.0.3", | ||
"gulp-debug": "~0.3.0", | ||
"gulp-browserify": "~0.5.0", | ||
"gulp-size": "~0.4.0", | ||
"gulp-streamify": "0.0.5", | ||
"gulp-uglify": "~0.3.1", | ||
"gulp-util": "~2.2.19", | ||
"gulp-watch": "~0.6.8", | ||
"jstransform": "~5.0.0", | ||
"jstransformify": "~0.2.2", | ||
"karma": "~0.12.16", | ||
"gulp-karma": "0.0.4", | ||
"mocha": "~1.20.1", | ||
"karma-mocha": "~0.1.4", | ||
"karma-chai": "~0.1.0", | ||
"karma-chrome-launcher": "~0.1.4", | ||
"karma-spec-reporter": "0.0.13", | ||
"karma-coverage": "~0.2.4", | ||
"karma-es5-shim": "https://github.com/pokehanai/karma-es5-shim/archive/v2.1.0.tar.gz", | ||
"karma-mocha": "~0.1.4", | ||
"karma-phantomjs-launcher": "~0.1.4", | ||
"karma-sinon": "~1.0.3", | ||
"karma-chai": "~0.1.0", | ||
"karma-sinon-chai": "~0.2.0", | ||
"jstransformify": "~0.2.2", | ||
"browserify": "~4.1.11", | ||
"karma-spec-reporter": "0.0.13", | ||
"mocha": "~1.20.1", | ||
"require-dir": "~0.1.0", | ||
"run-sequence": "~0.3.6", | ||
"sinon": "~1.10.2", | ||
"sinon-chai": "~2.5.0", | ||
"through2": "~0.5.1", | ||
"tmp": "0.0.23", | ||
"vinyl-source-stream": "~0.1.1", | ||
"glob": "~4.0.2", | ||
"gulp-streamify": "0.0.5", | ||
"gulp-foreach": "0.0.1", | ||
"watchify": "~0.10.2", | ||
"gulp-watch": "~0.6.8", | ||
"gulp-util": "~2.2.19", | ||
"gulp-uglify": "~0.3.1", | ||
"envify": "~2.0.0", | ||
"karma-phantomjs-launcher": "~0.1.4", | ||
"karma-es5-shim": "https://github.com/pokehanai/karma-es5-shim/archive/v2.1.0.tar.gz", | ||
"require-dir": "~0.1.0", | ||
"run-sequence": "~0.3.6", | ||
"jstransform": "~5.0.0", | ||
"gulp-size": "~0.4.0", | ||
"browserify-istanbul": "~0.1.0", | ||
"gulp-coveralls": "~0.1.2" | ||
"watchify": "~0.10.2" | ||
} | ||
} |
#  Iniettore | ||
#  Iniettore | ||
[](https://travis-ci.org/cesarenaldi/iniettore) | ||
[](https://travis-ci.org/cesarenaldi/iniettore) | ||
[](http://badge.fury.io/gh/cesarenaldi%2Finiettore) | ||
[](https://david-dm.org/cesarenaldi/iniettore) | ||
[](https://coveralls.io/r/cesarenaldi/iniettore?branch=master) | ||
[](https://coveralls.io/r/cesarenaldi/iniettore?branch=api-refactoring) | ||
@@ -16,9 +16,10 @@ ## TODO | ||
- [ ] ~~Make possible to pass extra params~~ | ||
- [ ] Complete debug logs | ||
- [ ] Handle errors in case of wrong api calls | ||
- [x] Complete debug logs | ||
- [ ] ~~Handle errors in case of wrong api calls~~ Impossible to call methods in wrong order with new APIs. | ||
- [ ] Make dispose method name configurable | ||
- [ ] Improve fluid API | ||
- [ ] remove done call | ||
- [x] Improve fluid API | ||
- [x] remove done call | ||
- [x] add experimental contribution phase into a revealing construction pattern | ||
- [ ] test case when singletons do NOT implement a dispose method | ||
- [x] refactor APIs to be more explicit | ||
- [x] test case when singletons do NOT implement a dispose method (see test coverage) | ||
- [x] cleanup | ||
@@ -28,11 +29,67 @@ - [x] remove memoize if not used | ||
- [ ] DOCS | ||
- [ ] Features | ||
- [ ] Specify ECMA Script 5 required features or polyfills | ||
- [ ] Quick usage | ||
- [ ] Detailed examples | ||
DEFER | ||
### DEFER | ||
- [ ] ~~Detect invalid singleton destroy calls.~~ Too complex for a minor benefit. | ||
WONT DO IT | ||
### WONT DO IT | ||
- [ ] ~~Consider derring the release in case the object will be required in the short term~~ - complicated and no real benefits - the container consumer should do that before calling release | ||
## Features | ||
## Specify ECMA Script 5 required features or polyfills | ||
- `Object.create` | ||
- `Function.prototype.bind` | ||
## Quick start | ||
### Installation | ||
node.js: | ||
```bash | ||
npm install iniettore --save | ||
``` | ||
### Usage | ||
```javascript | ||
import iniettore from 'iniettore' | ||
import { VALUE, SINGLETON, CONSTRUCTOR } from 'iniettore/lib/options' | ||
class UltimateQuestion { | ||
constructor(answer) { | ||
console.log(answer) | ||
} | ||
} | ||
var container = iniettore.create(function (context) { | ||
context | ||
.map('answer') | ||
.to(42) | ||
.as(VALUE) | ||
.map('question') | ||
.to(UltimateQuestion) | ||
.as(SINGLETON, CONSTRUCTOR) | ||
.injecting('answer') | ||
}) | ||
var question = container.get('question') // 42 | ||
console.log(question instanceof UltimateQuestion) // true | ||
``` | ||
## Advanced usage | ||
### Value objects and instances | ||
### Functions | ||
### Providers | ||
### Constructor | ||
### Child context | ||
### Blueprint | ||
### Transient dependencies | ||
## Notes on singletons | ||
## Notes on lifecycle | ||
### Instances `dispose` | ||
### Context `dispose` | ||
## Throubleshooting |
@@ -43,2 +43,3 @@ var gulp = require('gulp') | ||
.pipe(streamify(size())) | ||
.pipe(streamify(size({ gzip: true }))) | ||
.pipe(gulp.dest('./dist')) | ||
@@ -45,0 +46,0 @@ }) |
var gulp = require('gulp') | ||
var mocha = require('gulp-mocha') | ||
var gutil = require('gulp-util') | ||
@@ -16,2 +17,6 @@ gulp.task('test-node', function () { | ||
})) | ||
.on('error', function (err) { | ||
gutil.log(gutil.colors.red(err.message)) | ||
this.emit('end') | ||
}) | ||
}) |
'use strict' | ||
import iniettore from '../../src/iniettore' | ||
import { VALUE, CONSTRUCTOR, PROVIDER, SINGLETON } from '../../src/options' | ||
import { VALUE, CONSTRUCTOR, PROVIDER, SINGLETON, TRANSIENT, BLUEPRINT } from '../../src/options' | ||
@@ -12,9 +12,2 @@ describe('Given a container', function () { | ||
before(function () { | ||
container = iniettore.create() | ||
.bind('bar', VALUE_A).as(VALUE) | ||
.bind('pluto', parentProvideStub).as(SINGLETON, PROVIDER) | ||
.done() | ||
}) | ||
describe('and a registered blueprint with some registered bindings', function () { | ||
@@ -27,10 +20,15 @@ | ||
before(function () { | ||
container.createBlueprint('foo', function (container) { | ||
function configureChildContext(context) { | ||
context | ||
.map('baz').to(blueprintProviderStub) | ||
.as(TRANSIENT, SINGLETON, PROVIDER) | ||
.injecting('bar') | ||
} | ||
container | ||
.bind('baz', blueprintProviderStub) | ||
.as(SINGLETON, PROVIDER) | ||
.inject('bar') | ||
.done() | ||
}).done() | ||
container = iniettore.create(function (context) { | ||
context | ||
.map('bar').to(VALUE_A).as(VALUE) | ||
.map('foo').to(configureChildContext).as(BLUEPRINT) | ||
.map('pluto').to(parentProvideStub).as(TRANSIENT, SINGLETON, PROVIDER) | ||
}) | ||
}) | ||
@@ -63,10 +61,15 @@ | ||
before(function () { | ||
container.createBlueprint('foo', function (container) { | ||
function configureChildContext(context) { | ||
context | ||
.map('baz').to(blueprintProviderStub) | ||
.as(TRANSIENT, SINGLETON, PROVIDER) | ||
.injecting('bar') | ||
} | ||
container | ||
.bind('baz', blueprintProviderStub) | ||
.as(SINGLETON, PROVIDER) | ||
.inject('bar') | ||
}).exports('baz').done() | ||
container = iniettore.create(function (context) { | ||
context | ||
.map('bar').to(VALUE_A).as(VALUE) | ||
.map('foo').to(configureChildContext).as(BLUEPRINT).exports('baz') | ||
.map('pluto').to(parentProvideStub).as(TRANSIENT, SINGLETON, PROVIDER) | ||
}) | ||
}) | ||
@@ -73,0 +76,0 @@ |
'use strict' | ||
import iniettore from '../../src/iniettore' | ||
import { VALUE, CONSTRUCTOR, PROVIDER, SINGLETON } from '../../src/options' | ||
import { VALUE, CONSTRUCTOR, PROVIDER, SINGLETON, TRANSIENT } from '../../src/options' | ||
function noop() {} | ||
describe('Given a child container', function () { | ||
var parent | ||
var child | ||
var OBJECT = {} | ||
beforeEach(function () { | ||
parent = iniettore.create() | ||
child = parent.createChild() | ||
}) | ||
describe('when requesting an alias registered in the parent container', function () { | ||
it('should return the same binding as requested to the parent container', function () { | ||
var parent = iniettore.create(function (context) { | ||
context | ||
.map('foo').to(OBJECT) | ||
.as(VALUE) | ||
}) | ||
var child = parent.createChild(noop) | ||
it('should be possible to request registered alias of the parent container', function () { | ||
parent | ||
.bind('foo', OBJECT) | ||
.as(VALUE) | ||
.done() | ||
expect(child.get('foo')) | ||
.to.equal(parent.get('foo')) | ||
expect(child.get('foo')) | ||
.to.equal(parent.get('foo')) | ||
}) | ||
}) | ||
it('should be possible to get the respective containers using the reserved alias `$container`', function () { | ||
expect(parent.get('$container')).to.equal(parent) | ||
expect(child.get('$container')).to.equal(child) | ||
expect(parent.get('$container')).to.not.equal(child.get('$container')) | ||
describe('when requesting a reference to the containers themself', function () { | ||
it('should return the respective containers', function () { | ||
var parent = iniettore.create(noop) | ||
var child = parent.createChild(noop) | ||
expect(parent.get('$container')).to.equal(parent) | ||
expect(child.get('$container')).to.equal(child) | ||
expect(parent.get('$container')).to.not.equal(child.get('$container')) | ||
}) | ||
}) | ||
describe('with a registered alias that shadows the parent one', function () { | ||
describe('with a registered alias that shadows the one registered in the parent container', function () { | ||
describe('when requesting its alias from the child container', function () { | ||
var parent | ||
beforeEach(function () { | ||
parent | ||
.bind('foo', OBJECT) | ||
.as(VALUE) | ||
.done() | ||
parent = iniettore.create(function (context) { | ||
context | ||
.map('foo').to(OBJECT) | ||
.as(VALUE) | ||
}) | ||
}) | ||
it('should retrieve the child container version', function () { | ||
child | ||
.bind('foo', 42) | ||
.as(VALUE) | ||
.done() | ||
var child = parent.createChild(function (context) { | ||
context | ||
.map('foo').to(42) | ||
.as(VALUE) | ||
}) | ||
expect(child.get('foo')).to.equal(42) | ||
@@ -57,2 +65,3 @@ expect(parent.get('foo')).to.equal(OBJECT) | ||
describe('when requesting an instance from a provider registered in the child container', function () { | ||
describe('with a dependency from a provider registered in the parent container', function () { | ||
@@ -64,13 +73,16 @@ | ||
var chilProviderStub = sinon.stub().returns(CHILD_INSTANCE) | ||
var parent, child | ||
beforeEach(function () { | ||
parent | ||
.bind('bar', parentProviderStub) | ||
.as(SINGLETON, PROVIDER) | ||
.done() | ||
child | ||
.bind('baz', chilProviderStub) | ||
.as(SINGLETON, PROVIDER) | ||
.inject('bar') | ||
.done() | ||
parent = iniettore.create(function (context) { | ||
context | ||
.map('bar').to(parentProviderStub) | ||
.as(TRANSIENT, SINGLETON, PROVIDER) | ||
}) | ||
child = parent.createChild(function (context) { | ||
context | ||
.map('baz').to(chilProviderStub) | ||
.as(TRANSIENT, SINGLETON, PROVIDER) | ||
.injecting('bar') | ||
}) | ||
}) | ||
@@ -77,0 +89,0 @@ |
'use strict' | ||
import iniettore from '../../src/iniettore' | ||
import { VALUE, CONSTRUCTOR, SINGLETON } from '../../src/options' | ||
import { VALUE, CONSTRUCTOR, SINGLETON, TRANSIENT } from '../../src/options' | ||
@@ -9,8 +9,2 @@ | ||
var container | ||
beforeEach(function () { | ||
container = iniettore.create() | ||
}) | ||
describe('when requesting the corresponding alias', function () { | ||
@@ -26,6 +20,7 @@ | ||
container | ||
.bind('foo', Foo) | ||
.as(CONSTRUCTOR) | ||
.done() | ||
var container = iniettore.create(function (context) { | ||
context | ||
.map('foo').to(Foo) | ||
.as(CONSTRUCTOR) | ||
}) | ||
@@ -53,6 +48,7 @@ container.get('foo') | ||
container | ||
.bind('foo', Foo) | ||
.as(SINGLETON, CONSTRUCTOR) | ||
.done() | ||
var container = iniettore.create(function (context) { | ||
context | ||
.map('foo').to(Foo) | ||
.as(TRANSIENT, SINGLETON, CONSTRUCTOR) | ||
}) | ||
@@ -63,3 +59,3 @@ expect(constructorSpy).to.not.be.called | ||
expect(constructorSpy).to.not.be.calleOnce | ||
expect(constructorSpy).to.not.be.calle | ||
@@ -101,12 +97,13 @@ secondGet = container.get('foo') | ||
container | ||
.bind('common', Common) | ||
.as(SINGLETON, CONSTRUCTOR) | ||
.bind('bar', Bar) | ||
.as(SINGLETON, CONSTRUCTOR) | ||
.inject('common') | ||
.bind('foo', Foo) | ||
.as(SINGLETON, CONSTRUCTOR) | ||
.inject('common', 'bar') | ||
.done() | ||
var container = iniettore.create(function (context) { | ||
context | ||
.map('common').to(Common) | ||
.as(TRANSIENT, SINGLETON, CONSTRUCTOR) | ||
.map('bar').to(Bar) | ||
.as(TRANSIENT, SINGLETON, CONSTRUCTOR) | ||
.injecting('common') | ||
.map('foo').to(Foo) | ||
.as(TRANSIENT, SINGLETON, CONSTRUCTOR) | ||
.injecting('common', 'bar') | ||
}) | ||
@@ -113,0 +110,0 @@ expect(container.get('foo')).to.be.instanceof(Foo) |
@@ -8,7 +8,9 @@ 'use strict' | ||
CONSTRUCTOR, | ||
SINGLETON | ||
SINGLETON, | ||
TRANSIENT, | ||
PERSISTENT | ||
} from '../../src/options' | ||
describe('Given a container with registered providers and constructor', function () { | ||
describe('Given a container', function () { | ||
@@ -30,35 +32,31 @@ var container, providerStub | ||
beforeEach(function () { | ||
describe('when releasing a transient singleton', function () { | ||
container = iniettore.create() | ||
beforeEach(function () { | ||
providerStub = sinon.stub() | ||
BazDisposeSpy.reset() | ||
providerStub = sinon.stub() | ||
BazDisposeSpy.reset() | ||
providerStub | ||
.onFirstCall().returns(BAR_1) | ||
.onSecondCall().returns(BAR_2) | ||
providerStub | ||
.onFirstCall().returns(BAR_1) | ||
.onSecondCall().returns(BAR_2) | ||
container = iniettore.create(function (context) { | ||
context | ||
.map('foo').to(OBJECT_DEP) | ||
.as(VALUE) | ||
container | ||
.bind('foo', OBJECT_DEP) | ||
.as(VALUE) | ||
.done() | ||
container | ||
.bind('baz', Baz) | ||
.as(SINGLETON, CONSTRUCTOR) | ||
.inject('foo') | ||
.done() | ||
container | ||
.bind('bar', providerStub) | ||
.as(SINGLETON, PROVIDER) | ||
.inject('foo', 'baz') | ||
.done() | ||
.map('baz').to(Baz) | ||
.as(TRANSIENT, SINGLETON, CONSTRUCTOR) | ||
.injecting('foo') | ||
BAR_1.dispose.reset() | ||
BAR_2.dispose.reset() | ||
}) | ||
.map('bar').to(providerStub) | ||
.as(TRANSIENT, SINGLETON, PROVIDER) | ||
.injecting('foo', 'baz') | ||
}) | ||
describe('when releasing a singleton', function () { | ||
BAR_1.dispose.reset() | ||
BAR_2.dispose.reset() | ||
}) | ||
it('should not release the instance if still in use', function () { | ||
it('should not dispose the instance if still in use', function () { | ||
var firstRequest = container.get('bar') | ||
@@ -76,2 +74,23 @@ var secondRequest = container.get('bar') | ||
describe('and the instance does not have a dispose method', function () { | ||
class Foo {} | ||
beforeEach(function () { | ||
container = iniettore.create(function (context) { | ||
context | ||
.map('foo').to(Foo) | ||
.as(TRANSIENT, SINGLETON, CONSTRUCTOR) | ||
}) | ||
container.get('foo') | ||
}) | ||
it('should not throw an Error', function () { | ||
function testCase() { | ||
container.release('foo') | ||
} | ||
expect(testCase).to.not.throw(TypeError) | ||
}) | ||
}) | ||
describe('the same amount of times it has been acquired', function () { | ||
@@ -125,14 +144,69 @@ | ||
describe('and child container', function () { | ||
describe('when releasing a persistent singleton adn there is no more reference to the singleton', function () { | ||
var PARENT_INSTANCE = { dispose: sinon.spy() } | ||
var CHILD_INSTANCE = { dispose: sinon.spy() } | ||
var parentProviderStub = sinon.stub().returns(PARENT_INSTANCE) | ||
var chilProviderStub = sinon.stub().returns(CHILD_INSTANCE) | ||
before(function () { | ||
container = iniettore.create(function (context) { | ||
context | ||
.map('baz').to(Baz) | ||
.as(PERSISTENT, SINGLETON, CONSTRUCTOR) | ||
}) | ||
}) | ||
it('should not dispose the instance', function () { | ||
container.get('baz') | ||
container.release('baz') | ||
expect(BazDisposeSpy).to.not.be.called | ||
}) | ||
}) | ||
describe('with a registered singleton constructor and singleton provider', function () { | ||
class Bar {} | ||
function dummyProvider() { return {} } | ||
before(function () { | ||
container = iniettore.create(function (context) { | ||
context | ||
.map('bar').to(Bar) | ||
.as(TRANSIENT, SINGLETON, CONSTRUCTOR) | ||
context | ||
.map('foo').to(dummyProvider) | ||
.as(TRANSIENT, SINGLETON, PROVIDER) | ||
}) | ||
}) | ||
describe('and none of the two has been requested before', function () { | ||
describe('when disposing the container', function () { | ||
it('should not throw an Error related to dispoding instances that don\'t exists', function () { | ||
function testCase() { | ||
container.dispose() | ||
} | ||
expect(testCase).to.not.throw(TypeError, /Cannot read property 'dispose' of undefined/i) | ||
}) | ||
}) | ||
}) | ||
}) | ||
describe('and a child container', function () { | ||
var INSTANCE_IN_PARENT = { dispose: sinon.spy() } | ||
var INSTANCE_IN_CHILD = { dispose: sinon.spy() } | ||
var parentProviderStub = sinon.stub().returns(INSTANCE_IN_PARENT) | ||
var chilProviderStub = sinon.stub().returns(INSTANCE_IN_CHILD) | ||
var child | ||
beforeEach(function () { | ||
child = container.createChild() | ||
PARENT_INSTANCE.dispose.reset() | ||
CHILD_INSTANCE.dispose.reset() | ||
container = iniettore.create(function (context) { | ||
context | ||
.map('bar').to(parentProviderStub) | ||
.as(TRANSIENT, SINGLETON, PROVIDER) | ||
}) | ||
child = container.createChild(function (context) { | ||
context | ||
.map('baz').to(chilProviderStub) | ||
.as(TRANSIENT, SINGLETON, PROVIDER) | ||
.injecting('bar') | ||
}) | ||
INSTANCE_IN_PARENT.dispose.reset() | ||
INSTANCE_IN_CHILD.dispose.reset() | ||
}) | ||
@@ -145,11 +219,2 @@ | ||
beforeEach(function () { | ||
container | ||
.bind('bar', parentProviderStub) | ||
.as(SINGLETON, PROVIDER) | ||
.done() | ||
child | ||
.bind('baz', chilProviderStub) | ||
.as(SINGLETON, PROVIDER) | ||
.inject('bar') | ||
.done() | ||
child.get('baz') | ||
@@ -160,4 +225,4 @@ }) | ||
child.release('baz') | ||
expect(CHILD_INSTANCE.dispose).to.be.calledOnce | ||
expect(PARENT_INSTANCE.dispose).to.be.calledOnce | ||
expect(INSTANCE_IN_CHILD.dispose).to.be.calledOnce | ||
expect(INSTANCE_IN_PARENT.dispose).to.be.calledOnce | ||
done() | ||
@@ -169,4 +234,4 @@ }) | ||
child.release('baz') | ||
expect(CHILD_INSTANCE.dispose).to.be.calledOnce | ||
expect(PARENT_INSTANCE.dispose).to.not.be.called | ||
expect(INSTANCE_IN_CHILD.dispose).to.be.calledOnce | ||
expect(INSTANCE_IN_PARENT.dispose).to.not.be.called | ||
}) | ||
@@ -176,43 +241,49 @@ }) | ||
describe('when disposing the child container itself', function () { | ||
describe('when disposing the child container', function () { | ||
beforeEach(function () { | ||
container | ||
.bind('bar', parentProviderStub) | ||
.as(SINGLETON, PROVIDER) | ||
.done() | ||
child | ||
.bind('baz', chilProviderStub) | ||
.as(SINGLETON, PROVIDER) | ||
.inject('bar') | ||
.done() | ||
}) | ||
it('should dispose the registered singleton instances', function () { | ||
it('should dispose the registered singleton instances in the respective containers', function () { | ||
child.get('baz') | ||
child.dispose() | ||
expect(CHILD_INSTANCE.dispose).to.be.calledOnce | ||
expect(PARENT_INSTANCE.dispose).to.be.calledOnce | ||
expect(INSTANCE_IN_CHILD.dispose).to.be.calledOnce | ||
expect(INSTANCE_IN_PARENT.dispose).to.be.calledOnce | ||
}) | ||
}) | ||
describe.skip('when disposing the parent container', function () { | ||
describe('when disposing the parent container', function () { | ||
beforeEach(function () { | ||
sinon.spy(child, 'dispose') | ||
child.dispose.reset() | ||
}) | ||
after(function () { | ||
child.dispose.restore() | ||
}) | ||
it('should dispose the child container as well', function () { | ||
expect(false).to.be.true | ||
container.dispose() | ||
expect(child.dispose).to.be.calledOnce | ||
}) | ||
}) | ||
}) | ||
describe.skip('when disposing the instance outside of the container lifecycle management', function () { | ||
it('should throw an Error', function () { | ||
var instance = container.get('baz') | ||
it('should dispose instances in the child container before moving to dispose the ones in the parent container', function () { | ||
child.get('bar') // request bar to avoid auto-dispose when disposing child container and its instances | ||
child.get('baz') | ||
container.dispose() | ||
expect(INSTANCE_IN_PARENT.dispose) | ||
.to.be.calledOnce | ||
expect(INSTANCE_IN_CHILD.dispose) | ||
.to.be.calledOnce | ||
.and.to.be.calledBefore(INSTANCE_IN_PARENT.dispose) | ||
}) | ||
function testCase() { | ||
instance.dispose() | ||
} | ||
expect(testCase).to.throw(Error) | ||
describe('after having disposed a child container', function () { | ||
it('should not call the child dispose method', function () { | ||
child.dispose() | ||
child.dispose.reset() | ||
container.dispose() | ||
expect(child.dispose).to.not.be.called | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
'use strict' | ||
import iniettore from '../../src/iniettore' | ||
import { VALUE, CONSTRUCTOR, PROVIDER, SINGLETON } from '../../src/options' | ||
import { VALUE, CONSTRUCTOR, PROVIDER, SINGLETON, TRANSIENT } from '../../src/options' | ||
describe('Given iniettore', function () { | ||
describe('when creating a new container without a contribution function', function () { | ||
it('should throw an Error', function () { | ||
function testCase() { | ||
iniettore.create() | ||
} | ||
expect(testCase).to.throw(Error, /missing contribution function/i) | ||
}) | ||
}) | ||
}) | ||
describe('Given a container', function () { | ||
@@ -10,7 +22,21 @@ | ||
beforeEach(function () { | ||
container = iniettore.create() | ||
describe('when registering a mapping with an invalid options combination', function () { | ||
it('should throw an Error', function () { | ||
function testCase() { | ||
iniettore.create(function (context) { | ||
context | ||
.map('foo').to({}) | ||
.as(SINGLETON, VALUE) | ||
}) | ||
} | ||
expect(testCase).to.throw(Error, /invalid flags combination/i) | ||
}) | ||
}) | ||
describe('when requesting an alias that has never registered before', function () { | ||
before(function () { | ||
container = iniettore.create(function () {}) | ||
}) | ||
it('should throw an Error', function () { | ||
@@ -33,6 +59,7 @@ function testCase() { | ||
beforeEach(function () { | ||
container | ||
.bind('foo', Foo) | ||
.as(CONSTRUCTOR) | ||
.done() | ||
container = iniettore.create(function (context) { | ||
context | ||
.map('foo').to(Foo) | ||
.as(CONSTRUCTOR) | ||
}) | ||
}) | ||
@@ -57,9 +84,10 @@ | ||
beforeEach(function () { | ||
container | ||
.bind('bar', providerStub) | ||
.as(SINGLETON, PROVIDER) | ||
.done() | ||
container = iniettore.create(function (context) { | ||
context | ||
.map('bar').to(providerStub) | ||
.as(TRANSIENT, SINGLETON, PROVIDER) | ||
}) | ||
}) | ||
describe('when disposing and instance it throws an Error', function () { | ||
describe('when releasing and instance it throws an Error', function () { | ||
@@ -71,3 +99,3 @@ it('should catch it and throw an Error specifing the failing component', function () { | ||
} | ||
expect(testCase).to.throw(Error, 'Failed while disposing \'bar\' due to:\n\tUnexpected issue') | ||
expect(testCase).to.throw(Error, 'Failed while releasing \'bar\' due to:\n\tUnexpected issue') | ||
}) | ||
@@ -96,7 +124,8 @@ }) | ||
container | ||
.bind('bar', Bar) | ||
.as(CONSTRUCTOR) | ||
.inject('bar') | ||
.done() | ||
container = iniettore.create(function (context) { | ||
context | ||
.map('bar').to(Bar) | ||
.as(CONSTRUCTOR) | ||
.injecting('bar') | ||
}) | ||
@@ -103,0 +132,0 @@ function testCase () { |
@@ -20,6 +20,6 @@ 'use strict' | ||
container = iniettore.create(function (container) { | ||
container | ||
.bind('bar', BAR).as(VALUE) | ||
.bind('foo', foo).as(FUNCTION).inject('bar') | ||
container = iniettore.create(function (context) { | ||
context | ||
.map('bar').to(BAR).as(VALUE) | ||
.map('foo').to(foo).as(FUNCTION).injecting('bar') | ||
}) | ||
@@ -26,0 +26,0 @@ |
@@ -7,3 +7,4 @@ 'use strict' | ||
PROVIDER, | ||
SINGLETON | ||
SINGLETON, | ||
TRANSIENT | ||
} from '../../src/options' | ||
@@ -21,4 +22,2 @@ | ||
beforeEach(function () { | ||
container = iniettore.create() | ||
container.bind('foo', 42).as(VALUE).done() | ||
providerStub.reset() | ||
@@ -38,8 +37,11 @@ providerStub | ||
container | ||
.bind('bar', providerStub) | ||
.as(PROVIDER) | ||
.inject('foo') | ||
.done() | ||
container = iniettore.create(function (context) { | ||
context | ||
.map('foo').to(42).as(VALUE) | ||
.map('bar').to(providerStub) | ||
.as(PROVIDER) | ||
.injecting('foo') | ||
}) | ||
result = container.get('bar') | ||
@@ -63,8 +65,10 @@ | ||
beforeEach(function () { | ||
container | ||
.bind('bar', providerStub) | ||
.as(SINGLETON, PROVIDER) | ||
.inject('foo') | ||
.done() | ||
before(function () { | ||
container = iniettore.create(function (context) { | ||
context | ||
.map('foo').to(42).as(VALUE) | ||
.map('bar').to(providerStub) | ||
.as(TRANSIENT, SINGLETON, PROVIDER) | ||
.injecting('foo') | ||
}) | ||
}) | ||
@@ -71,0 +75,0 @@ |
@@ -11,6 +11,2 @@ 'use strict' | ||
before(function () { | ||
}) | ||
describe('with a registered object as value', function () { | ||
@@ -21,3 +17,3 @@ | ||
container | ||
.bind('bar', DUMMY_VALUE) | ||
.map('bar').to(DUMMY_VALUE) | ||
.as(VALUE) | ||
@@ -39,3 +35,3 @@ }) | ||
container | ||
.bind('bar', DUMMY_VALUE) | ||
.map('bar').to(DUMMY_VALUE) | ||
.as(INSTANCE) | ||
@@ -42,0 +38,0 @@ }) |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
2190773
64
14652
1
94
100
49