@@ -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+
2934type 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+
161198function validateCachedResult ( cachedResult : mixed ) : ?CachedResult {
162199 if (
163200 cachedResult != null &&
@@ -204,10 +241,17 @@ function globalizeTransformOptions(
204241class 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 ( ) {
0 commit comments