Skip to content

Commit

Permalink
Added destroy to WebSocketProvider.
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Jul 12, 2020
1 parent c660176 commit d0a79c6
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/providers/src.ts/websocket-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit d0a79c6

Please sign in to comment.