From a2464d244b3011e7a9d875e9cbe1ee4329d6ef87 Mon Sep 17 00:00:00 2001 From: indexzero Date: Sun, 25 Sep 2011 00:46:28 -0400 Subject: [PATCH] [api] Load sources into the default system store so they are permenantly cached --- lib/nconf/provider.js | 57 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 12 deletions(-) diff --git a/lib/nconf/provider.js b/lib/nconf/provider.js index a88e8ba..10e31ca 100644 --- a/lib/nconf/provider.js +++ b/lib/nconf/provider.js @@ -292,8 +292,7 @@ Provider.prototype.merge = function () { // Provider.prototype.load = function (callback) { var self = this, - stores = this._stores.map(function (name) { return self[name] }) - .concat(this.sources); + stores = this._stores.map(function (name) { return self[name] }); function loadStoreSync(store) { if (!store.loadSync) { @@ -313,18 +312,52 @@ Provider.prototype.load = function (callback) { : store.load(next); } - // - // If we don't have a callback and the current - // store is capable of loading synchronously - // then do so. - // - if (!callback) { - return common.merge(stores.map(loadStoreSync)); + function loadBatch (targets, done) { + if (!done) { + return common.merge(targets.map(loadStoreSync)); + } + + async.map(targets, loadStore, function (err, objs) { + return err ? done(err) : done(null, common.merge(objs)); + }); } - async.map(stores, loadStore, function (err, objs) { - return err ? callback(err) : callback(null, common.merge(objs)); - }); + function mergeSources (data) { + // + // If `data` was returned then merge it into + // the system store. + // + if (data && typeof data === 'object') { + Object.keys(data).forEach(function (key) { + self.system.merge(key, data[key]); + }); + } + } + + function loadSources () { + // + // If we don't have a callback and the current + // store is capable of loading synchronously + // then do so. + // + if (!callback) { + mergeSources(loadBatch(self.sources)); + return loadBatch(stores); + } + + loadBatch(self.sources, function (err, data) { + if (err) { + return callback(err); + } + + mergeSources(data); + return loadBatch(stores, callback); + }); + } + + return self.sources.length + ? loadSources() + : loadBatch(stores, callback); }; //