Comparing version 0.0.18 to 0.0.19
@@ -93,1 +93,9 @@ 'use strict'; | ||
}; | ||
/** | ||
* For debugging purposes | ||
* | ||
setTimeout(function () { | ||
console.log(require('util').inspect(mc.$modules, {colors: true, depth: null})); | ||
}, 5000); | ||
//*/ |
@@ -11,2 +11,29 @@ 'use strict'; | ||
function executeCallbacks(callbacks, err, value) { | ||
_.each(callbacks, function (cb) { | ||
cb(err, value); | ||
}); | ||
callbacks.splice(0, callbacks.length); | ||
} | ||
function isDependentOf(modules, modA, modB) { | ||
var lookedIn = []; | ||
var toLook = [modB]; | ||
while (toLook.length > 0) { | ||
lookedIn = lookedIn.concat(toLook); | ||
var tmp = toLook; | ||
toLook = []; | ||
if (_.some(tmp, function (mod) { | ||
toLook = toLook.concat(_.difference(tmp, lookedIn)); | ||
return modules[mod].dependent.indexOf(modA) >= 0; | ||
})) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
function ModuleContext(config, define) { | ||
@@ -35,4 +62,6 @@ if (!(this instanceof ModuleContext)) { | ||
moduleFn(function (err, module) { | ||
this.$modules[resolvedName].callbacks.push(callback); | ||
this.$modules[resolvedName].loading = false; | ||
if (err) { | ||
return callback(err); | ||
return executeCallbacks(this.$modules[resolvedName].callbacks, err); | ||
} | ||
@@ -45,8 +74,3 @@ if (module.constructor.name === 'Object') { | ||
} | ||
this.$modules[resolvedName].loading = false; | ||
_.each(this.$modules[resolvedName].callbacks, function (callback) { | ||
callback(null, this.$modules[resolvedName].content); | ||
}.bind(this)); | ||
this.$modules[resolvedName].callbacks = []; | ||
callback(null, this.$modules[resolvedName].content); | ||
executeCallbacks(this.$modules[resolvedName].callbacks, null, this.$modules[resolvedName].content); | ||
}.bind(this)); | ||
@@ -60,3 +84,3 @@ }; | ||
if (this.$modules[name].loading) { | ||
if (this.$modules[callee].dependent.indexOf(name) < 0) { | ||
if (!isDependentOf(this.$modules, callee, name)) {//this.$modules[callee].dependent.indexOf(name) < 0) { | ||
this.$modules[name].callbacks.push(callback); | ||
@@ -63,0 +87,0 @@ return; |
{ | ||
"name": "r42", | ||
"version": "0.0.18", | ||
"version": "0.0.19", | ||
"description": "Dependency injection done right.", | ||
@@ -5,0 +5,0 @@ "author": "Quentin Raynaud <npm@qraynaud.eu>", |
14485
302