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

Commit a4a010d

Browse files
committed
Merge pull request #1536 from wesleycho/feat/optimize-resize
feat(perf): debounce resize callback
2 parents 5a42bef + 115ebf4 commit a4a010d

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/uiSelectController.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
* put as much logic in the controller (instead of the link functions) as possible so it can be easily tested.
66
*/
77
uis.controller('uiSelectCtrl',
8-
['$scope', '$element', '$timeout', '$filter', 'uisRepeatParser', 'uiSelectMinErr', 'uiSelectConfig', '$parse', '$injector', '$window',
9-
function($scope, $element, $timeout, $filter, RepeatParser, uiSelectMinErr, uiSelectConfig, $parse, $injector, $window) {
8+
['$scope', '$element', '$timeout', '$filter', '$$uisDebounce', 'uisRepeatParser', 'uiSelectMinErr', 'uiSelectConfig', '$parse', '$injector', '$window',
9+
function($scope, $element, $timeout, $filter, $$uisDebounce, RepeatParser, uiSelectMinErr, uiSelectConfig, $parse, $injector, $window) {
1010

1111
var ctrl = this;
1212

@@ -635,8 +635,8 @@ uis.controller('uiSelectCtrl',
635635
ctrl.searchInput.off('keyup keydown tagged blur paste');
636636
});
637637

638-
angular.element($window).bind('resize', function() {
638+
angular.element($window).bind('resize', $$uisDebounce(function() {
639639
ctrl.sizeSearchInput();
640-
});
640+
}), 50);
641641

642642
}]);

src/uisDebounceService.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Debounces functions
3+
*
4+
* Taken from UI Bootstrap $$debounce source code
5+
* See https://github.com/angular-ui/bootstrap/blob/master/src/debounce/debounce.js
6+
*
7+
*/
8+
uis.factory('$$uisDebounce', ['$timeout', function($timeout) {
9+
return function(callback, debounceTime) {
10+
var timeoutPromise;
11+
12+
return function() {
13+
var self = this;
14+
var args = Array.prototype.slice.call(arguments);
15+
if (timeoutPromise) {
16+
$timeout.cancel(timeoutPromise);
17+
}
18+
19+
timeoutPromise = $timeout(function() {
20+
callback.apply(self, args);
21+
}, debounceTime);
22+
};
23+
};
24+
}]);

0 commit comments

Comments
 (0)