From 01aaeaef03854828c9ce1742843b5343dfa0e2f3 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 7 Sep 2021 14:36:58 -0400 Subject: [PATCH] SearchKit - Prevent race conditions in search display loading - Increases debounce timeout from 100 to 800 ms - Internally tracks the requests to ensure that later requests always take priority --- .../traits/searchDisplayBaseTrait.service.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/ext/search_kit/ang/crmSearchDisplay/traits/searchDisplayBaseTrait.service.js b/ext/search_kit/ang/crmSearchDisplay/traits/searchDisplayBaseTrait.service.js index f4700a6b0188..39b631e0e990 100644 --- a/ext/search_kit/ang/crmSearchDisplay/traits/searchDisplayBaseTrait.service.js +++ b/ext/search_kit/ang/crmSearchDisplay/traits/searchDisplayBaseTrait.service.js @@ -3,7 +3,8 @@ // Trait provides base methods and properties common to all search display types angular.module('crmSearchDisplay').factory('searchDisplayBaseTrait', function(crmApi4) { - var ts = CRM.ts('org.civicrm.search_kit'); + var ts = CRM.ts('org.civicrm.search_kit'), + runCount = 0; // Replace tokens keyed to rowData. // Pass view=true to replace with view value, otherwise raw value is used. @@ -77,7 +78,7 @@ $scope.$apply(function() { ctrl.runSearch(); }); - }, 100); + }, 800); // If search is embedded in contact summary tab, display count in tab-header var contactTab = $element.closest('.crm-contact-page .ui-tabs-panel').attr('id'); @@ -142,12 +143,16 @@ // Call SearchDisplay.run and update ctrl.results and ctrl.rowCount runSearch: function(editedRow) { var ctrl = this, + requestId = ++runCount; apiParams = this.getApiParams(); this.loading = true; _.each(ctrl.onPreRun, function(callback) { callback.call(ctrl, apiParams); }); return crmApi4('SearchDisplay', 'run', apiParams).then(function(results) { + if (requestId < runCount) { + return; // Another request started after this one + } ctrl.results = results; ctrl.editing = ctrl.loading = false; if (!ctrl.rowCount) { @@ -164,6 +169,9 @@ callback.call(ctrl, results, 'success', editedRow); }); }, function(error) { + if (requestId < runCount) { + return; // Another request started after this one + } ctrl.results = []; ctrl.editing = ctrl.loading = false; _.each(ctrl.onPostRun, function(callback) {