From 933927cda189a6a682b16ea28773536a4d363106 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Mon, 29 Jun 2015 09:48:32 -0700 Subject: [PATCH] [Common UI] Reduce digest count Reduce number of digest cycles triggered by mct-resize; only trigger a new digest cycle when size actually changes. #19. --- .../general/src/directives/MCTResize.js | 5 +-- .../general/test/directives/MCTResizeSpec.js | 36 +++++++++++++++++-- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/platform/commonUI/general/src/directives/MCTResize.js b/platform/commonUI/general/src/directives/MCTResize.js index 62ae977271..efe534ffd8 100644 --- a/platform/commonUI/general/src/directives/MCTResize.js +++ b/platform/commonUI/general/src/directives/MCTResize.js @@ -73,6 +73,7 @@ define( lastBounds.width !== bounds.width || lastBounds.height !== bounds.height) { scope.$eval(attrs.mctResize, { bounds: bounds }); + scope.$apply(); // Trigger a digest lastBounds = bounds; } } @@ -85,7 +86,7 @@ define( height: element[0].offsetHeight }); if (active) { - $timeout(onInterval, currentInterval()); + $timeout(onInterval, currentInterval(), false); } } @@ -111,4 +112,4 @@ define( return MCTResize; } -); \ No newline at end of file +); diff --git a/platform/commonUI/general/test/directives/MCTResizeSpec.js b/platform/commonUI/general/test/directives/MCTResizeSpec.js index b0ff7b748a..e5374305c2 100644 --- a/platform/commonUI/general/test/directives/MCTResizeSpec.js +++ b/platform/commonUI/general/test/directives/MCTResizeSpec.js @@ -35,7 +35,7 @@ define( beforeEach(function () { mockTimeout = jasmine.createSpy("$timeout"); - mockScope = jasmine.createSpyObj("$scope", ["$eval", "$on"]); + mockScope = jasmine.createSpyObj("$scope", ["$eval", "$on", "$apply"]); testElement = { offsetWidth: 100, offsetHeight: 200 }; testAttrs = { mctResize: "some-expr" }; @@ -52,7 +52,8 @@ define( mctResize.link(mockScope, [testElement], testAttrs); expect(mockTimeout).toHaveBeenCalledWith( jasmine.any(Function), - jasmine.any(Number) + jasmine.any(Number), + false ); expect(mockScope.$eval).toHaveBeenCalledWith( testAttrs.mctResize, @@ -110,6 +111,35 @@ define( expect(mockTimeout.calls.length).toEqual(2); }); + it("triggers a digest cycle when size changes", function () { + var applyCount; + mctResize.link(mockScope, [testElement], testAttrs); + applyCount = mockScope.$apply.calls.length; + + // Change the element's apparent size + testElement.offsetWidth = 300; + testElement.offsetHeight = 350; + + // Fire the timeout + mockTimeout.mostRecentCall.args[0](); + + // No more apply calls + expect(mockScope.$apply.calls.length) + .toBeGreaterThan(applyCount); + }); + + it("does not trigger a digest cycle when size does not change", function () { + var applyCount; + mctResize.link(mockScope, [testElement], testAttrs); + applyCount = mockScope.$apply.calls.length; + + // Fire the timeout + mockTimeout.mostRecentCall.args[0](); + + // No more apply calls + expect(mockScope.$apply.calls.length).toEqual(applyCount); + }); + }); } -); \ No newline at end of file +);