Move probe to WS class for reuse in the badge

This commit is contained in:
Arlo Breault 2019-09-26 11:38:58 -04:00
parent 685c3bd262
commit aa107862c5
2 changed files with 32 additions and 26 deletions

View file

@ -1,4 +1,4 @@
/* global Util, chrome, Config, UI, Broker, Snowflake */ /* global Util, chrome, Config, UI, Broker, Snowflake, WS */
/* eslint no-unused-vars: 0 */ /* eslint no-unused-vars: 0 */
/* /*
@ -31,20 +31,9 @@ class WebExtUI extends UI {
this.setEnabled(false); this.setEnabled(false);
return; return;
} }
(new Promise((resolve, reject) => { WS.probeWebsocket(config.relayAddr)
const ws = WS.makeWebsocket(config.relayAddr); .then(
ws.onopen = () => { () => {
resolve();
ws.close();
};
ws.onerror = () => {
this.missingFeature = 'popupBridgeUnreachable';
this.setEnabled(false);
reject('Could not connect to bridge.');
ws.close();
};
}))
.then(() => {
chrome.storage.local.get("snowflake-enabled", (result) => { chrome.storage.local.get("snowflake-enabled", (result) => {
let enabled = this.enabled; let enabled = this.enabled;
if (result['snowflake-enabled'] !== void 0) { if (result['snowflake-enabled'] !== void 0) {
@ -54,10 +43,13 @@ class WebExtUI extends UI {
} }
this.setEnabled(enabled); this.setEnabled(enabled);
}); });
}) },
.catch((e) => { () => {
log(e); log('Could not connect to bridge.');
}); this.missingFeature = 'popupBridgeUnreachable';
this.setEnabled(false);
}
);
} }
postActive() { postActive() {

View file

@ -54,6 +54,20 @@ class WS {
return 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; WS.WSS_ENABLED = true;