From f9cfd134b717b25a98e3db52764423d79c99b65f Mon Sep 17 00:00:00 2001 From: zadam Date: Thu, 22 Jul 2021 20:19:44 +0200 Subject: [PATCH] fixed corner cases in becca updates from sync --- src/becca/becca_loader.js | 28 +++++++++++++++++++-------- src/becca/entities/abstract_entity.js | 2 +- src/services/sync_update.js | 4 ++-- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/becca/becca_loader.js b/src/becca/becca_loader.js index 7b8af0eb7..3a3e7b6c5 100644 --- a/src/becca/becca_loader.js +++ b/src/becca/becca_loader.js @@ -60,25 +60,31 @@ eventService.subscribe([eventService.ENTITY_CHANGED, eventService.ENTITY_CHANGE_ } }); -eventService.subscribe([eventService.ENTITY_DELETED, eventService.ENTITY_DELETE_SYNCED], ({entityName, entity}) => { +eventService.subscribe([eventService.ENTITY_DELETED, eventService.ENTITY_DELETE_SYNCED], ({entityName, entityId}) => { if (!becca.loaded) { return; } if (entityName === 'notes') { - noteDeleted(entity); + noteDeleted(entityId); } else if (entityName === 'branches') { - branchDeleted(entity); + branchDeleted(entityId); } else if (entityName === 'attributes') { - attributeDeleted(entity); + attributeDeleted(entityId); } }); -function noteDeleted(note) { - delete becca.notes[note.noteId]; +function noteDeleted(noteId) { + delete becca.notes[noteId]; } -function branchDeleted(branch) { +function branchDeleted(branchId) { + const branch = becca.branches[branchId]; + + if (!branch) { + return; + } + const childNote = becca.notes[branch.noteId]; if (childNote) { @@ -110,7 +116,13 @@ function branchUpdated(branch) { } } -function attributeDeleted(attribute) { +function attributeDeleted(attributeId) { + const attribute = becca.attributes[attributeId]; + + if (!attribute) { + return; + } + const note = becca.notes[attribute.noteId]; if (note) { diff --git a/src/becca/entities/abstract_entity.js b/src/becca/entities/abstract_entity.js index 93631eac3..7d9cc7662 100644 --- a/src/becca/entities/abstract_entity.js +++ b/src/becca/entities/abstract_entity.js @@ -110,7 +110,7 @@ class AbstractEntity { this.addEntityChange(true); - eventService.emit(eventService.ENTITY_DELETED, { entityName, entity: this }); + eventService.emit(eventService.ENTITY_DELETED, { entityName, entityId, entity: this }); } } diff --git a/src/services/sync_update.js b/src/services/sync_update.js index 2493a64c4..6c48ab913 100644 --- a/src/services/sync_update.js +++ b/src/services/sync_update.js @@ -26,14 +26,14 @@ function updateEntity(entityChange, entity) { ? updateNoteReordering(entityChange, entity) : updateNormalEntity(entityChange, entity); - if (updated && !entityChange.isErased) { + if (updated) { if (entity.isDeleted) { eventService.emit(eventService.ENTITY_DELETE_SYNCED, { entityName: entityChange.entityName, entityId: entityChange.entityId }); } - else { + else if (!entityChange.isErased) { eventService.emit(eventService.ENTITY_CHANGE_SYNCED, { entityName: entityChange.entityName, entity