From c5c01c52bf8eec6cf1cbb0137f32ccee4788f1d6 Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Fri, 14 Apr 2023 13:27:44 +0530 Subject: [PATCH] fix: add to cache only if synced --- fyo/core/docHandler.ts | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/fyo/core/docHandler.ts b/fyo/core/docHandler.ts index a1404cd1..de6c218a 100644 --- a/fyo/core/docHandler.ts +++ b/fyo/core/docHandler.ts @@ -74,7 +74,7 @@ export class DocHandler { doc = this.getNewDoc(schemaName, { name }, false); await doc.load(); - this.#addToCache(doc); + this.#addToCache(doc, false); return doc; } @@ -101,7 +101,7 @@ export class DocHandler { const doc = new Model!(schema, data, this.fyo, isRawValueMap); doc.name ??= this.getTemporaryName(schema); if (cacheDoc) { - this.#addToCache(doc); + this.#addToCache(doc, true); } return doc; @@ -131,7 +131,7 @@ export class DocHandler { * Cache operations */ - #addToCache(doc: Doc) { + #addToCache(doc: Doc, isNew: boolean) { if (!doc.name) { return; } @@ -144,7 +144,9 @@ export class DocHandler { this.#setCacheUpdationListeners(schemaName); } - this.docs.get(schemaName)![name] = doc; + if (!isNew) { + this.docs.get(schemaName)![name] = doc; + } // singles available as first level objects too if (schemaName === doc.name) { @@ -157,12 +159,12 @@ export class DocHandler { }); doc.on('afterSync', () => { - if (doc.name === name) { + if (doc.name === name && this.#cacheHas(schemaName, name)) { return; } this.#removeFromCache(doc.schemaName, name); - this.#addToCache(doc); + this.#addToCache(doc, false); }); } @@ -180,7 +182,7 @@ export class DocHandler { } this.#removeFromCache(schemaName, names.oldName); - this.#addToCache(doc); + this.#addToCache(doc, false); } ); } @@ -194,4 +196,8 @@ export class DocHandler { const docMap = this.docs.get(schemaName); return docMap?.[name]; } + + #cacheHas(schemaName: string, name: string): boolean { + return !!this.#getFromCache(schemaName, name); + } }