metro-bundler: elaborate the global cache test a bit

Reviewed By: davidaurelio

Differential Revision: D5650882

fbshipit-source-id: b1f283f00e6e200f3b731126ed7a273fe782aeaa
This commit is contained in:
Jean Lauliac 2017-08-18 04:04:54 -07:00 committed by Facebook Github Bot
parent 55986976b1
commit 936b8c31e7
1 changed files with 13 additions and 10 deletions

View File

@ -50,13 +50,14 @@ export type GlobalTransformCache = {
fetch(props: FetchProps): Promise<?CachedResult>, fetch(props: FetchProps): Promise<?CachedResult>,
/** /**
* Try to store a result, without waiting for the success or failure of the * Try to store a result. Callsites won't necessarily wait for the success or
* operation. Consequently, the actual storage operation could be done at a * failure of the Promise, so errors may be handled internally, they may
* much later point if desired. It is recommended to actually have this * otherwise be silently ignored. The actual storage operation could be done
* function be a no-op in production, and only do the storage operation from * at a later point if desired. It is recommended to have this function be a
* a script running on your Continuous Integration platform. * no-op in production, and only do the storage operation from a script
* running on a Continuous Integration platform.
*/ */
store(props: FetchProps, result: CachedResult): void, store(props: FetchProps, result: CachedResult): Promise<void>,
}; };
type FetchResultURIs = (keys: Array<string>) => Promise<Map<string, string>>; type FetchResultURIs = (keys: Array<string>) => Promise<Map<string, string>>;
@ -112,6 +113,7 @@ type KeyedResult = {key: string, result: CachedResult};
class KeyResultStore { class KeyResultStore {
_storeResults: StoreResults; _storeResults: StoreResults;
_batchProcessor: BatchProcessor<KeyedResult, void>; _batchProcessor: BatchProcessor<KeyedResult, void>;
_promises: Array<Promise<void>>;
async _processResults(keyResults: Array<KeyedResult>): Promise<Array<void>> { async _processResults(keyResults: Array<KeyedResult>): Promise<Array<void>> {
const resultsByKey = new Map( const resultsByKey = new Map(
@ -121,8 +123,8 @@ class KeyResultStore {
return new Array(keyResults.length); return new Array(keyResults.length);
} }
store(key: string, result: CachedResult) { async store(key: string, result: CachedResult): Promise<void> {
this._batchProcessor.queue({key, result}); await this._batchProcessor.queue({key, result});
} }
constructor(storeResults: StoreResults) { constructor(storeResults: StoreResults) {
@ -135,6 +137,7 @@ class KeyResultStore {
}, },
this._processResults.bind(this), this._processResults.bind(this),
); );
this._promises = [];
} }
} }
@ -346,9 +349,9 @@ class URIBasedGlobalTransformCache {
return await this._fetchResultFromURI(uri); return await this._fetchResultFromURI(uri);
} }
store(props: FetchProps, result: CachedResult) { async store(props: FetchProps, result: CachedResult): Promise<void> {
if (this._store != null) { if (this._store != null) {
this._store.store(this.keyOf(props), result); await this._store.store(this.keyOf(props), result);
} }
} }
} }