From a0f216764b6b211eefe42690301e149de956c1d8 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Wed, 28 Jan 2015 15:53:13 -0800 Subject: [PATCH] [Plot] Cancel interval correctly Fix approach used for interval cancellation associated with resource leak closing for WTD-717; this avoids the exception observed in WTD-750. --- platform/features/plot/src/MCTChart.js | 11 +++++++++-- platform/features/plot/test/MCTChartSpec.js | 11 ++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/platform/features/plot/src/MCTChart.js b/platform/features/plot/src/MCTChart.js index 586d581fc7..43ebaa891f 100644 --- a/platform/features/plot/src/MCTChart.js +++ b/platform/features/plot/src/MCTChart.js @@ -46,7 +46,7 @@ define( function linkChart(scope, element) { var canvas = element.find("canvas")[0], - releaseInterval, + activeInterval, chart; // Try to initialize GLChart, which allows drawing using WebGL. @@ -111,8 +111,15 @@ define( } } + // Stop watching for changes to size (scope destroyed) + function releaseInterval() { + if (activeInterval) { + $interval.cancel(activeInterval); + } + } + // Check for resize, on a timer - releaseInterval = $interval(drawIfResized, 1000); + activeInterval = $interval(drawIfResized, 1000); // Watch "draw" for external changes to the set of // things to be drawn. diff --git a/platform/features/plot/test/MCTChartSpec.js b/platform/features/plot/test/MCTChartSpec.js index 837f8d0ef7..a8ddc24096 100644 --- a/platform/features/plot/test/MCTChartSpec.js +++ b/platform/features/plot/test/MCTChartSpec.js @@ -15,7 +15,7 @@ define( mockElement, mockCanvas, mockGL, - mockCancelInterval, + mockPromise, mctChart; beforeEach(function () { @@ -27,7 +27,8 @@ define( jasmine.createSpyObj("$scope", ["$watchCollection", "$on"]); mockElement = jasmine.createSpyObj("element", ["find"]); - mockCancelInterval = jasmine.createSpy("cancelInterval"); + mockInterval.cancel = jasmine.createSpy("cancelInterval"); + mockPromise = jasmine.createSpyObj("promise", ["then"]); // mct-chart uses GLChart, so it needs WebGL API @@ -72,7 +73,7 @@ define( mockElement.find.andReturn([mockCanvas]); mockCanvas.getContext.andReturn(mockGL); - mockInterval.andReturn(mockCancelInterval); + mockInterval.andReturn(mockPromise); mctChart = new MCTChart(mockInterval, mockLog); }); @@ -164,13 +165,13 @@ define( ); // Precondition - interval still active - expect(mockCancelInterval).not.toHaveBeenCalled(); + expect(mockInterval.cancel).not.toHaveBeenCalled(); // Broadcast a $destroy mockScope.$on.mostRecentCall.args[1](); // Should have stopped the interval - expect(mockCancelInterval).toHaveBeenCalled(); + expect(mockInterval.cancel).toHaveBeenCalledWith(mockPromise); }); });