Added destroy to WebSocketProvider.

This commit is contained in:
Richard Moore 2020-07-12 05:02:08 -04:00
parent c6601769ad
commit d0a79c6a13
No known key found for this signature in database
GPG Key ID: 665176BE8E9DC651

@ -274,4 +274,22 @@ export class WebSocketProvider extends JsonRpcProvider {
this.send("eth_unsubscribe", [ subId ]);
});
}
async destroy(): Promise<void> {
// Wait until we have connected before trying to disconnect
if (this._websocket.readyState === WebSocket.CONNECTING) {
await new Promise((resolve) => {
this._websocket.on("open", () => {
resolve(true);
});
this._websocket.on("error", () => {
resolve(false);
});
});
}
// Hangup (navigating away from the page that opened the connection)
this._websocket.close(1001);
}
}