diff --git a/src/routes/api/tree.js b/src/routes/api/tree.js index fc795578c..0897266b3 100644 --- a/src/routes/api/tree.js +++ b/src/routes/api/tree.js @@ -31,7 +31,7 @@ function getNotesAndBranchesAndAttributes(noteIds) { for (const attr of note.ownedAttributes) { collectedAttributeIds.add(attr.attributeId); - if (attr.type === 'relation' && attr.name === 'template') { + if (attr.type === 'relation' && attr.name === 'template' && attr.targetNote) { collectEntityIds(attr.targetNote); } } diff --git a/src/services/note_cache/entities/note.js b/src/services/note_cache/entities/note.js index 3adef3148..855b0474e 100644 --- a/src/services/note_cache/entities/note.js +++ b/src/services/note_cache/entities/note.js @@ -234,11 +234,19 @@ class Note { this.ancestorCache = null; } - invalidateSubtreeCaches() { + invalidateSubtreeCaches(path = []) { + if (path.includes(this.noteId)) { + return; + } + this.invalidateThisCache(); + if (this.children.length || this.targetRelations.length) { + path = [...path, this.noteId]; + } + for (const childNote of this.children) { - childNote.invalidateSubtreeCaches(); + childNote.invalidateSubtreeCaches(path); } for (const targetRelation of this.targetRelations) { @@ -246,7 +254,7 @@ class Note { const note = targetRelation.note; if (note) { - note.invalidateSubtreeCaches(); + note.invalidateSubtreeCaches(path); } } } diff --git a/src/services/note_cache/note_cache_loader.js b/src/services/note_cache/note_cache_loader.js index a022e6c6e..63a3de371 100644 --- a/src/services/note_cache/note_cache_loader.js +++ b/src/services/note_cache/note_cache_loader.js @@ -106,6 +106,8 @@ eventService.subscribe([eventService.ENTITY_CHANGED, eventService.ENTITY_DELETED // first invalidate and only then remove the attribute (otherwise invalidation wouldn't be complete) if (attr.isAffectingSubtree || note.isTemplate) { note.invalidateSubtreeCaches(); + } else { + note.invalidateThisCache(); } note.ownedAttributes = note.ownedAttributes.filter(attr => attr.attributeId !== attributeId); @@ -137,7 +139,7 @@ eventService.subscribe([eventService.ENTITY_CHANGED, eventService.ENTITY_DELETED note.invalidateSubtreeFlatText(); } else { - note.flatTextCache = null; + note.invalidateThisCache(); } } else { diff --git a/src/services/search/expressions/relation_where.js b/src/services/search/expressions/relation_where.js index 486b1b941..8f463a992 100644 --- a/src/services/search/expressions/relation_where.js +++ b/src/services/search/expressions/relation_where.js @@ -18,7 +18,7 @@ class RelationWhereExp extends Expression { for (const attr of noteCache.findAttributes('relation', this.relationName)) { const note = attr.note; - if (inputNoteSet.hasNoteId(note.noteId)) { + if (inputNoteSet.hasNoteId(note.noteId) && attr.targetNote) { const subInputNoteSet = new NoteSet([attr.targetNote]); const subResNoteSet = this.subExpression.execute(subInputNoteSet, executionContext);