From a684879b91fe9b0c966b2192e6ad7a3d2d1e718a Mon Sep 17 00:00:00 2001 From: azivner Date: Mon, 13 Aug 2018 17:16:06 +0200 Subject: [PATCH] primitive attribute caching inside note entity, fixes #149 --- src/entities/note.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/entities/note.js b/src/entities/note.js index 89c05e6b8..c75a9144f 100644 --- a/src/entities/note.js +++ b/src/entities/note.js @@ -71,6 +71,18 @@ class Note extends Entity { } async getAttributes() { + if (!this.__attributeCache) { + await this.loadAttributesToCache(); + } + + return this.__attributeCache; + } + + invalidateAttributeCache() { + this.__attributeCache = null; + } + + async loadAttributesToCache() { const attributes = await repository.getEntities(` WITH RECURSIVE tree(noteId, level) AS ( @@ -129,7 +141,7 @@ class Note extends Entity { attr.isOwned = attr.noteId === this.noteId; } - return filteredAttributes; + this.__attributeCache = filteredAttributes; } async hasLabel(name) { @@ -171,6 +183,8 @@ class Note extends Entity { }); await label.save(); + + this.invalidateAttributeCache(); } } @@ -181,6 +195,8 @@ class Note extends Entity { if (attribute.type === 'label' && (!value || value === attribute.value)) { attribute.isDeleted = true; await attribute.save(); + + this.invalidateAttributeCache(); } } }