fix(desk): maintain realtime & cached data consistency

- Clear docinfo_update callbacks before setting one; ensure only
  one active callback at any given point.
- Remove document from local cache if list_update sent if not edited
This commit is contained in:
Gavin D'souza 2022-11-16 15:04:55 +05:30
parent 45b0c3e28d
commit 2b7e4554c4
2 changed files with 16 additions and 10 deletions

View file

@ -1944,21 +1944,24 @@ frappe.ui.form.Form = class FrappeForm {
let docname = this.docname;
frappe.socketio.doc_subscribe(doctype, docname);
if (frappe.socketio.is_docinfo_listener_setup) {
return;
}
frappe.realtime.off("docinfo_update");
frappe.realtime.on("docinfo_update", ({ doc, key, action = "update" }) => {
let doc_list = frappe.model.docinfo[doctype][docname][key] || [];
if (action === "add") {
frappe.model.docinfo[doctype][docname][key].push(doc);
if (
!doc.reference_doctype ||
!doc.reference_name ||
doc.reference_doctype !== doctype ||
doc.reference_name !== docname
) {
return;
}
let doc_list = frappe.model.docinfo[doctype][docname][key] || [];
let docindex = doc_list.findIndex((old_doc) => {
return old_doc.name === doc.name;
});
if (action === "add") {
frappe.model.docinfo[doctype][docname][key].push(doc);
}
if (docindex > -1) {
if (action === "update") {
frappe.model.docinfo[doctype][docname][key].splice(docindex, 1, doc);
@ -1979,7 +1982,6 @@ frappe.ui.form.Form = class FrappeForm {
this.timeline && this.timeline.refresh();
}
});
frappe.socketio.is_docinfo_listener_setup = true;
}
// Filters fields from the reference doctype and sets them as options for a Select field

View file

@ -1317,6 +1317,10 @@ frappe.views.ListView = class ListView extends frappe.views.BaseList {
}
frappe.socketio.list_subscribe(this.doctype);
frappe.realtime.on("list_update", (data) => {
if (!frappe.get_doc(data?.doctype, data?.name)?.__unsaved) {
frappe.model.remove_from_locals(data.doctype, data.name);
}
if (this.avoid_realtime_update()) {
return;
}