yet another attempt at fixing reporting sync changes to client

This commit is contained in:
azivner
2017-12-19 23:22:21 -05:00
parent f54d855f55
commit 333735543e
8 changed files with 54 additions and 45 deletions

View File

@@ -47,7 +47,7 @@ async function runChecks() {
await runSyncRowChecks("recent_notes", "note_tree_id", errorList);
if (errorList.length > 0) {
messaging.sendMessage({type: 'consistency-checks-failed'});
messaging.sendMessageToAllClients({type: 'consistency-checks-failed'});
}
else {
log.info("All consistency checks passed.");

View File

@@ -1,6 +1,9 @@
const WebSocket = require('ws');
const utils = require('./utils');
const log = require('./log');
const sql = require('./sql');
const options = require('./options');
const sync_setup = require('./sync_setup');
let webSocketServer;
@@ -29,6 +32,9 @@ function init(httpServer, sessionParser) {
if (message.type === 'log-error') {
log.error('JS Error: ' + message.error);
}
else if (message.type === 'ping') {
sendPing(ws, message.lastSyncId);
}
else {
log.error('Unrecognized message: ');
log.error(message);
@@ -37,7 +43,15 @@ function init(httpServer, sessionParser) {
});
}
async function sendMessage(message) {
async function sendMessage(client, message) {
const jsonStr = JSON.stringify(message);
if (client.readyState === WebSocket.OPEN) {
client.send(jsonStr);
}
}
async function sendMessageToAllClients(message) {
const jsonStr = JSON.stringify(message);
webSocketServer.clients.forEach(function each(client) {
@@ -47,7 +61,21 @@ async function sendMessage(message) {
});
}
async function sendPing(client, lastSentSyncId) {
const syncData = await sql.getResults("SELECT * FROM sync WHERE id > ?", [lastSentSyncId]);
const lastSyncedPush = await options.getOption('last_synced_push');
const changesToPushCount = await sql.getSingleValue("SELECT COUNT(*) FROM sync WHERE id > ?", [lastSyncedPush]);
await sendMessage(client, {
type: 'sync',
data: syncData,
changesToPushCount: sync_setup.isSyncSetup ? changesToPushCount : 0
});
}
module.exports = {
init,
sendMessage
sendMessageToAllClients
};

View File

@@ -1,30 +0,0 @@
const sql = require('./sql');
const messaging = require('./messaging');
const options = require('./options');
const sync_setup = require('./sync_setup');
let lastSentSyncId;
async function sendPing() {
const syncData = await sql.getResults("SELECT * FROM sync WHERE id > ?", [lastSentSyncId]);
const lastSyncedPush = await options.getOption('last_synced_push');
const changesToPushCount = await sql.getSingleValue("SELECT COUNT(*) FROM sync WHERE id > ?", [lastSyncedPush]);
messaging.sendMessage({
type: 'sync',
data: syncData,
changesToPushCount: sync_setup.isSyncSetup ? changesToPushCount : 0
});
if (syncData.length > 0) {
lastSentSyncId = syncData[syncData.length - 1].id;
}
}
sql.dbReady.then(async () => {
lastSentSyncId = await sql.getSingleValue("SELECT MAX(id) FROM sync");
setInterval(sendPing, 1000);
});

View File

@@ -267,7 +267,7 @@ async function checkContentHash(syncContext) {
if (key !== 'recent_notes') {
// let's not get alarmed about recent notes which get updated often and can cause failures in race conditions
await messaging.sendMessage({type: 'sync-hash-check-failed'});
await messaging.sendMessageToAllClients({type: 'sync-hash-check-failed'});
}
}
}