commit aa107862c5300ce52842f92196a3f1d1527b48b4 Author: Arlo Breault arlolra@gmail.com Date: Thu Sep 26 11:38:58 2019 -0400
Move probe to WS class for reuse in the badge --- proxy/init-webext.js | 44 ++++++++++++++++++-------------------------- proxy/websocket.js | 14 ++++++++++++++ 2 files changed, 32 insertions(+), 26 deletions(-)
diff --git a/proxy/init-webext.js b/proxy/init-webext.js index dff1208..f6c0d0f 100644 --- a/proxy/init-webext.js +++ b/proxy/init-webext.js @@ -1,4 +1,4 @@ -/* global Util, chrome, Config, UI, Broker, Snowflake */ +/* global Util, chrome, Config, UI, Broker, Snowflake, WS */ /* eslint no-unused-vars: 0 */
/* @@ -31,33 +31,25 @@ class WebExtUI extends UI { this.setEnabled(false); return; } - (new Promise((resolve, reject) => { - const ws = WS.makeWebsocket(config.relayAddr); - ws.onopen = () => { - resolve(); - ws.close(); - }; - ws.onerror = () => { + WS.probeWebsocket(config.relayAddr) + .then( + () => { + chrome.storage.local.get("snowflake-enabled", (result) => { + let enabled = this.enabled; + if (result['snowflake-enabled'] !== void 0) { + enabled = result['snowflake-enabled']; + } else { + log("Toggle state not yet saved"); + } + this.setEnabled(enabled); + }); + }, + () => { + log('Could not connect to bridge.'); this.missingFeature = 'popupBridgeUnreachable'; this.setEnabled(false); - reject('Could not connect to bridge.'); - ws.close(); - }; - })) - .then(() => { - chrome.storage.local.get("snowflake-enabled", (result) => { - let enabled = this.enabled; - if (result['snowflake-enabled'] !== void 0) { - enabled = result['snowflake-enabled']; - } else { - log("Toggle state not yet saved"); - } - this.setEnabled(enabled); - }); - }) - .catch((e) => { - log(e); - }); + } + ); }
postActive() { diff --git a/proxy/websocket.js b/proxy/websocket.js index 410fdc0..da7ba94 100644 --- a/proxy/websocket.js +++ b/proxy/websocket.js @@ -54,6 +54,20 @@ class WS { return ws; }
+ static probeWebsocket(addr) { + return new Promise((resolve, reject) => { + const ws = WS.makeWebsocket(addr); + ws.onopen = () => { + resolve(); + ws.close(); + }; + ws.onerror = () => { + reject(); + ws.close(); + }; + }); + } + }
WS.WSS_ENABLED = true;
tor-commits@lists.torproject.org