Skip to content

Commit

Permalink
refactor(AutoResize): Avoid excessive $digests
Browse files Browse the repository at this point in the history
Using $timeout for the loop to check whether the grid dimensions have
changed or not is causing a $digest() loop every 250ms. This can cause
quite a lot of slow-down in a large grid.

Changing to explicitly calling $apply() within a setTimeout() when the
dimensions have changed should fix this. The tests have been updated to
use Jasmine's async features to cover this alteration.

Fixes #2284. Fixes #2247
  • Loading branch information
c0bra committed Dec 5, 2014
1 parent a54c663 commit 9c495da
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
20 changes: 11 additions & 9 deletions src/features/auto-resize-grid/js/auto-resize.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,26 @@
// Initialize the dimensions
getDimensions();

var canceler;
var resizeTimeoutId;
function startTimeout() {
$timeout.cancel(canceler);
clearTimeout(resizeTimeoutId);

canceler = $timeout(function () {
resizeTimeoutId = setTimeout(function () {
var newGridHeight = gridUtil.elementHeight($elm);
var newGridWidth = gridUtil.elementWidth($elm);

if (newGridHeight !== prevGridHeight || newGridWidth !== prevGridWidth) {
uiGridCtrl.grid.gridHeight = newGridHeight;
uiGridCtrl.grid.gridWidth = newGridWidth;

uiGridCtrl.grid.refresh()
.then(function () {
getDimensions();
$scope.$apply(function () {
uiGridCtrl.grid.refresh()
.then(function () {
getDimensions();

startTimeout();
});
startTimeout();
});
});
}
else {
startTimeout();
Expand All @@ -56,7 +58,7 @@
startTimeout();

$scope.$on('$destroy', function() {
$timeout.cancel(canceler);
clearTimeout(resizeTimeoutId);
});
}
};
Expand Down
26 changes: 17 additions & 9 deletions src/features/auto-resize-grid/test/auto-resize-grid.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
describe('ui.grid.autoResizeGrid', function () {
ddescribe('ui.grid.autoResizeGrid', function () {
var gridScope, gridElm, viewportElm, $scope, $compile, recompile, uiGridConstants;

var data = [
Expand Down Expand Up @@ -44,13 +44,17 @@ describe('ui.grid.autoResizeGrid', function () {
var w = $(viewportElm).width();
var h = $(viewportElm).height();

$(gridElm).width(600);
runs(function () {
$(gridElm).width(600);
});

$timeout.flush();
waits(300);

var newW = $(viewportElm).width();
runs(function () {
var newW = $(viewportElm).width();

expect(newW).toBeGreaterThan(w);
expect(newW).toBeGreaterThan(w);
});
}));
});

Expand All @@ -75,13 +79,17 @@ describe('ui.grid.autoResizeGrid', function () {
var w = $(viewportElm).width();
var h = $(viewportElm).height();

$(gridContainerElm).width(500);
runs(function () {
$(gridContainerElm).width(500);
});

$timeout.flush();
waits(300);

var newW = $(viewportElm).width();
runs(function () {
var newW = $(viewportElm).width();

expect(newW).toBeGreaterThan(w);
expect(newW).toBeGreaterThan(w);
});
}));
});

Expand Down

0 comments on commit 9c495da

Please sign in to comment.