Remove rejected promises from the cache

Summary: Rejected promises in the cache would prevent the cache from persisting. This removes rejected promises from the cache, so that subsequent successful cache updates can be persisted.

Reviewed By: cpojer

Differential Revision: D3726691

fbshipit-source-id: ddec03676a7a89264fe64b4af4b183cbead638fb
This commit is contained in:
David Aurelio 2016-08-16 17:39:09 -07:00 committed by Facebook Github Bot 7
parent 08cc4d6832
commit 9e20146f7f
2 changed files with 28 additions and 2 deletions

View File

@ -153,6 +153,30 @@ describe('Cache', () => {
) )
); );
}); });
it('does not cache rejections', () => {
fs.stat.mockImpl((file, callback) => {
callback(null, {
mtime: {
getTime: () => {},
},
});
});
var cache = new Cache({
cacheKey: 'cache',
});
var loaderCb = () => Promise.reject('lol');
return cache
.get('/rootDir/someFile', 'field', loaderCb)
.catch(() => {
var shouldBeCalled = jest.fn(() => Promise.resolve());
const assert = value => expect(shouldBeCalled).toBeCalled();
return cache.get('/rootDir/someFile', 'field', shouldBeCalled)
.then(assert, assert);
});
});
}); });
describe('loading cache from disk', () => { describe('loading cache from disk', () => {

View File

@ -87,7 +87,7 @@ class Cache {
this._data[filepath].metadata = Object.create(null); this._data[filepath].metadata = Object.create(null);
} }
record.data[field] = loaderPromise const cachedPromise = record.data[field] = loaderPromise
.then(data => Promise.all([ .then(data => Promise.all([
data, data,
denodeify(fs.stat)(filepath), denodeify(fs.stat)(filepath),
@ -106,7 +106,9 @@ class Cache {
return data; return data;
}); });
return record.data[field]; // don't cache rejected promises
cachedPromise.catch(error => delete record.data[field]);
return cachedPromise;
} }
_persistCache() { _persistCache() {