Skip to content

Commit 46f8eb0

Browse files
Jean LauliacMartin Konicek
Jean Lauliac
authored and
Martin Konicek
committed
packager: make legocastle script to write to the global cache
Reviewed By: davidaurelio Differential Revision: D4251001 fbshipit-source-id: bde1e50c82c2cb8f2c2577139061498c809aa37e
1 parent d414fdc commit 46f8eb0

File tree

2 files changed

+83
-5
lines changed

2 files changed

+83
-5
lines changed

packager/react-packager/src/lib/GlobalTransformCache.js

+59-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ type FetchResultURIs = (
2626
callback: (error?: Error, results?: Map<string, string>) => void,
2727
) => mixed;
2828

29+
type StoreResults = (
30+
resultsByKey: Map<string, CachedResult>,
31+
callback: (error?: Error) => void,
32+
) => mixed;
33+
2934
type FetchProps = {
3035
filePath: string,
3136
sourceCode: string,
@@ -158,6 +163,38 @@ class KeyURIFetcher {
158163

159164
}
160165

166+
class KeyResultStore {
167+
168+
_storeResults: StoreResults;
169+
_batchProcessor: BatchProcessor<{key: string, result: CachedResult}, void>;
170+
171+
_processResults(
172+
keyResults: Array<{key: string, result: CachedResult}>,
173+
callback: (error?: Error) => mixed,
174+
) {
175+
const resultsByKey = new Map(
176+
keyResults.map(pair => [pair.key, pair.result]),
177+
);
178+
this._storeResults(resultsByKey, error => {
179+
callback(error);
180+
});
181+
}
182+
183+
store(key: string, result: CachedResult) {
184+
this._batchProcessor.queue({key, result}, () => {});
185+
}
186+
187+
constructor(storeResults: StoreResults) {
188+
this._storeResults = storeResults;
189+
this._batchProcessor = new BatchProcessor({
190+
maximumDelayMs: 1000,
191+
maximumItems: 100,
192+
concurrency: 10,
193+
}, this._processResults.bind(this));
194+
}
195+
196+
}
197+
161198
function validateCachedResult(cachedResult: mixed): ?CachedResult {
162199
if (
163200
cachedResult != null &&
@@ -204,10 +241,17 @@ function globalizeTransformOptions(
204241
class GlobalTransformCache {
205242

206243
_fetcher: KeyURIFetcher;
244+
_store: ?KeyResultStore;
207245
static _global: ?GlobalTransformCache;
208246

209-
constructor(fetchResultURIs: FetchResultURIs) {
247+
constructor(
248+
fetchResultURIs: FetchResultURIs,
249+
storeResults?: StoreResults,
250+
) {
210251
this._fetcher = new KeyURIFetcher(fetchResultURIs);
252+
if (storeResults != null) {
253+
this._store = new KeyResultStore(storeResults);
254+
}
211255
}
212256

213257
/**
@@ -263,6 +307,12 @@ class GlobalTransformCache {
263307
});
264308
}
265309

310+
store(props: FetchProps, result: CachedResult) {
311+
if (this._store != null) {
312+
this._store.store(GlobalTransformCache.keyOf(props), result);
313+
}
314+
}
315+
266316
/**
267317
* For using the global cache one needs to have some kind of central key-value
268318
* store that gets prefilled using keyOf() and the transformed results. The
@@ -271,8 +321,14 @@ class GlobalTransformCache {
271321
* of returning the content directly allows for independent fetching of each
272322
* result.
273323
*/
274-
static configure(fetchResultURIs: FetchResultURIs) {
275-
GlobalTransformCache._global = new GlobalTransformCache(fetchResultURIs);
324+
static configure(
325+
fetchResultURIs: FetchResultURIs,
326+
storeResults?: StoreResults,
327+
) {
328+
GlobalTransformCache._global = new GlobalTransformCache(
329+
fetchResultURIs,
330+
storeResults,
331+
);
276332
}
277333

278334
static get() {

packager/react-packager/src/node-haste/Module.js

+24-2
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,28 @@ class Module {
230230
);
231231
}
232232

233+
_transformAndStoreCodeGlobally(
234+
cacheProps: ReadTransformProps,
235+
globalCache: GlobalTransformCache,
236+
callback: (error: ?Error, result: ?TransformedCode) => void,
237+
) {
238+
this._transformCodeForCallback(
239+
cacheProps,
240+
(transformError, transformResult) => {
241+
if (transformError != null) {
242+
callback(transformError);
243+
return;
244+
}
245+
invariant(
246+
transformResult != null,
247+
'Inconsistent state: there is no error, but no results either.',
248+
);
249+
globalCache.store(cacheProps, transformResult);
250+
callback(undefined, transformResult);
251+
},
252+
);
253+
}
254+
233255
_getTransformedCode(
234256
cacheProps: ReadTransformProps,
235257
callback: (error: ?Error, result: ?TransformedCode) => void,
@@ -253,8 +275,8 @@ class Module {
253275
));
254276
}
255277
}
256-
if (globalCacheError != null || globalCachedResult == null) {
257-
this._transformCodeForCallback(cacheProps, callback);
278+
if (globalCachedResult == null) {
279+
this._transformAndStoreCodeGlobally(cacheProps, globalCache, callback);
258280
return;
259281
}
260282
callback(undefined, globalCachedResult);

0 commit comments

Comments
 (0)