UNPKG

@owja/ioc

Version:

dependency injection for javascript

3 lines (2 loc) 2.12 kB
var n=function(n){this.t=n};n.prototype.inSingletonScope=function(){this.t.singleton=!0};var t=function(n){this.t=n};t.prototype.to=function(t){return this.t.object=t,new n(this.t)},t.prototype.toFactory=function(t){return this.t.factory=t,new n(this.t)},t.prototype.toValue=function(n){if(void 0===n)throw"cannot bind a value of type undefined";this.t.value=n};var i=function(){this.i=new Map,this.o=[]};i.prototype.bind=function(n){return new t(this.u(n))},i.prototype.rebind=function(n){return this.remove(n).bind(n)},i.prototype.remove=function(n){if(void 0===this.i.get(n))throw n.toString()+" was never bound";return this.i.delete(n),this},i.prototype.get=function(n){var t=this.i.get(n);if(void 0===t)throw"nothing bound to "+n.toString();var i=t.object,r=t.factory,o=t.value,u=t.cache,e=t.singleton,f=function(n){return e&&void 0!==u?u:e?(t.cache=n(),t.cache):n()};if(void 0!==o)return o;if(void 0!==i)return f(function(){return new i});if(void 0!==r)return f(function(){return r()});throw"nothing is bound to "+n.toString()},i.prototype.snapshot=function(){return this.o.push(new Map(this.i)),this},i.prototype.restore=function(){return this.i=this.o.pop()||this.i,this},i.prototype.u=function(n){if(void 0!==this.i.get(n))throw"object can only bound once: "+n.toString();var t={singleton:!1};return this.i.set(n,t),t};var r=Symbol("NOCACHE");function o(n,t,i,o,u){Object.defineProperty(n,t,{get:function(){var n=i.get(o);return-1===u.indexOf(r)&&Object.defineProperty(this,t,{value:n,enumerable:!0}),n},configurable:!0,enumerable:!0})}function u(n){return function(t){for(var i=[],r=arguments.length-1;r-- >0;)i[r]=arguments[r+1];return function(n,t,i){return function(r,u){o(r,u,t,n,i)}}(t,n,i)}}function e(n){return function(t,i,r){for(var u=[],e=arguments.length-3;e-- >0;)u[e]=arguments[e+3];o(t,i,n,r,u)}}function f(n){return function(t){for(var i,o=[],u=arguments.length-1;u-- >0;)o[u]=arguments[u+1];return function(){return-1===o.indexOf(r)&&void 0!==i||(i=n.get(t)),i}}}export{i as Container,u as createDecorator,e as createWire,f as createResolve,r as NOCACHE}; //# sourceMappingURL=ioc.module.js.map