@@ -286,6 +286,12 @@ export class JsonRpcProvider extends BaseProvider {
286
286
// all be the same, so we can dedup the calls to save requests and
287
287
// bandwidth. @TODO : Try out generalizing this against send?
288
288
_eventLoopCache : Record < string , Promise < any > > ;
289
+ get _cache ( ) : Record < string , Promise < any > > {
290
+ if ( this . _eventLoopCache == null ) {
291
+ this . _eventLoopCache = { } ;
292
+ }
293
+ return this . _eventLoopCache ;
294
+ }
289
295
290
296
constructor ( url ?: ConnectionInfo | string , network ?: Networkish ) {
291
297
logger . checkNew ( new . target , JsonRpcProvider ) ;
@@ -307,8 +313,6 @@ export class JsonRpcProvider extends BaseProvider {
307
313
308
314
super ( networkOrReady ) ;
309
315
310
- this . _eventLoopCache = { } ;
311
-
312
316
// Default URL
313
317
if ( ! url ) { url = getStatic < ( ) => string > ( this . constructor , "defaultUrl" ) ( ) ; }
314
318
@@ -328,15 +332,15 @@ export class JsonRpcProvider extends BaseProvider {
328
332
}
329
333
330
334
detectNetwork ( ) : Promise < Network > {
331
- if ( ! this . _eventLoopCache [ "detectNetwork" ] ) {
332
- this . _eventLoopCache [ "detectNetwork" ] = this . _uncachedDetectNetwork ( ) ;
335
+ if ( ! this . _cache [ "detectNetwork" ] ) {
336
+ this . _cache [ "detectNetwork" ] = this . _uncachedDetectNetwork ( ) ;
333
337
334
338
// Clear this cache at the beginning of the next event loop
335
339
setTimeout ( ( ) => {
336
- this . _eventLoopCache [ "detectNetwork" ] = null ;
340
+ this . _cache [ "detectNetwork" ] = null ;
337
341
} , 0 ) ;
338
342
}
339
- return this . _eventLoopCache [ "detectNetwork" ] ;
343
+ return this . _cache [ "detectNetwork" ] ;
340
344
}
341
345
342
346
async _uncachedDetectNetwork ( ) : Promise < Network > {
@@ -400,8 +404,8 @@ export class JsonRpcProvider extends BaseProvider {
400
404
// We can expand this in the future to any call, but for now these
401
405
// are the biggest wins and do not require any serializing parameters.
402
406
const cache = ( [ "eth_chainId" , "eth_blockNumber" ] . indexOf ( method ) >= 0 ) ;
403
- if ( cache && this . _eventLoopCache [ method ] ) {
404
- return this . _eventLoopCache [ method ] ;
407
+ if ( cache && this . _cache [ method ] ) {
408
+ return this . _cache [ method ] ;
405
409
}
406
410
407
411
const result = fetchJson ( this . connection , JSON . stringify ( request ) , getResult ) . then ( ( result ) => {
@@ -427,9 +431,9 @@ export class JsonRpcProvider extends BaseProvider {
427
431
428
432
// Cache the fetch, but clear it on the next event loop
429
433
if ( cache ) {
430
- this . _eventLoopCache [ method ] = result ;
434
+ this . _cache [ method ] = result ;
431
435
setTimeout ( ( ) => {
432
- this . _eventLoopCache [ method ] = null ;
436
+ this . _cache [ method ] = null ;
433
437
} , 0 ) ;
434
438
}
435
439
0 commit comments