Skip to content

Commit

Permalink
[api] Load sources into the default system store so they are permenan…
Browse files Browse the repository at this point in the history
…tly cached
  • Loading branch information
indexzero committed Sep 25, 2011
1 parent e243b0b commit a2464d2
Showing 1 changed file with 45 additions and 12 deletions.
57 changes: 45 additions & 12 deletions lib/nconf/provider.js
Expand Up @@ -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) {
Expand All @@ -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);
};

//
Expand Down

0 comments on commit a2464d2

Please sign in to comment.