2
0
mirror of https://github.com/frappe/books.git synced 2024-09-19 19:19:02 +00:00

fix: add to cache only if synced

This commit is contained in:
18alantom 2023-04-14 13:27:44 +05:30
parent 0c369309e3
commit c5c01c52bf

View File

@ -74,7 +74,7 @@ export class DocHandler {
doc = this.getNewDoc(schemaName, { name }, false); doc = this.getNewDoc(schemaName, { name }, false);
await doc.load(); await doc.load();
this.#addToCache(doc); this.#addToCache(doc, false);
return doc; return doc;
} }
@ -101,7 +101,7 @@ export class DocHandler {
const doc = new Model!(schema, data, this.fyo, isRawValueMap); const doc = new Model!(schema, data, this.fyo, isRawValueMap);
doc.name ??= this.getTemporaryName(schema); doc.name ??= this.getTemporaryName(schema);
if (cacheDoc) { if (cacheDoc) {
this.#addToCache(doc); this.#addToCache(doc, true);
} }
return doc; return doc;
@ -131,7 +131,7 @@ export class DocHandler {
* Cache operations * Cache operations
*/ */
#addToCache(doc: Doc) { #addToCache(doc: Doc, isNew: boolean) {
if (!doc.name) { if (!doc.name) {
return; return;
} }
@ -144,7 +144,9 @@ export class DocHandler {
this.#setCacheUpdationListeners(schemaName); this.#setCacheUpdationListeners(schemaName);
} }
this.docs.get(schemaName)![name] = doc; if (!isNew) {
this.docs.get(schemaName)![name] = doc;
}
// singles available as first level objects too // singles available as first level objects too
if (schemaName === doc.name) { if (schemaName === doc.name) {
@ -157,12 +159,12 @@ export class DocHandler {
}); });
doc.on('afterSync', () => { doc.on('afterSync', () => {
if (doc.name === name) { if (doc.name === name && this.#cacheHas(schemaName, name)) {
return; return;
} }
this.#removeFromCache(doc.schemaName, name); 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.#removeFromCache(schemaName, names.oldName);
this.#addToCache(doc); this.#addToCache(doc, false);
} }
); );
} }
@ -194,4 +196,8 @@ export class DocHandler {
const docMap = this.docs.get(schemaName); const docMap = this.docs.get(schemaName);
return docMap?.[name]; return docMap?.[name];
} }
#cacheHas(schemaName: string, name: string): boolean {
return !!this.#getFromCache(schemaName, name);
}
} }