Skip to content
Browse files

code

  • Loading branch information...
1 parent a0cde55 commit ebc23943cf9fde4bcb7e574d1db4924fc2499414 @isaacs committed May 30, 2012
Showing with 41 additions and 0 deletions.
  1. +41 −0 emcee.js
View
41 emcee.js
@@ -0,0 +1,41 @@
+module.exports = exports = MC
+
+function MC () {
+ this._loading = 0
+ this.models = {}
+}
+
+var modelLoaders = {}
+MC.model = function (name, loader) {
+ modelLoaders[name] = loader
+ return MC
+}
+
+MC.prototype.load = function (name) {
+ if (!modelLoaders[name]) {
+ throw new Error('Unknown model: ' + name)
+ }
+ if (this.error) return
+ var a = new Array(arguments.length)
+ for (var i = 1; i < arguments.length; i ++) {
+ a[i-1] = arguments[i]
+ }
+ a[i-1] = next.bind(this)
+ this._loading ++
+ modelLoaders[name].apply(this, a)
+ function next (er, data) {
+ if (this.error) return
+ this.error = er
+ this.models[name] = data
+
+ this._loading --
+ if (er || this._loading === 0) {
+ this.ondone(this.error, this.models)
+ }
+ }
+}
+
+MC.prototype.end = function (cb) {
+ this.ondone = cb
+ if (this._loading === 0) this.ondone(this.error, this.models)
+}

0 comments on commit ebc2394

Please sign in to comment.
Something went wrong with that request. Please try again.