From cb31e0acf231ab699288b774a11b9e91128e5fe1 Mon Sep 17 00:00:00 2001 From: azivner Date: Sat, 25 Nov 2017 18:31:38 -0500 Subject: [PATCH] broadcast messages to all clients, send also number of outstanding syncs --- public/javascripts/init.js | 3 +++ public/javascripts/status.js | 4 ++-- services/sync_table.js | 20 +++++++++++--------- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/public/javascripts/init.js b/public/javascripts/init.js index 60df6451e..ceea8c2af 100644 --- a/public/javascripts/init.js +++ b/public/javascripts/init.js @@ -165,5 +165,8 @@ ws.onmessage = function (event) { noteEditor.reload(); } + + const changesToPushCountEl = $("#changesToPushCount"); + changesToPushCountEl.html(message.changesToPushCount); } }; \ No newline at end of file diff --git a/public/javascripts/status.js b/public/javascripts/status.js index fcd3a92d6..4ca558a8c 100644 --- a/public/javascripts/status.js +++ b/public/javascripts/status.js @@ -38,10 +38,10 @@ const status = (function() { // noteEditor.reload(); // } - changesToPushCountEl.html(resp.changesToPushCount); + //changesToPushCountEl.html(resp.changesToPushCount); } - setInterval(checkStatus, 5 * 1000); + //setInterval(checkStatus, 5 * 1000); return { checkStatus diff --git a/services/sync_table.js b/services/sync_table.js index 42a6cf6ae..c1cc0a476 100644 --- a/services/sync_table.js +++ b/services/sync_table.js @@ -2,6 +2,7 @@ const sql = require('./sql'); const source_id = require('./source_id'); const utils = require('./utils'); const messaging = require('./messaging'); +const options = require('./options'); async function addNoteSync(noteId, sourceId) { await addEntitySync("notes", noteId, sourceId) @@ -40,7 +41,7 @@ let startTime = utils.nowTimestamp(); let sentSyncId = []; setInterval(async () => { - const syncs = await sql.getResults("SELECT * FROM sync WHERE sync_date >= ?", [startTime]); + const syncs = await sql.getResults("SELECT * FROM sync WHERE sync_date >= ? AND source_id != ?", [startTime, source_id.currentSourceId]); startTime = utils.nowTimestamp(); const data = {}; @@ -59,15 +60,16 @@ setInterval(async () => { syncIds.push(sync.id); } - if (syncIds.length > 0) { - messaging.send({ - type: 'sync', - data: data - }); + const lastSyncedPush = await sql.getSingleValue("SELECT opt_value FROM options WHERE opt_name = 'last_synced_push'"); - for (const syncId of syncIds) { - sentSyncId.push(syncId); - } + messaging.send({ + type: 'sync', + data: data, + changesToPushCount: await sql.getSingleValue("SELECT COUNT(*) FROM sync WHERE id > ?", [lastSyncedPush]) + }); + + for (const syncId of syncIds) { + sentSyncId.push(syncId); } }, 1000);