From f51d9718da8d99428a8d9592b251044123204ce2 Mon Sep 17 00:00:00 2001 From: Christoph Pojer Date: Sun, 31 Jan 2016 23:24:39 -0800 Subject: [PATCH] Add per-field cache invalidation Summary: Jest needs this for efficient caching of resolution responses. public Reviewed By: voideanvalue Differential Revision: D2873291 fb-gh-sync-id: fee27d2ffdfe64bd68fdb4d9e4259e721b33631f --- .../Cache/__tests__/Cache-test.js | 29 +++++++++++++++++++ .../src/DependencyResolver/Cache/index.js | 12 +++++--- .../DependencyGraph/index.js | 2 +- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/react-packager/src/DependencyResolver/Cache/__tests__/Cache-test.js b/react-packager/src/DependencyResolver/Cache/__tests__/Cache-test.js index 22d6825e..2de2318e 100644 --- a/react-packager/src/DependencyResolver/Cache/__tests__/Cache-test.js +++ b/react-packager/src/DependencyResolver/Cache/__tests__/Cache-test.js @@ -303,4 +303,33 @@ describe('Cache', () => { }); }); }); + + describe('invalidate', () => { + it('invalidates the cache per file or per-field', () => { + fs.stat.mockImpl((file, callback) => + callback(null, { + mtime: { + getTime: () => {}, + }, + }) + ); + + var cache = new Cache({ + cacheKey: 'cache', + }); + var loaderCb = jest.genMockFn().mockImpl(() => + Promise.resolve('banana') + ); + var file = '/rootDir/someFile'; + + return cache.get(file, 'field', loaderCb).then(() => { + expect(cache.has(file)).toBe(true); + cache.invalidate(file, 'field'); + expect(cache.has(file)).toBe(true); + expect(cache.has(file, 'field')).toBe(false); + cache.invalidate(file); + expect(cache.has(file)).toBe(false); + }); + }); + }); }); diff --git a/react-packager/src/DependencyResolver/Cache/index.js b/react-packager/src/DependencyResolver/Cache/index.js index 40f16c77..06d346d6 100644 --- a/react-packager/src/DependencyResolver/Cache/index.js +++ b/react-packager/src/DependencyResolver/Cache/index.js @@ -58,9 +58,13 @@ class Cache { return recordP.then(record => record); } - invalidate(filepath) { - if (this.has(filepath)) { - delete this._data[filepath]; + invalidate(filepath, field) { + if (this.has(filepath, field)) { + if (field == null) { + delete this._data[filepath]; + } else { + delete this._data[filepath].data[field]; + } } } @@ -70,7 +74,7 @@ class Cache { has(filepath, field) { return Object.prototype.hasOwnProperty.call(this._data, filepath) && - (!field || Object.prototype.hasOwnProperty.call(this._data[filepath].data, field)); + (field == null || Object.prototype.hasOwnProperty.call(this._data[filepath].data, field)); } _set(filepath, field, loaderPromise) { diff --git a/react-packager/src/DependencyResolver/DependencyGraph/index.js b/react-packager/src/DependencyResolver/DependencyGraph/index.js index 273b9b58..6fda8d93 100644 --- a/react-packager/src/DependencyResolver/DependencyGraph/index.js +++ b/react-packager/src/DependencyResolver/DependencyGraph/index.js @@ -59,7 +59,7 @@ class DependencyGraph { mocksPattern, extractRequires, shouldThrowOnUnresolvedErrors, - transformCode + transformCode, }; this._cache = cache; this._helpers = new DependencyGraphHelpers(this._opts);