mirror of https://github.com/status-im/metro.git
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:
parent
08cc4d6832
commit
9e20146f7f
|
@ -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', () => {
|
||||||
|
|
|
@ -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() {
|
||||||
|
|
Loading…
Reference in New Issue