Throw if there's an error persisting the cache

Reviewed By: davidaurelio

Differential Revision: D2824116

fb-gh-sync-id: 46b3c51a11d22e2da750f9c5a4b431a88004cd81
This commit is contained in:
Martín Bigio 2016-01-15 10:31:22 -08:00 committed by facebook-github-bot-1
parent dd06596595
commit 38486dcbba
1 changed files with 13 additions and 9 deletions

View File

@ -109,18 +109,18 @@ class Cache {
return this._persisting;
}
var data = this._data;
var cacheFilepath = this._cacheFilePath;
const data = this._data;
const cacheFilepath = this._cacheFilePath;
var allPromises = getObjectValues(data)
const allPromises = getObjectValues(data)
.map(record => {
var fieldNames = Object.keys(record.data);
var fieldValues = getObjectValues(record.data);
const fieldNames = Object.keys(record.data);
const fieldValues = getObjectValues(record.data);
return Promise
.all(fieldValues)
.then(ref => {
var ret = Object.create(null);
const ret = Object.create(null);
ret.metadata = record.metadata;
ret.data = Object.create(null);
fieldNames.forEach((field, index) =>
@ -134,18 +134,22 @@ class Cache {
this._persisting = Promise.all(allPromises)
.then(values => {
var json = Object.create(null);
const json = Object.create(null);
Object.keys(data).forEach((key, i) => {
if (!values[i]) {
// make sure the key wasn't added nor removed after we started
// persisting the cache
const value = values[i];
if (!value) {
return;
}
json[key] = Object.create(null);
json[key].metadata = data[key].metadata;
json[key].data = values[i].data;
json[key].data = value.data;
});
return Promise.denodeify(fs.writeFile)(cacheFilepath, JSON.stringify(json));
})
.catch(e => console.error('Error while persisting cache:', e.message))
.then(() => {
this._persisting = null;
return true;