summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAndrej Mihajlov <and@mullvad.net>2018-06-11 13:55:25 +0200
committerAndrej Mihajlov <and@mullvad.net>2018-06-12 15:33:31 +0200
commit068ff217d75ec578aacb01fd441e23898919913b (patch)
treefaeece30342d7f2688da64f74a1e983b284f04fa
parentadf9d3420f25e1ab0fb24ce8adf73ffb6964ae11 (diff)
downloadmullvadvpn-068ff217d75ec578aacb01fd441e23898919913b.tar.xz
mullvadvpn-068ff217d75ec578aacb01fd441e23898919913b.zip
Migrate some of old IPC methods to use async+await
-rw-r--r--app/lib/jsonrpc-ws-ipc.js48
1 files changed, 21 insertions, 27 deletions
diff --git a/app/lib/jsonrpc-ws-ipc.js b/app/lib/jsonrpc-ws-ipc.js
index 43c6c5c06c..ff73f4cc88 100644
--- a/app/lib/jsonrpc-ws-ipc.js
+++ b/app/lib/jsonrpc-ws-ipc.js
@@ -96,26 +96,22 @@ export default class Ipc {
this._closeConnectionHandler = handler;
}
- on(event: string, listener: (mixed) => void): Promise<*> {
- log.debug('Adding a listener to', event);
- return this.send(event + '_subscribe')
- .then((subscriptionId) => {
- if (typeof subscriptionId === 'string' || typeof subscriptionId === 'number') {
- this._subscriptions.set(subscriptionId, listener);
- } else {
- throw new InvalidReply(
- subscriptionId,
- 'The subscription id was not a string or a number',
- );
- }
- })
- .catch((e) => {
- log.error('Failed adding listener to', event, ':', e);
- });
+ async on(event: string, listener: (mixed) => void): Promise<*> {
+ log.silly(`Adding a listener to ${event}`);
+ try {
+ const subscriptionId = await this.send(`${event}_subscribe`);
+ if (typeof subscriptionId === 'string' || typeof subscriptionId === 'number') {
+ this._subscriptions.set(subscriptionId, listener);
+ } else {
+ throw new InvalidReply(subscriptionId, 'The subscription id was not a string or a number');
+ }
+ } catch (e) {
+ log.error(`Failed adding listener to ${event}: ${e.message}`);
+ }
}
send(action: string, data: mixed, timeout: number = DEFAULT_TIMEOUT_MILLIS): Promise<mixed> {
- return new Promise((resolve, reject) => {
+ return new Promise(async (resolve, reject) => {
const id = uuid.v4();
const params = this._prepareParams(data);
@@ -128,15 +124,14 @@ export default class Ipc {
message: jsonrpcMessage,
});
- this._getWebSocket()
- .then((ws) => {
- log.debug('Sending message', id, action);
- ws.send(jsonrpcMessage);
- })
- .catch((e) => {
- log.error('Failed sending RPC message "' + action + '":', e);
- reject(e);
- });
+ try {
+ const ws = await this._getWebSocket();
+ log.silly('Sending message', id, action);
+ ws.send(jsonrpcMessage);
+ } catch (e) {
+ log.error(`Failed sending RPC message "${action}": ${e.message}`);
+ reject(e);
+ }
});
}
@@ -159,7 +154,6 @@ export default class Ipc {
_getWebSocket(): Promise<WebSocket> {
return new Promise((resolve) => {
if (this._websocket && this._websocket.readyState === 1) {
- // Connected
resolve(this._websocket);
} else {
log.debug('Waiting for websocket to connect');