From 029e5c1ec39fd35c9edf74c680f7c742e12486f0 Mon Sep 17 00:00:00 2001 From: Carlo Zancanaro Date: Fri, 19 Sep 2014 12:19:31 +1000 Subject: Initial commit - minimal, synchronous, injector --- injector.min.js | 1 + 1 file changed, 1 insertion(+) create mode 100644 injector.min.js (limited to 'injector.min.js') diff --git a/injector.min.js b/injector.min.js new file mode 100644 index 0000000..f1cf96d --- /dev/null +++ b/injector.min.js @@ -0,0 +1 @@ +/*! injector 0.1.0 */var Injector=function(){var a=function(){this.specs={},this.values={},this.stack=[],this.usedRequestor=null};a.prototype.register=function(a,b){if("function"==typeof a||a instanceof Function)this.specs[a.name]=a;else if("string"==typeof a||a instanceof String)this.specs[a]=b;else for(var c in a)this.register(c,a[c]);return this},a.prototype.get=function(a){if(a in this.specs){var b=this.usedRequestor;this.usedRequestor=!1,this.stack.push(a);try{f(a,this.stack);var c=this.invoke(this.specs[a]);return this.usedRequestor?c:(delete this.specs[a],this.values[a]=c)}catch(d){throw d instanceof g?d:new g("Error constructing value for "+e(this.stack),d)}finally{this.stack.pop(),this.usedRequestor=b}}if(a in this.values)return this.values[a];throw new g("Dependency "+a+" not found")},a.prototype.requestor=function(){switch(this.stack.length){case 0:throw new g("Cannot use requestor for invoked function - none exists");case 1:throw new g("Cannot use requestor for top-level constructor - none exists");default:return this.usedRequestor===!1&&(this.usedRequestor=!0),this.stack[this.stack.length-2]}},a.prototype.invoke=function(a){var b=d(a),c=b[0],e=b[1];return c.apply(this,e.map(function(a){return this.get(a)},this))},a.prototype.construct=function(){};var b=/^function[^(]*\(([^)]*)\)/,c=function(a){var c=b.exec(a.toString().replace(/\s+/g,""));if(null==c)throw new Error("Unable to parse fn definition");return c[1]?c[1].split(/,/):[]},d=function(a){var b,d;return"function"==typeof a||a instanceof Function?(d=c(a),b=a):(b=a[a.length-1],d=a.slice(0,a.length-1)),[b,d]},e=function(a){a.reverse();var b=a.join(" <- ");return a.reverse(),b},f=function(a,b){if(b.indexOf(a)!=b.length-1)throw new g("Cyclic dependency detected "+e(b))},g=function(a,b){this.name="InjectorError",this.message=a,this.cause=b};return g.prototype=new Error,g.prototype.constructor=g,g.prototype.toString=function(){return"InjectorError: "+this.message+(this.cause?" [caused by "+this.cause+"]":"")},a}();"undefined"!=typeof module&&(module.exports=Injector); \ No newline at end of file -- cgit v1.2.3