packager: make legocastle script to write to the global cache

Reviewed By: davidaurelio

Differential Revision: D4251001

fbshipit-source-id: bde1e50c82c2cb8f2c2577139061498c809aa37e
This commit is contained in:
Jean Lauliac 2016-12-02 06:54:45 -08:00 коммит произвёл Facebook Github Bot
Родитель 779508c0ba
Коммит e5de8bce61
2 изменённых файлов: 83 добавлений и 5 удалений

Просмотреть файл

@ -26,6 +26,11 @@ type FetchResultURIs = (
callback: (error?: Error, results?: Map<string, string>) => void, callback: (error?: Error, results?: Map<string, string>) => void,
) => mixed; ) => mixed;
type StoreResults = (
resultsByKey: Map<string, CachedResult>,
callback: (error?: Error) => void,
) => mixed;
type FetchProps = { type FetchProps = {
filePath: string, filePath: string,
sourceCode: string, sourceCode: string,
@ -158,6 +163,38 @@ class KeyURIFetcher {
} }
class KeyResultStore {
_storeResults: StoreResults;
_batchProcessor: BatchProcessor<{key: string, result: CachedResult}, void>;
_processResults(
keyResults: Array<{key: string, result: CachedResult}>,
callback: (error?: Error) => mixed,
) {
const resultsByKey = new Map(
keyResults.map(pair => [pair.key, pair.result]),
);
this._storeResults(resultsByKey, error => {
callback(error);
});
}
store(key: string, result: CachedResult) {
this._batchProcessor.queue({key, result}, () => {});
}
constructor(storeResults: StoreResults) {
this._storeResults = storeResults;
this._batchProcessor = new BatchProcessor({
maximumDelayMs: 1000,
maximumItems: 100,
concurrency: 10,
}, this._processResults.bind(this));
}
}
function validateCachedResult(cachedResult: mixed): ?CachedResult { function validateCachedResult(cachedResult: mixed): ?CachedResult {
if ( if (
cachedResult != null && cachedResult != null &&
@ -204,10 +241,17 @@ function globalizeTransformOptions(
class GlobalTransformCache { class GlobalTransformCache {
_fetcher: KeyURIFetcher; _fetcher: KeyURIFetcher;
_store: ?KeyResultStore;
static _global: ?GlobalTransformCache; static _global: ?GlobalTransformCache;
constructor(fetchResultURIs: FetchResultURIs) { constructor(
fetchResultURIs: FetchResultURIs,
storeResults?: StoreResults,
) {
this._fetcher = new KeyURIFetcher(fetchResultURIs); this._fetcher = new KeyURIFetcher(fetchResultURIs);
if (storeResults != null) {
this._store = new KeyResultStore(storeResults);
}
} }
/** /**
@ -263,6 +307,12 @@ class GlobalTransformCache {
}); });
} }
store(props: FetchProps, result: CachedResult) {
if (this._store != null) {
this._store.store(GlobalTransformCache.keyOf(props), result);
}
}
/** /**
* For using the global cache one needs to have some kind of central key-value * For using the global cache one needs to have some kind of central key-value
* store that gets prefilled using keyOf() and the transformed results. The * store that gets prefilled using keyOf() and the transformed results. The
@ -271,8 +321,14 @@ class GlobalTransformCache {
* of returning the content directly allows for independent fetching of each * of returning the content directly allows for independent fetching of each
* result. * result.
*/ */
static configure(fetchResultURIs: FetchResultURIs) { static configure(
GlobalTransformCache._global = new GlobalTransformCache(fetchResultURIs); fetchResultURIs: FetchResultURIs,
storeResults?: StoreResults,
) {
GlobalTransformCache._global = new GlobalTransformCache(
fetchResultURIs,
storeResults,
);
} }
static get() { static get() {

Просмотреть файл

@ -230,6 +230,28 @@ class Module {
); );
} }
_transformAndStoreCodeGlobally(
cacheProps: ReadTransformProps,
globalCache: GlobalTransformCache,
callback: (error: ?Error, result: ?TransformedCode) => void,
) {
this._transformCodeForCallback(
cacheProps,
(transformError, transformResult) => {
if (transformError != null) {
callback(transformError);
return;
}
invariant(
transformResult != null,
'Inconsistent state: there is no error, but no results either.',
);
globalCache.store(cacheProps, transformResult);
callback(undefined, transformResult);
},
);
}
_getTransformedCode( _getTransformedCode(
cacheProps: ReadTransformProps, cacheProps: ReadTransformProps,
callback: (error: ?Error, result: ?TransformedCode) => void, callback: (error: ?Error, result: ?TransformedCode) => void,
@ -253,8 +275,8 @@ class Module {
)); ));
} }
} }
if (globalCacheError != null || globalCachedResult == null) { if (globalCachedResult == null) {
this._transformCodeForCallback(cacheProps, callback); this._transformAndStoreCodeGlobally(cacheProps, globalCache, callback);
return; return;
} }
callback(undefined, globalCachedResult); callback(undefined, globalCachedResult);