From 68663f1b2bc78de76cfd9a7c978f83d9d7c0c21c Mon Sep 17 00:00:00 2001 From: "Loan (lse)" Date: Wed, 2 Mar 2022 11:53:43 +0000 Subject: [PATCH] [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 --- .../static/src/js/microsoft_calendar.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/addons/microsoft_calendar/static/src/js/microsoft_calendar.js b/addons/microsoft_calendar/static/src/js/microsoft_calendar.js index 35fcac650be..4a1c8c22687 100644 --- a/addons/microsoft_calendar/static/src/js/microsoft_calendar.js +++ b/addons/microsoft_calendar/static/src/js/microsoft_calendar.js @@ -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 }); },