[api] Load sources into the default system store so they are permenantly cached

master
indexzero 2011-09-25 00:46:28 -04:00
parent e243b0befe
commit a2464d244b
1 changed files with 45 additions and 12 deletions

View File

@ -292,8 +292,7 @@ Provider.prototype.merge = function () {
// //
Provider.prototype.load = function (callback) { Provider.prototype.load = function (callback) {
var self = this, var self = this,
stores = this._stores.map(function (name) { return self[name] }) stores = this._stores.map(function (name) { return self[name] });
.concat(this.sources);
function loadStoreSync(store) { function loadStoreSync(store) {
if (!store.loadSync) { if (!store.loadSync) {
@ -313,18 +312,52 @@ Provider.prototype.load = function (callback) {
: store.load(next); : store.load(next);
} }
// function loadBatch (targets, done) {
// If we don't have a callback and the current if (!done) {
// store is capable of loading synchronously return common.merge(targets.map(loadStoreSync));
// then do so. }
//
if (!callback) { async.map(targets, loadStore, function (err, objs) {
return common.merge(stores.map(loadStoreSync)); return err ? done(err) : done(null, common.merge(objs));
});
} }
async.map(stores, loadStore, function (err, objs) { function mergeSources (data) {
return err ? callback(err) : callback(null, common.merge(objs)); //
}); // 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);
}; };
// //