diff --git a/addons/mail/static/src/models/user_setting/user_setting.js b/addons/mail/static/src/models/user_setting/user_setting.js index 363530fc15e..565d0d2b8e8 100644 --- a/addons/mail/static/src/models/user_setting/user_setting.js +++ b/addons/mail/static/src/models/user_setting/user_setting.js @@ -1,23 +1,21 @@ /** @odoo-module **/ -import { browser } from "@web/core/browser/browser"; - import { registerModel } from '@mail/model/model_core'; import { attr, many, one } from '@mail/model/model_field'; -import { insertAndReplace } from '@mail/model/model_field_command'; +import { clear, insertAndReplace } from '@mail/model/model_field_command'; registerModel({ name: 'UserSetting', identifyingFields: ['id'], lifecycleHooks: { _created() { - this._timeoutIds = {}; this._loadLocalSettings(); }, _willDelete() { - for (const timeoutId of Object.values(this._timeoutIds)) { - browser.clearTimeout(timeoutId); + for (const timeout of Object.values(this.volumeSettingsTimeouts)) { + this.messaging.browser.clearTimeout(timeout); } + this.messaging.browser.clearTimeout(this.globalSettingsTimeout); }, }, modelMethods: { @@ -130,23 +128,18 @@ registerModel({ * @param {number} param0.volume */ async saveVolumeSetting({ guestId, partnerId, volume }) { - this._debounce(async () => { - await this.async(() => this.env.services.rpc( - { - model: 'res.users.settings', - method: 'set_volume_setting', - args: [ - [this.messaging.currentUser.resUsersSettingsId], - partnerId, - volume, - ], - kwargs: { - guest_id: guestId, - }, - }, - { shadow: true }, - )); - }, 5000, `sound_${partnerId}`); + if (this.volumeSettingsTimeouts[partnerId]) { + this.messaging.browser.clearTimeout(this.volumeSettingsTimeouts[partnerId]); + } + this.update({ + volumeSettingsTimeouts: { + ...this.volumeSettingsTimeouts, + [partnerId]: this.messaging.browser.setTimeout( + this._onSaveVolumeSettingTimeout.bind(this, { guestId, partnerId, volume }), + 5000, + ), + }, + }); }, /** * @param {float} voiceActivationThreshold @@ -172,21 +165,6 @@ registerModel({ toggleWindow() { this.update({ isOpen: !this.isOpen }); }, - /** - * @private - * @param {function} f - * @param {number} delay in ms - * @param {any} key - */ - _debounce(f, delay, key) { - this._timeoutIds[key] && browser.clearTimeout(this._timeoutIds[key]); - this._timeoutIds[key] = browser.setTimeout(() => { - if (!this.exists()) { - return; - } - f(); - }, delay); - }, /** * @private */ @@ -208,21 +186,64 @@ registerModel({ /** * @private */ - async _saveSettings() { - this._debounce(async () => { - await this.async(() => this.env.services.rpc( - { - model: 'res.users.settings', - method: 'set_res_users_settings', - args: [[this.messaging.currentUser.resUsersSettingsId], { - push_to_talk_key: this.pushToTalkKey, - use_push_to_talk: this.usePushToTalk, - voice_active_duration: this.voiceActiveDuration, - }], + async _onSaveGlobalSettingsTimeout() { + if (!this.exists()) { + return; + } + this.update({ globalSettingsTimeout: clear() }); + await this.env.services.rpc( + { + model: 'res.users.settings', + method: 'set_res_users_settings', + args: [[this.messaging.currentUser.resUsersSettingsId], { + push_to_talk_key: this.pushToTalkKey, + use_push_to_talk: this.usePushToTalk, + voice_active_duration: this.voiceActiveDuration, + }], + }, + { shadow: true }, + ); + }, + /** + * @param {Object} param0 + * @param {number} [param0.guestId] + * @param {number} [param0.partnerId] + * @param {number} param0.volume + */ + async _onSaveVolumeSettingTimeout({ guestId, partnerId, volume }) { + if (!this.exists()) { + return; + } + const newVolumeSettingsTimeouts = { ...this.volumeSettingsTimeouts }; + delete newVolumeSettingsTimeouts[partnerId]; + this.update({ volumeSettingsTimeouts: newVolumeSettingsTimeouts }); + await this.env.services.rpc( + { + model: 'res.users.settings', + method: 'set_volume_setting', + args: [ + [this.messaging.currentUser.resUsersSettingsId], + partnerId, + volume, + ], + kwargs: { + guest_id: guestId, }, - { shadow: true }, - )); - }, 2000, 'globalSettings'); + }, + { shadow: true }, + ); + }, + /** + * @private + */ + async _saveSettings() { + this.messaging.browser.clearTimeout(this.globalSettingsTimeout); + this.update({ + globalSettingsTimeout: this.messaging.browser.setTimeout( + this._onSaveGlobalSettingsTimeout, + 2000, + ), + }); }, }, fields: { @@ -232,6 +253,7 @@ registerModel({ audioInputDeviceId: attr({ default: '', }), + globalSettingsTimeout: attr(), id: attr({ readonly: true, required: true, @@ -288,5 +310,8 @@ registerModel({ inverse: 'userSetting', isCausal: true, }), + volumeSettingsTimeouts: attr({ + default: {}, + }), }, });