From 63345bbd3b3fea4919bbe8947fa018c8d5d7417d Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Thu, 5 Feb 2015 17:34:37 -0800 Subject: [PATCH 1/2] [Plot] Add test for resource release Add test to detect plot failing to release its subscriptions, which causes WTD-840. --- platform/features/plot/test/PlotControllerSpec.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/platform/features/plot/test/PlotControllerSpec.js b/platform/features/plot/test/PlotControllerSpec.js index a8523502ee..865ad2c335 100644 --- a/platform/features/plot/test/PlotControllerSpec.js +++ b/platform/features/plot/test/PlotControllerSpec.js @@ -173,6 +173,19 @@ define( // Placeholder; need to support requesting telemetry expect(controller.isRequestPending()).toBeFalsy(); }); + + it("unsubscribes when destroyed", function () { + // Make an object available + mockScope.$watch.mostRecentCall.args[1](mockDomainObject); + // Make sure $destroy is what's listened for + expect(mockScope.$on.mostRecentCall.args[0]).toEqual('$destroy'); + // Also verify precondition + expect(mockSubscription.unsubscribe).not.toHaveBeenCalled(); + // Destroy the scope + mockScope.$on.mostRecentCall.args[1](); + // Should have unsubscribed + expect(mockSubscription.unsubscribe).toHaveBeenCalled(); + }); }); } ); \ No newline at end of file From f78226bc69a572f5478552a9cdc2522e47bdb5eb Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Thu, 5 Feb 2015 17:37:47 -0800 Subject: [PATCH 2/2] [Plot] Unsubscribe on destroy Unsubscribe when a plot view is destroyed; this avoids resource leaks which cause WTD-840. --- platform/features/plot/src/PlotController.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/platform/features/plot/src/PlotController.js b/platform/features/plot/src/PlotController.js index ef53efd87f..6449e0f3a1 100644 --- a/platform/features/plot/src/PlotController.js +++ b/platform/features/plot/src/PlotController.js @@ -113,8 +113,20 @@ define( } } + // Release the current subscription (called when scope is destroyed) + function releaseSubscription() { + if (subscription) { + subscription.unsubscribe(); + subscription = undefined; + } + } + + // Subscribe to telemetry when a domain object becomes available $scope.$watch('domainObject', subscribe); + // Unsubscribe when the plot is destroyed + $scope.$on("$destroy", releaseSubscription); + return { /** * Get the color (as a style-friendly string) to use