Skip to content

Commit 58488e7

Browse files
committed
Fixed JsonRpcProvider event-loop caching when using any network (ethers-io#1484).
1 parent 2911659 commit 58488e7

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

packages/providers/src.ts/json-rpc-provider.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,12 @@ export class JsonRpcProvider extends BaseProvider {
286286
// all be the same, so we can dedup the calls to save requests and
287287
// bandwidth. @TODO: Try out generalizing this against send?
288288
_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+
}
289295

290296
constructor(url?: ConnectionInfo | string, network?: Networkish) {
291297
logger.checkNew(new.target, JsonRpcProvider);
@@ -307,8 +313,6 @@ export class JsonRpcProvider extends BaseProvider {
307313

308314
super(networkOrReady);
309315

310-
this._eventLoopCache = { };
311-
312316
// Default URL
313317
if (!url) { url = getStatic<() => string>(this.constructor, "defaultUrl")(); }
314318

@@ -328,15 +332,15 @@ export class JsonRpcProvider extends BaseProvider {
328332
}
329333

330334
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();
333337

334338
// Clear this cache at the beginning of the next event loop
335339
setTimeout(() => {
336-
this._eventLoopCache["detectNetwork"] = null;
340+
this._cache["detectNetwork"] = null;
337341
}, 0);
338342
}
339-
return this._eventLoopCache["detectNetwork"];
343+
return this._cache["detectNetwork"];
340344
}
341345

342346
async _uncachedDetectNetwork(): Promise<Network> {
@@ -400,8 +404,8 @@ export class JsonRpcProvider extends BaseProvider {
400404
// We can expand this in the future to any call, but for now these
401405
// are the biggest wins and do not require any serializing parameters.
402406
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];
405409
}
406410

407411
const result = fetchJson(this.connection, JSON.stringify(request), getResult).then((result) => {
@@ -427,9 +431,9 @@ export class JsonRpcProvider extends BaseProvider {
427431

428432
// Cache the fetch, but clear it on the next event loop
429433
if (cache) {
430-
this._eventLoopCache[method] = result;
434+
this._cache[method] = result;
431435
setTimeout(() => {
432-
this._eventLoopCache[method] = null;
436+
this._cache[method] = null;
433437
}, 0);
434438
}
435439

0 commit comments

Comments
 (0)