Skip to content

Instantly share code, notes, and snippets.

@naneau
Created April 4, 2011 18:08
Show Gist options
  • Save naneau/902096 to your computer and use it in GitHub Desktop.
Save naneau/902096 to your computer and use it in GitHub Desktop.
Yeah, I don't think I could've written this without coffeescript...
(function() {
var a,
b,
c = Object.prototype.hasOwnProperty,
d = function(a, b) {
function e() {
this.constructor = a
}
for (var d in b) c.call(b, d) && (a[d] = b[d]);
e.prototype = b.prototype,
a.prototype = new e,
a.__super__ = b.prototype;
return a
},
e = function(a, b) {
return function() {
return a.apply(b, arguments)
}
},
f = Array.prototype.slice;
b = require("events").EventEmitter,
a = function() {
function a(a) {
a == null && (a = {}),
this.config = this.parseConfig(a),
this.resolving = {},
this.resolved = {},
this.required = {},
void 0
}
d(a, b),
a.prototype.loadConfig = function(a) {
return this.addConfig(require(a))
},
a.prototype.addConfig = function(a) {
var b,
c,
d,
e;
c = this.parseConfig(a),
e = [];
for (b in c) {
d = c[b];
if (this.config[b] != null) throw new Error("" + b + " already exists");
e.push(this.config[b] = d)
}
return e
},
a.prototype.parseConfig = function(a) {
var b,
c,
d,
f;
c = {},
f = e(function(a, b) {
return c[a] = this.parseConfigItem(b)
},
this);
for (b in a) d = a[b],
f(b, d);
return c
},
a.prototype.parseConfigItem = function(a) {
var b,
c,
d;
a.loadConfig && (a = require(a.loadConfig));
if (a.inject != null && a.callbackInject != null) throw new Error("Can not have both `inject` and `callbackInject` in one config item");
if (a.children != null) {
d = a.children;
for (c in d) {
b = d[c];
if (typeof b == "string") continue;
a.children[c] = this.parseConfigItem(b)
}
}
return a
},
a.prototype.get = function(a, b) {
var c;
this.resolved[a] != null && b(this.resolved[a]),
c = this.getDiConfig(a);
if (c == null) throw new Error("Can not resolve " + a);
this.getWithStack(a, [], e(function(d) {
if (c.store == null || c.store) this.resolved[a] = d;
return b(d)
},
this));
return
},
a.prototype.getWithStack = function(a, b, c) {
var d,
g,
h,
i,
j;
if (function() {
var c,
d,
e;
e = [];
for (c = 0, d = b.length; c < d; c++) j = b[c],
j === a && e.push(j);
return e
} ().length > 0) throw new Error("Circular dependency for " + a);
b.push(a),
i = this.resolveParam(a);
if (i != null) return c(i);
d = this.getDiConfig(a);
if (d.injectCallback != null) return this.getServiceThroughCallbackInjection(a, b, c);
h = this.require(a);
if (function() {
var a;
a = [];
for (g in d)(g === "call" || g === "instantiate" || g === "inject" || g === "callback") && a.push(g);
return a
} ().length === 0) return c(h);
if (this.isResolving(a)) return this.on("async." + a,
function(a) {
return c(a)
});
this.setResolving(a);
if (d.inject == null) return this.handleAsync(a, this.instantiate(h, d), d, c);
this.resolveInjection(d.inject, b, e(function() {
var b;
b = 1 <= arguments.length ? f.call(arguments, 0) : [];
return this.handleAsync(a, this.instantiate.apply(this, [h, d].concat(f.call(b))), d, c)
},
this));
return
},
a.prototype.resolveInjection = function(a, b, c) {
var d,
f;
f = [],
d = e(function() {
var g;
g = a.shift();
if (g == null) return c.apply(null, f);
return this.getWithStack(g, b, e(function(a) {
var c,
e,
h;
for (e = 0, h = b.length; e < h; e++) c = b[e],
c === !g && (b = c);
f.push(a);
return d()
},
this))
},
this),
d();
return
},
a.prototype.getServiceThroughCallbackInjection = function(a, b, c) {
var d;
this.setResolving(a),
d = this.getDiConfig(a);
return this.resolveInjection(d.injectCallback, b, e(function() {
var b;
b = 1 <= arguments.length ? f.call(arguments, 0) : [];
return d.callback.apply(d, f.call(b).concat([e(function(b) {
this.setResolved(a, b);
return c(b)
},
this)]))
},
this))
},
a.prototype.instantiate = function() {
var a,
b,
c;
c = arguments[0],
a = arguments[1],
b = 3 <= arguments.length ? f.call(arguments, 2) : [];
if (typeof a.instantiate == "string") return function(a, b, c) {
c.prototype = a.prototype;
var d = new c,
e = a.apply(d, b);
return typeof e == "object" ? e: d
} (c[a.instantiate], b,
function() {});
if (a.call) return c[a.call].apply(c, b);
return function(a, b, c) {
c.prototype = a.prototype;
var d = new c,
e = a.apply(d, b);
return typeof e == "object" ? e: d
} (c, b,
function() {})
},
a.prototype.setResolving = function(a) {
return this.resolving[a] = !0
},
a.prototype.isResolving = function(a) {
return this.resolving[a] != null
},
a.prototype.setResolved = function(a, b) {
this.emit("async." + a, b),
this.removeAllListeners("async." + a);
return delete this.resolving[a]
},
a.prototype.handleAsync = function(a, b, c, d) {
var f;
f = e(function(b) {
this.setResolved(a, b);
return d(b)
},
this);
if (c.callback == null) return f(b);
return c.callback(b,
function(a) {
return f(a)
})
},
a.prototype.getDiConfig = function(a) {
var b,
c,
d,
e,
f,
g,
h;
c = a.split("."),
d = (g = this.config) != null ? g[c.shift()] : void 0;
for (e = 0, f = c.length; e < f; e++) b = c[e],
d = d != null ? (h = d.children) != null ? h[b] : void 0: void 0;
if (d == null) throw "Can not find configuration for " + a;
return d
},
a.prototype.resolveParam = function(a) {
var b,
c,
d,
e;
d = a.split(".");
if (! (d.length < 2)) {
c = d.pop(),
b = this.getDiConfig(d.join("."));
return b != null ? (e = b.params) != null ? e[c] : void 0: void 0
}
},
a.prototype.require = function(a) {
var b,
c;
b = this.getDiConfig(a);
if (b.require == null && typeof b != "string") throw new Error("Can not instantiate " + a + ", missing require statement");
this.required[a] != null ? c = this.required[a] : c = require(typeof b == "string" ? b: b.require);
if (b.storeRequire == null || b.storeRequire) return this.required[a] = c
};
return a
} (),
module.exports = {
Container: a
}
}).call(this)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment