Added cache to module-loader, should speed things up a bit

This commit is contained in:
Dallon Feldner
2012-11-21 10:43:51 -07:00
parent 2172875108
commit d84351bebd

View File

@@ -7,7 +7,11 @@ var fs = require('fs')
, q = require('q')
, async = require('async');
var cache;
module.exports = function loadModules(basepath, fn) {
if (cache) fn(null, cache);
if(typeof basepath == 'function') {
fn = basepath;
basepath = undefined;
@@ -54,6 +58,7 @@ module.exports = function loadModules(basepath, fn) {
});
modulesQ.then(function(modules) {
cache = modules;
fn(null, modules);
}, function(error) {
fn(error);
@@ -61,6 +66,10 @@ module.exports = function loadModules(basepath, fn) {
};
module.exports.clearCache = function(){
cache = null;
};
function loadModule(file, fn) {
var statQ = q.ninvoke(fs, 'stat', file);
var moduleQ = statQ.then(function(stat) {