diff --git a/src/services/autocomplete.js b/src/services/autocomplete.js index 43f0820ca..e815d5eb3 100644 --- a/src/services/autocomplete.js +++ b/src/services/autocomplete.js @@ -1,5 +1,7 @@ const sql = require('./sql'); const sqlInit = require('./sql_init'); +const syncTableService = require('./sync_table'); +const repository = require('./repository'); let noteTitles; let noteIds; @@ -114,6 +116,20 @@ function getNoteTitle(path) { return titles.join(' / '); } +syncTableService.addListener(async (entityName, entityId) => { + if (entityName === 'notes') { + const note = await repository.getNote(entityId); + + if (note.isDeleted) { + delete noteTitles[note.noteId]; + delete childToParent[note.noteId]; + } + else { + noteTitles[note.noteId] = note.title; + } + } +}); + sqlInit.dbReady.then(load); module.exports = { diff --git a/src/services/sync_table.js b/src/services/sync_table.js index 7ba8f5212..e2c70ccf0 100644 --- a/src/services/sync_table.js +++ b/src/services/sync_table.js @@ -5,6 +5,12 @@ const syncSetup = require('./sync_setup'); const log = require('./log'); const cls = require('./cls'); +const listeners = []; + +function addListener(listener) { + listeners.push(listener); +} + async function addNoteSync(noteId, sourceId) { await addEntitySync("notes", noteId, sourceId) } @@ -58,6 +64,11 @@ async function addEntitySync(entityName, entityId, sourceId) { // useful when you fork the DB for new "client" instance, it won't try to sync the whole DB await sql.execute("UPDATE options SET value = (SELECT MAX(id) FROM sync) WHERE name IN('lastSyncedPush', 'lastSyncedPull')"); } + + for (const listener of listeners) { + // not awaiting to not block the execution + listener(entityName, entityId); + } } async function cleanupSyncRowsForMissingEntities(entityName, entityKey) { @@ -104,6 +115,7 @@ async function fillAllSyncRows() { } module.exports = { + addListener, addNoteSync, addBranchSync, addNoteReorderingSync,