making WS connection asynchronous to not block module registration

This commit is contained in:
azivner 2018-03-25 13:08:58 -04:00
parent fddd1c278f
commit 299252b650

View file

@ -3,9 +3,14 @@
import treeService from './tree_service.js';
import noteDetailService from './note_detail.js';
import utils from './utils.js';
import recentNotes from '../dialogs/recent_notes.js';
const $changesToPushCount = $("#changes-to-push-count");
let ws;
let lastSyncId;
let lastPingTs;
function logError(message) {
console.log(utils.now(), message); // needs to be separate from .trace()
console.trace();
@ -80,36 +85,38 @@ function connectWebSocket() {
return ws;
}
const ws = connectWebSocket();
setTimeout(() => {
ws = connectWebSocket();
let lastSyncId = glob.maxSyncIdAtLoad;
let lastPingTs = new Date().getTime();
let connectionBrokenNotification = null;
lastSyncId = glob.maxSyncIdAtLoad;
lastPingTs = new Date().getTime();
let connectionBrokenNotification = null;
setInterval(async () => {
if (new Date().getTime() - lastPingTs > 30000) {
if (!connectionBrokenNotification) {
connectionBrokenNotification = $.notify({
// options
message: "Lost connection to server"
},{
// settings
type: 'danger',
delay: 100000000 // keep it until we explicitly close it
});
setInterval(async () => {
if (new Date().getTime() - lastPingTs > 30000) {
if (!connectionBrokenNotification) {
connectionBrokenNotification = $.notify({
// options
message: "Lost connection to server"
},{
// settings
type: 'danger',
delay: 100000000 // keep it until we explicitly close it
});
}
}
}
else if (connectionBrokenNotification) {
await connectionBrokenNotification.close();
connectionBrokenNotification = null;
else if (connectionBrokenNotification) {
await connectionBrokenNotification.close();
connectionBrokenNotification = null;
utils.showMessage("Re-connected to server");
}
utils.showMessage("Re-connected to server");
}
ws.send(JSON.stringify({
type: 'ping',
lastSyncId: lastSyncId
}));
ws.send(JSON.stringify({
type: 'ping',
lastSyncId: lastSyncId
}));
}, 1000);
}, 1000);
export default {