UNPKG

@coinbase/wallet-sdk

Version:
125 lines 4.3 kB
// Copyright (c) 2018-2023 Coinbase, Inc. <https://www.coinbase.com/> export var ConnectionState; (function (ConnectionState) { ConnectionState[ConnectionState["DISCONNECTED"] = 0] = "DISCONNECTED"; ConnectionState[ConnectionState["CONNECTING"] = 1] = "CONNECTING"; ConnectionState[ConnectionState["CONNECTED"] = 2] = "CONNECTED"; })(ConnectionState || (ConnectionState = {})); export class WalletLinkWebSocket { setConnectionStateListener(listener) { this.connectionStateListener = listener; } setIncomingDataListener(listener) { this.incomingDataListener = listener; } /** * Constructor * @param url WebSocket server URL * @param [WebSocketClass] Custom WebSocket implementation */ constructor(url, WebSocketClass = WebSocket) { this.WebSocketClass = WebSocketClass; this.webSocket = null; this.pendingData = []; this.url = url.replace(/^http/, 'ws'); } /** * Make a websocket connection * @returns a Promise that resolves when connected */ async connect() { if (this.webSocket) { throw new Error('webSocket object is not null'); } return new Promise((resolve, reject) => { var _a; let webSocket; try { this.webSocket = webSocket = new this.WebSocketClass(this.url); } catch (err) { reject(err); return; } (_a = this.connectionStateListener) === null || _a === void 0 ? void 0 : _a.call(this, ConnectionState.CONNECTING); webSocket.onclose = (evt) => { var _a; this.clearWebSocket(); reject(new Error(`websocket error ${evt.code}: ${evt.reason}`)); (_a = this.connectionStateListener) === null || _a === void 0 ? void 0 : _a.call(this, ConnectionState.DISCONNECTED); }; webSocket.onopen = (_) => { var _a; resolve(); (_a = this.connectionStateListener) === null || _a === void 0 ? void 0 : _a.call(this, ConnectionState.CONNECTED); if (this.pendingData.length > 0) { const pending = [...this.pendingData]; pending.forEach((data) => this.sendData(data)); this.pendingData = []; } }; webSocket.onmessage = (evt) => { var _a, _b; if (evt.data === 'h') { (_a = this.incomingDataListener) === null || _a === void 0 ? void 0 : _a.call(this, { type: 'Heartbeat', }); } else { try { const message = JSON.parse(evt.data); (_b = this.incomingDataListener) === null || _b === void 0 ? void 0 : _b.call(this, message); } catch (_c) { /* empty */ } } }; }); } /** * Disconnect from server */ disconnect() { var _a; const { webSocket } = this; if (!webSocket) { return; } this.clearWebSocket(); (_a = this.connectionStateListener) === null || _a === void 0 ? void 0 : _a.call(this, ConnectionState.DISCONNECTED); this.connectionStateListener = undefined; this.incomingDataListener = undefined; try { webSocket.close(); } catch (_b) { // noop } } /** * Send data to server * @param data text to send */ sendData(data) { const { webSocket } = this; if (!webSocket) { this.pendingData.push(data); this.connect(); return; } webSocket.send(data); } clearWebSocket() { const { webSocket } = this; if (!webSocket) { return; } this.webSocket = null; webSocket.onclose = null; webSocket.onerror = null; webSocket.onmessage = null; webSocket.onopen = null; } } //# sourceMappingURL=WalletLinkWebSocket.js.map