[FIX] microsoft_calendar: prevent parallel JS calls to sync_data

Before this commit:
A call to `sync_data` was made each time an event happen in the calendar
( changing the day, switching to day/week/month/year ).
This can easily saturate the server and spam the Microsoft API.

After this commit:
One call is made at a time per tab.

This PR is an adaptation of the same fix which was done only for
Google calendar at:
https://github.com/odoo/odoo/pull/69619/files#diff-8457ad89f2f7a1847af533828b40994bfc6d80e1e9d45ee7707b534bda2b0f06
and partially solve another PR that I was planning (but a bit too "overkilled"):
https://github.com/odoo/odoo/pull/85465

OPW-2749791 & OPW-2741728

closes odoo/odoo#85754

X-original-commit: d25b3d783fd08cd1c84095da554f52b463422bc2
Signed-off-by: Arnaud Joset <arj@odoo.com>
This commit is contained in:
Loan (lse)
2022-03-07 10:41:44 +00:00
parent c319e9c55f
commit 68663f1b2b
@@ -19,6 +19,7 @@ const MicrosoftCalendarModel = CalendarModel.include({
init: function () {
this._super.apply(this, arguments);
this.microsoft_is_sync = true;
this.microsoft_pending_sync = false;
},
/**
@@ -36,6 +37,10 @@ const MicrosoftCalendarModel = CalendarModel.include({
*/
async _loadCalendar() {
const _super = this._super.bind(this);
// When the calendar synchronization takes some time, prevents retriggering the sync while navigating the calendar.
if (this.microsoft_pending_sync) {
return _super(...arguments);
}
try {
await Promise.race([
new Promise(resolve => setTimeout(resolve, 1000)),
@@ -46,12 +51,14 @@ const MicrosoftCalendarModel = CalendarModel.include({
error.event.preventDefault();
}
console.error("Could not synchronize Outlook events now.", error);
this.microsoft_pending_sync = false;
}
return _super(...arguments);
},
_syncMicrosoftCalendar(shadow = false) {
var self = this;
this.microsoft_pending_sync = true;
return this._rpc({
route: '/microsoft_calendar/sync_data',
params: {
@@ -64,6 +71,7 @@ const MicrosoftCalendarModel = CalendarModel.include({
} else if (result.status === "no_new_event_from_microsoft" || result.status === "need_refresh") {
self.microsoft_is_sync = true;
}
self.microsoft_pending_sync = false;
return result
});
},