EspoCRM – Open Source CRM Application
PHP calendarcontactscrmcrm-platformcrm-systemcustomer-portalcustomer-supportcustomizabledocumentsemail-marketingkanbanleadsopen-sourcephpplatformsales-automationsingle-page-applicationsupport
record websocket debounce
Yuri Kuznetsov832cfb891591
diff --git a/client/src/helpers/util/debounce.js b/client/src/helpers/util/debounce.jsnew file mode 100644index 0000000000..ca609049dc--- /dev/null+++ b/client/src/helpers/util/debounce.js@@ -0,0 +1,100 @@+/************************************************************************+ * This file is part of EspoCRM.+ *+ * EspoCRM – Open Source CRM application.+ * Copyright (C) 2014-2025 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko+ * Website: https://www.espocrm.com+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as published by+ * the Free Software Foundation, either version 3 of the License, or+ * (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ * The interactive user interfaces in modified source and object code versions+ * of this program must display Appropriate Legal Notices, as required under+ * Section 5 of the GNU Affero General Public License version 3.+ *+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.+ ************************************************************************/++/**+ * A debounce helper.+ *+ * @since 9.1.0+ */+export default class DebounceHelper {++ /**+ * @type {boolean}+ * @private+ */+ _blocked = false++ /**+ *+ * @type {boolean}+ * @private+ */+ _calledWhenBlocked = false+++ /**+ * @param {{+ * handler: function(),+ * interval: number,+ * }} options+ * @param options+ */+ constructor(options) {+ /**+ * @private+ * @type {function}+ */+ this.handler = options.handler;++ /**+ * @private+ * @type {number}+ */+ this.interval = options.interval;+ }++ /**+ * Process.+ */+ process() {+ const handle = () => {+ if (this._blocked) {+ this._calledWhenBlocked = true;++ return;+ }++ this.handler();++ this._blocked = true;++ setTimeout(() => {+ const reRun = this._calledWhenBlocked;++ this._blocked = false;+ this._calledWhenBlocked = false;++ if (reRun) {+ handle();+ }+ }, this.interval);+ };++ handle();+ }+}diff --git a/client/src/views/record/detail.js b/client/src/views/record/detail.jsindex 383fe54daf..fe7409e88d 100644--- a/client/src/views/record/detail.js+++ b/client/src/views/record/detail.js@@ -33,6 +33,7 @@ import ViewRecordHelper from 'view-record-helper';import ActionItemSetup from 'helpers/action-item-setup';import StickyBarHelper from 'helpers/record/misc/sticky-bar';import SelectTemplateModalView from 'views/modals/select-template';+import DebounceHelper from 'helpers/util/debounce';/*** A detail record view.@@ -543,6 +544,18 @@ class DetailRecordView extends BaseRecordView {*/rootData+ /**+ * @private+ * @type {DebounceHelper}+ */+ _webSocketDebounceHelper++ /**+ * @private+ * @type {number}+ */+ _webSocketDebounceInterval = 500+/*** A shortcut-key => action map.*@@ -1888,7 +1901,7 @@ class DetailRecordView extends BaseRecordView {this.numId = Math.floor((Math.random() * 10000) + 1);// For testing purpose.- $(window).on('fetch-record.' + this.cid, () => this.handleRecordUpdate());+ $(window).on('fetch-record.' + this.cid, () => this._webSocketDebounceHelper.process());this.once('remove', () => {if (this.isChanged) {@@ -1976,6 +1989,11 @@ class DetailRecordView extends BaseRecordView {this.initElementReferences();});+ this._webSocketDebounceHelper = new DebounceHelper({+ interval: this._webSocketDebounceInterval,+ handler: () => this.handleRecordUpdate(),+ });+if (!this.isNew &&!!this.getHelper().webSocketManager &&@@ -3550,9 +3568,7 @@ class DetailRecordView extends BaseRecordView {this.recordUpdateWebSocketTopic = topic;this.isSubscribedToWebSocket = true;- this.getHelper().webSocketManager.subscribe(topic, () => {- this.handleRecordUpdate();- });+ this.getHelper().webSocketManager.subscribe(topic, () => this._webSocketDebounceHelper.process());}unsubscribeFromWebSocket() {@@ -3563,7 +3579,10 @@ class DetailRecordView extends BaseRecordView {this.getHelper().webSocketManager.unsubscribe(this.recordUpdateWebSocketTopic);}- handleRecordUpdate() {+ /**+ * @private+ */+ async handleRecordUpdate() {if (this.updateWebSocketIsBlocked) {return;}@@ -3571,16 +3590,16 @@ class DetailRecordView extends BaseRecordView {if (this.inlineEditModeIsOn || this.mode === this.MODE_EDIT) {const m = this.model.clone();- m.fetch().then(() => {- if (this.inlineEditModeIsOn || this.mode === this.MODE_EDIT) {- this.updatedAttributes = Espo.Utils.cloneDeep(m.attributes);- }- });+ await m.fetch();++ if (this.inlineEditModeIsOn || this.mode === this.MODE_EDIT) {+ this.updatedAttributes = Espo.Utils.cloneDeep(m.attributes);+ }return;}- this.model.fetch({highlight: true});+ await this.model.fetch({highlight: true});}blockUpdateWebSocket(toUnblock) {