From eb4dfbad9268b25f7e1a289d8d91bfe9f7557716 Mon Sep 17 00:00:00 2001 From: zadam Date: Mon, 16 Dec 2019 22:47:07 +0100 Subject: [PATCH] sync fixes --- .../javascripts/services/tree_builder.js | 2 +- src/public/javascripts/services/ws.js | 24 ++++++++++++------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/public/javascripts/services/tree_builder.js b/src/public/javascripts/services/tree_builder.js index 70522af7c..eda30b55a 100644 --- a/src/public/javascripts/services/tree_builder.js +++ b/src/public/javascripts/services/tree_builder.js @@ -67,7 +67,7 @@ async function prepareNode(branch) { const note = await branch.getNote(); if (!note) { - console.log(`Branch has no note: ${branch}`); + throw new Error(`Branch has no note ` + branch.noteId); } const title = (branch.prefix ? (branch.prefix + " - ") : "") + note.title; diff --git a/src/public/javascripts/services/ws.js b/src/public/javascripts/services/ws.js index b6bb40882..918ab8aee 100644 --- a/src/public/javascripts/services/ws.js +++ b/src/public/javascripts/services/ws.js @@ -59,6 +59,10 @@ async function handleMessage(event) { syncDataQueue.push(...syncRows); + // we set lastAcceptedSyncId even before sync processing and send ping so that backend can start sending more updates + lastAcceptedSyncId = Math.max(lastAcceptedSyncId, syncRows[syncRows.length - 1].id); + sendPing(); + // first wait for all the preceding consumers to finish while (consumeQueuePromise) { await consumeQueuePromise; @@ -132,15 +136,19 @@ async function consumeSyncData() { const outsideSyncData = allSyncData.filter(sync => sync.sourceId !== glob.sourceId); - // we set lastAcceptedSyncId even before sync processing and send ping so that backend can start sending more updates - lastAcceptedSyncId = Math.max(lastAcceptedSyncId, allSyncData[allSyncData.length - 1].id); - sendPing(); + try { + // the update process should be synchronous as a whole but individual handlers can run in parallel + await Promise.all([ + ...allSyncMessageHandlers.map(syncHandler => runSafely(syncHandler, allSyncData)), + ...outsideSyncMessageHandlers.map(syncHandler => runSafely(syncHandler, outsideSyncData)) + ]); + } + catch (e) { + logError(`Encountered error ${e.message}, reloading frontend.`); - // the update process should be synchronous as a whole but individual handlers can run in parallel - await Promise.all([ - ...allSyncMessageHandlers.map(syncHandler => runSafely(syncHandler, allSyncData)), - ...outsideSyncMessageHandlers.map(syncHandler => runSafely(syncHandler, outsideSyncData)) - ]); + // if there's an error in updating the frontend then the easy option to recover is to reload the frontend completely + utils.reloadApp(); + } lastProcessedSyncId = Math.max(lastProcessedSyncId, allSyncData[allSyncData.length - 1].id); }