Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
small sync fixes
zadamaff9ce97eee4
diff --git a/src/public/javascripts/entities/branch.js b/src/public/javascripts/entities/branch.jsindex 396e1c2291..b06a9e523a 100644--- a/src/public/javascripts/entities/branch.js+++ b/src/public/javascripts/entities/branch.js@@ -6,7 +6,6 @@ class Branch {this.branchId = row.branchId;/** @param {string} */this.noteId = row.noteId;- this.note = null;/** @param {string} */this.parentNoteId = row.parentNoteId;/** @param {int} */diff --git a/src/public/javascripts/services/tree_cache.js b/src/public/javascripts/services/tree_cache.jsindex ff8a910c29..9d6257cf87 100644--- a/src/public/javascripts/services/tree_cache.js+++ b/src/public/javascripts/services/tree_cache.js@@ -133,7 +133,7 @@ class TreeCache {/** @return {Promise<NoteShort[]>} */async getNotes(noteIds, silentNotFoundError = false) {- const missingNoteIds = noteIds.filter(noteId => this.notes[noteId] === undefined);+ const missingNoteIds = noteIds.filter(noteId => !this.notes[noteId]);if (missingNoteIds.length > 0) {await this.reloadNotes(missingNoteIds);diff --git a/src/public/javascripts/services/ws.js b/src/public/javascripts/services/ws.jsindex 463c1813cf..b6bb40882f 100644--- a/src/public/javascripts/services/ws.js+++ b/src/public/javascripts/services/ws.js@@ -64,13 +64,16 @@ async function handleMessage(event) {await consumeQueuePromise;}- // it's my turn so start it up- consumeQueuePromise = consumeSyncData();+ try {+ // it's my turn so start it up+ consumeQueuePromise = consumeSyncData();- await consumeQueuePromise;-- // finish and set to null to signal somebody else can pick it up- consumeQueuePromise = null;+ await consumeQueuePromise;+ }+ finally {+ // finish and set to null to signal somebody else can pick it up+ consumeQueuePromise = null;+ }}}else if (message.type === 'sync-hash-check-failed') {@@ -113,6 +116,15 @@ function checkSyncIdListeners() {.forEach(l => console.log(`Waiting for syncId ${l.desiredSyncId} while current is ${lastProcessedSyncId} for ${Math.floor((Date.now() - l.start) / 1000)}s`));}+async function runSafely(syncHandler, syncData) {+ try {+ return await syncHandler(syncData);+ }+ catch (e) {+ console.log(`Sync handler failed with ${e.message}: ${e.stack}`);+ }+}+async function consumeSyncData() {if (syncDataQueue.length > 0) {const allSyncData = syncDataQueue;@@ -126,8 +138,8 @@ async function consumeSyncData() {// the update process should be synchronous as a whole but individual handlers can run in parallelawait Promise.all([- ...allSyncMessageHandlers.map(syncHandler => syncHandler(allSyncData)),- ...outsideSyncMessageHandlers.map(syncHandler => syncHandler(outsideSyncData))+ ...allSyncMessageHandlers.map(syncHandler => runSafely(syncHandler, allSyncData)),+ ...outsideSyncMessageHandlers.map(syncHandler => runSafely(syncHandler, outsideSyncData))]);lastProcessedSyncId = Math.max(lastProcessedSyncId, allSyncData[allSyncData.length - 1].id);@@ -171,8 +183,6 @@ async function sendPing() {setTimeout(() => {ws = connectWebSocket();- lastAcceptedSyncId = glob.maxSyncIdAtLoad;- lastProcessedSyncId = glob.maxSyncIdAtLoad;lastPingTs = Date.now();setInterval(sendPing, 1000);diff --git a/src/services/notes.js b/src/services/notes.jsindex 26a743b3f3..d6a7052cf2 100644--- a/src/services/notes.js+++ b/src/services/notes.js@@ -3,7 +3,6 @@ const sqlInit = require('./sql_init');const optionService = require('./options');const dateUtils = require('./date_utils');const syncTableService = require('./sync_table');-const attributeService = require('./attributes');const eventService = require('./events');const repository = require('./repository');const cls = require('../services/cls');@@ -462,8 +461,11 @@ async function eraseDeletedNotes() {return;}- // it's better to not use repository for this because it will complain about saving protected notes- // out of protected session, also we don't want these changes to be synced (since they are done on all instances anyway)+ // it's better to not use repository for this because:+ // - it would complain about saving protected notes out of protected session+ // - we don't want these changes to be synced (since they are done on all instances anyway)+ // - we don't want change the hash since this erasing happens on each instance separately+ // and changing the hash would fire up the sync errors temporarily// setting contentLength to zero would serve no benefit and it leaves potentially useful trailawait sql.executeMany(`