updating the note cache after sync

This commit is contained in:
azivner 2019-01-03 23:27:10 +01:00
parent e2dfe1b6de
commit c3913a8735
4 changed files with 31 additions and 5 deletions

View File

@ -12,6 +12,7 @@ const Attribute = require('../entities/attribute');
const NoteRevision = require('../entities/note_revision'); const NoteRevision = require('../entities/note_revision');
const RecentNote = require('../entities/recent_note'); const RecentNote = require('../entities/recent_note');
const Option = require('../entities/option'); const Option = require('../entities/option');
const Link = require('../entities/link');
async function getHash(entityConstructor, whereBranch) { async function getHash(entityConstructor, whereBranch) {
// subselect is necessary to have correct ordering in GROUP_CONCAT // subselect is necessary to have correct ordering in GROUP_CONCAT
@ -37,7 +38,8 @@ async function getHashes() {
recent_notes: await getHash(RecentNote), recent_notes: await getHash(RecentNote),
options: await getHash(Option, "isSynced = 1"), options: await getHash(Option, "isSynced = 1"),
attributes: await getHash(Attribute), attributes: await getHash(Attribute),
api_tokens: await getHash(ApiToken) api_tokens: await getHash(ApiToken),
links: await getHash(Link)
}; };
const elapseTimeMs = new Date().getTime() - startTime.getTime(); const elapseTimeMs = new Date().getTime() - startTime.getTime();

View File

@ -5,14 +5,25 @@ const ENTER_PROTECTED_SESSION = "ENTER_PROTECTED_SESSION";
const ENTITY_CREATED = "ENTITY_CREATED"; const ENTITY_CREATED = "ENTITY_CREATED";
const ENTITY_CHANGED = "ENTITY_CHANGED"; const ENTITY_CHANGED = "ENTITY_CHANGED";
const ENTITY_DELETED = "ENTITY_DELETED"; const ENTITY_DELETED = "ENTITY_DELETED";
const ENTITY_SYNCED = "ENTITY_SYNCED";
const CHILD_NOTE_CREATED = "CHILD_NOTE_CREATED"; const CHILD_NOTE_CREATED = "CHILD_NOTE_CREATED";
const eventListeners = {}; const eventListeners = {};
function subscribe(eventType, listener) { /**
* @param eventTypes - can be either single event or an array of events
* @param listener
*/
function subscribe(eventTypes, listener) {
if (!Array.isArray(eventTypes)) {
eventTypes = [ eventTypes ];
}
for (const eventType of eventTypes) {
eventListeners[eventType] = eventListeners[eventType] || []; eventListeners[eventType] = eventListeners[eventType] || [];
eventListeners[eventType].push(listener); eventListeners[eventType].push(listener);
} }
}
async function emit(eventType, data) { async function emit(eventType, data) {
const listeners = eventListeners[eventType]; const listeners = eventListeners[eventType];
@ -39,5 +50,6 @@ module.exports = {
ENTITY_CREATED, ENTITY_CREATED,
ENTITY_CHANGED, ENTITY_CHANGED,
ENTITY_DELETED, ENTITY_DELETED,
ENTITY_SYNCED,
CHILD_NOTE_CREATED CHILD_NOTE_CREATED
}; };

View File

@ -299,7 +299,9 @@ function getNotePath(noteId) {
} }
} }
eventService.subscribe(eventService.ENTITY_CHANGED, async ({entityName, entity}) => { eventService.subscribe([eventService.ENTITY_CHANGED, eventService.ENTITY_DELETED, eventService.ENTITY_SYNCED], async ({entityName, entity}) => {
// note that entity can also be just POJO without methods if coming from sync
if (!loaded) { if (!loaded) {
return; return;
} }

View File

@ -2,6 +2,7 @@ const sql = require('./sql');
const log = require('./log'); const log = require('./log');
const eventLogService = require('./event_log'); const eventLogService = require('./event_log');
const syncTableService = require('./sync_table'); const syncTableService = require('./sync_table');
const eventService = require('./events');
async function updateEntity(sync, entity, sourceId) { async function updateEntity(sync, entity, sourceId) {
const {entityName} = sync; const {entityName} = sync;
@ -36,6 +37,15 @@ async function updateEntity(sync, entity, sourceId) {
else { else {
throw new Error(`Unrecognized entity type ${entityName}`); throw new Error(`Unrecognized entity type ${entityName}`);
} }
// currently making exception for protected notes and note revisions because here
// the title and content are not available decrypted as listeners would expect
if ((entityName !== 'notes' && entityName !== 'note_revisions') || !entity.isProtected) {
await eventService.emit(eventService.ENTITY_SYNCED, {
entityName,
entity
});
}
} }
function deserializeNoteContentBuffer(note) { function deserializeNoteContentBuffer(note) {