2
0
mirror of https://github.com/frappe/books.git synced 2024-11-08 14:50:56 +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);
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);
}
}