Skip to content
This repository has been archived by the owner on Oct 2, 2019. It is now read-only.

Commit

Permalink
feat(perf): debounce resize callback
Browse files Browse the repository at this point in the history
- The resize event fires often, so debouncing the function execution can
  improve performance dramatically
  • Loading branch information
wesleycho committed Mar 29, 2016
1 parent ce6a554 commit 115ebf4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/uiSelectController.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* put as much logic in the controller (instead of the link functions) as possible so it can be easily tested.
*/
uis.controller('uiSelectCtrl',
['$scope', '$element', '$timeout', '$filter', 'uisRepeatParser', 'uiSelectMinErr', 'uiSelectConfig', '$parse', '$injector', '$window',
function($scope, $element, $timeout, $filter, RepeatParser, uiSelectMinErr, uiSelectConfig, $parse, $injector, $window) {
['$scope', '$element', '$timeout', '$filter', '$$uisDebounce', 'uisRepeatParser', 'uiSelectMinErr', 'uiSelectConfig', '$parse', '$injector', '$window',
function($scope, $element, $timeout, $filter, $$uisDebounce, RepeatParser, uiSelectMinErr, uiSelectConfig, $parse, $injector, $window) {

var ctrl = this;

Expand Down Expand Up @@ -635,8 +635,8 @@ uis.controller('uiSelectCtrl',
ctrl.searchInput.off('keyup keydown tagged blur paste');
});

angular.element($window).bind('resize', function() {
angular.element($window).bind('resize', $$uisDebounce(function() {
ctrl.sizeSearchInput();
});
}), 50);

}]);
24 changes: 24 additions & 0 deletions src/uisDebounceService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Debounces functions
*
* Taken from UI Bootstrap $$debounce source code
* See https://github.com/angular-ui/bootstrap/blob/master/src/debounce/debounce.js
*
*/
uis.factory('$$uisDebounce', ['$timeout', function($timeout) {
return function(callback, debounceTime) {
var timeoutPromise;

return function() {
var self = this;
var args = Array.prototype.slice.call(arguments);
if (timeoutPromise) {
$timeout.cancel(timeoutPromise);
}

timeoutPromise = $timeout(function() {
callback.apply(self, args);
}, debounceTime);
};
};
}]);

0 comments on commit 115ebf4

Please sign in to comment.