@@ -26,6 +26,11 @@ type FetchResultURIs = (
26
26
callback : ( error ? : Error , results ? : Map < string , string > ) => void ,
27
27
) = > mixed ;
28
28
29
+ type StoreResults = (
30
+ resultsByKey : Map < string , CachedResult > ,
31
+ callback : ( error ? : Error ) => void ,
32
+ ) => mixed ;
33
+
29
34
type FetchProps = {
30
35
filePath : string ,
31
36
sourceCode : string ,
@@ -158,6 +163,38 @@ class KeyURIFetcher {
158
163
159
164
}
160
165
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
+
161
198
function validateCachedResult ( cachedResult : mixed ) : ?CachedResult {
162
199
if (
163
200
cachedResult != null &&
@@ -204,10 +241,17 @@ function globalizeTransformOptions(
204
241
class GlobalTransformCache {
205
242
206
243
_fetcher : KeyURIFetcher ;
244
+ _store : ?KeyResultStore ;
207
245
static _global : ?GlobalTransformCache ;
208
246
209
- constructor ( fetchResultURIs : FetchResultURIs ) {
247
+ constructor (
248
+ fetchResultURIs : FetchResultURIs ,
249
+ storeResults ?: StoreResults ,
250
+ ) {
210
251
this . _fetcher = new KeyURIFetcher ( fetchResultURIs ) ;
252
+ if ( storeResults != null ) {
253
+ this . _store = new KeyResultStore ( storeResults ) ;
254
+ }
211
255
}
212
256
213
257
/**
@@ -263,6 +307,12 @@ class GlobalTransformCache {
263
307
} ) ;
264
308
}
265
309
310
+ store ( props : FetchProps , result : CachedResult ) {
311
+ if ( this . _store != null ) {
312
+ this . _store . store ( GlobalTransformCache . keyOf ( props ) , result ) ;
313
+ }
314
+ }
315
+
266
316
/**
267
317
* For using the global cache one needs to have some kind of central key-value
268
318
* store that gets prefilled using keyOf() and the transformed results. The
@@ -271,8 +321,14 @@ class GlobalTransformCache {
271
321
* of returning the content directly allows for independent fetching of each
272
322
* result.
273
323
*/
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
+ ) ;
276
332
}
277
333
278
334
static get ( ) {
0 commit comments