Skip to content

Commit

Permalink
Merge pull request #21394 from colemanw/debounceGetResults
Browse files Browse the repository at this point in the history
SearchKit - Prevent race conditions in search display loading
  • Loading branch information
eileenmcnaughton authored Sep 8, 2021
2 parents 7c5afda + 01aaeae commit d83e22b
Showing 1 changed file with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit d83e22b

Please sign in to comment.