Skip to content

Commit

Permalink
Cache chain ID for WebSocketProvider (#1054).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Oct 3, 2020
1 parent e86f83b commit 40264ff
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/providers/src.ts/websocket-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import WebSocket from "ws";

import { BigNumber } from "@ethersproject/bignumber";
import { Networkish } from "@ethersproject/networks";
import { Network, Networkish } from "@ethersproject/networks";
import { defineReadOnly } from "@ethersproject/properties";

import { Event } from "./base-provider";
Expand Down Expand Up @@ -47,6 +47,7 @@ export type Subscription = {
export class WebSocketProvider extends JsonRpcProvider {
readonly _websocket: any;
readonly _requests: { [ name: string ]: InflightRequest };
readonly _detectNetwork: Promise<Network>;

// Maps event tag to subscription ID (we dedupe identical events)
readonly _subIds: { [ tag: string ]: Promise<string> };
Expand All @@ -67,13 +68,15 @@ export class WebSocketProvider extends JsonRpcProvider {
super(url, network);
this._pollingInterval = -1;

this._wsReady = false;

defineReadOnly(this, "_websocket", new WebSocket(this.connection.url));
defineReadOnly(this, "_requests", { });
defineReadOnly(this, "_subs", { });
defineReadOnly(this, "_subIds", { });
defineReadOnly(this, "_detectNetwork", super.detectNetwork());

// Stall sending requests until the socket is open...
this._wsReady = false;
this._websocket.onopen = () => {
this._wsReady = true;
Object.keys(this._requests).forEach((id) => {
Expand Down Expand Up @@ -125,6 +128,10 @@ export class WebSocketProvider extends JsonRpcProvider {
if (fauxPoll.unref) { fauxPoll.unref(); }
}

detectNetwork(): Promise<Network> {
return this._detectNetwork;
}

get pollingInterval(): number {
return 0;
}
Expand Down

0 comments on commit 40264ff

Please sign in to comment.