mirror of
https://github.com/nasa/openmct.git
synced 2025-06-14 13:18:15 +00:00
[Time Conductor] #933 Clean up time conductor listeners on scope destruction
This commit is contained in:
@ -85,6 +85,7 @@ define(
|
|||||||
ConductorRepresenter.prototype.destroy = function destroy() {
|
ConductorRepresenter.prototype.destroy = function destroy() {
|
||||||
this.conductor.off("bounds", this.boundsListener);
|
this.conductor.off("bounds", this.boundsListener);
|
||||||
this.conductor.off("timeSystem", this.timeSystemListener);
|
this.conductor.off("timeSystem", this.timeSystemListener);
|
||||||
|
this.conductor.off("follow", this.followListener);
|
||||||
};
|
};
|
||||||
|
|
||||||
return ConductorRepresenter;
|
return ConductorRepresenter;
|
||||||
|
@ -43,8 +43,6 @@ define(
|
|||||||
this.xScale = undefined;
|
this.xScale = undefined;
|
||||||
this.xAxis = undefined;
|
this.xAxis = undefined;
|
||||||
this.axisElement = undefined;
|
this.axisElement = undefined;
|
||||||
this.setScale = this.setScale.bind(this);
|
|
||||||
this.changeTimeSystem = this.changeTimeSystem.bind(this);
|
|
||||||
|
|
||||||
// Angular Directive interface
|
// Angular Directive interface
|
||||||
this.link = this.link.bind(this);
|
this.link = this.link.bind(this);
|
||||||
@ -52,6 +50,13 @@ define(
|
|||||||
this.template =
|
this.template =
|
||||||
"<div class=\"l-axis-holder\" mct-resize=\"resize()\"></div>";
|
"<div class=\"l-axis-holder\" mct-resize=\"resize()\"></div>";
|
||||||
this.priority = 1000;
|
this.priority = 1000;
|
||||||
|
|
||||||
|
//Bind all class functions to 'this'
|
||||||
|
Object.keys(MCTConductorAxis.prototype).filter(function (key) {
|
||||||
|
return typeof MCTConductorAxis.prototype[key] === 'function';
|
||||||
|
}).forEach(function (key) {
|
||||||
|
this[key] = this[key].bind(this);
|
||||||
|
}.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
MCTConductorAxis.prototype.setScale = function () {
|
MCTConductorAxis.prototype.setScale = function () {
|
||||||
@ -99,6 +104,11 @@ define(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
MCTConductorAxis.prototype.destroy = function () {
|
||||||
|
this.conductor.off('timeSystem', this.changeTimeSystem);
|
||||||
|
this.conductor.off('bounds', this.setScale);
|
||||||
|
};
|
||||||
|
|
||||||
MCTConductorAxis.prototype.link = function (scope, element) {
|
MCTConductorAxis.prototype.link = function (scope, element) {
|
||||||
var conductor = this.conductor;
|
var conductor = this.conductor;
|
||||||
this.target = element[0].firstChild;
|
this.target = element[0].firstChild;
|
||||||
@ -121,6 +131,8 @@ define(
|
|||||||
//On conductor bounds changes, redraw ticks
|
//On conductor bounds changes, redraw ticks
|
||||||
conductor.on('bounds', this.setScale);
|
conductor.on('bounds', this.setScale);
|
||||||
|
|
||||||
|
scope.$on("$destroy", this.destroy);
|
||||||
|
|
||||||
if (conductor.timeSystem() !== undefined) {
|
if (conductor.timeSystem() !== undefined) {
|
||||||
this.changeTimeSystem(conductor.timeSystem());
|
this.changeTimeSystem(conductor.timeSystem());
|
||||||
this.setScale();
|
this.setScale();
|
||||||
|
@ -32,7 +32,9 @@ define(['./MctConductorAxis'], function (MctConductorAxis) {
|
|||||||
d3;
|
d3;
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
mockScope = {};
|
mockScope = jasmine.createSpyObj("scope", [
|
||||||
|
"$on"
|
||||||
|
]);
|
||||||
|
|
||||||
//Add some HTML elements
|
//Add some HTML elements
|
||||||
mockTarget = {
|
mockTarget = {
|
||||||
@ -49,7 +51,8 @@ define(['./MctConductorAxis'], function (MctConductorAxis) {
|
|||||||
mockConductor = jasmine.createSpyObj("conductor", [
|
mockConductor = jasmine.createSpyObj("conductor", [
|
||||||
"timeSystem",
|
"timeSystem",
|
||||||
"bounds",
|
"bounds",
|
||||||
"on"
|
"on",
|
||||||
|
"off"
|
||||||
]);
|
]);
|
||||||
mockConductor.bounds.andReturn(mockBounds);
|
mockConductor.bounds.andReturn(mockBounds);
|
||||||
|
|
||||||
@ -85,6 +88,13 @@ define(['./MctConductorAxis'], function (MctConductorAxis) {
|
|||||||
expect(mockConductor.on).toHaveBeenCalledWith("bounds", directive.setScale);
|
expect(mockConductor.on).toHaveBeenCalledWith("bounds", directive.setScale);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("on scope destruction, deregisters listeners", function () {
|
||||||
|
expect(mockScope.$on).toHaveBeenCalledWith("$destroy", directive.destroy);
|
||||||
|
directive.destroy();
|
||||||
|
expect(mockConductor.off).toHaveBeenCalledWith("timeSystem", directive.changeTimeSystem);
|
||||||
|
expect(mockConductor.off).toHaveBeenCalledWith("bounds", directive.setScale);
|
||||||
|
});
|
||||||
|
|
||||||
describe("when the time system changes", function () {
|
describe("when the time system changes", function () {
|
||||||
var mockTimeSystem;
|
var mockTimeSystem;
|
||||||
var mockFormat;
|
var mockFormat;
|
||||||
|
@ -53,9 +53,6 @@ define(
|
|||||||
this.initializeScope();
|
this.initializeScope();
|
||||||
|
|
||||||
this.conductor.on('bounds', this.setFormFromBounds);
|
this.conductor.on('bounds', this.setFormFromBounds);
|
||||||
this.conductor.on('follow', function (follow) {
|
|
||||||
$scope.followMode = follow;
|
|
||||||
});
|
|
||||||
this.conductor.on('timeSystem', this.changeTimeSystem);
|
this.conductor.on('timeSystem', this.changeTimeSystem);
|
||||||
|
|
||||||
// If no mode selected, select fixed as the default
|
// If no mode selected, select fixed as the default
|
||||||
@ -100,6 +97,13 @@ define(
|
|||||||
|
|
||||||
// Watch scope for selection of mode or time system by user
|
// Watch scope for selection of mode or time system by user
|
||||||
this.$scope.$watch('modeModel.selectedKey', this.setMode);
|
this.$scope.$watch('modeModel.selectedKey', this.setMode);
|
||||||
|
|
||||||
|
this.$scope.$on('$destroy', this.destroy);
|
||||||
|
};
|
||||||
|
|
||||||
|
TimeConductorController.prototype.destroy = function () {
|
||||||
|
this.conductor.off('bounds', this.setFormFromBounds);
|
||||||
|
this.conductor.off('timeSystem', this.changeTimeSystem);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -30,14 +30,18 @@ define(['./TimeConductorController'], function (TimeConductorController) {
|
|||||||
var controller;
|
var controller;
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
mockScope = jasmine.createSpyObj("$scope", ["$watch"]);
|
mockScope = jasmine.createSpyObj("$scope", [
|
||||||
|
"$watch",
|
||||||
|
"$on"
|
||||||
|
]);
|
||||||
mockWindow = jasmine.createSpyObj("$window", ["requestAnimationFrame"]);
|
mockWindow = jasmine.createSpyObj("$window", ["requestAnimationFrame"]);
|
||||||
mockTimeConductor = jasmine.createSpyObj(
|
mockTimeConductor = jasmine.createSpyObj(
|
||||||
"TimeConductor",
|
"TimeConductor",
|
||||||
[
|
[
|
||||||
"bounds",
|
"bounds",
|
||||||
"timeSystem",
|
"timeSystem",
|
||||||
"on"
|
"on",
|
||||||
|
"off"
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
mockTimeConductor.bounds.andReturn({start: undefined, end: undefined});
|
mockTimeConductor.bounds.andReturn({start: undefined, end: undefined});
|
||||||
@ -124,9 +128,16 @@ define(['./TimeConductorController'], function (TimeConductorController) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("listens for changes to conductor state", function () {
|
it("listens for changes to conductor state", function () {
|
||||||
expect(mockTimeConductor.on).toHaveBeenCalledWith("timeSystem", jasmine.any(Function));
|
expect(mockTimeConductor.on).toHaveBeenCalledWith("timeSystem", controller.changeTimeSystem);
|
||||||
expect(mockTimeConductor.on).toHaveBeenCalledWith("bounds", jasmine.any(Function));
|
expect(mockTimeConductor.on).toHaveBeenCalledWith("bounds", controller.setFormFromBounds);
|
||||||
expect(mockTimeConductor.on).toHaveBeenCalledWith("follow", jasmine.any(Function));
|
});
|
||||||
|
|
||||||
|
it("deregisters conductor listens when scope is destroyed", function () {
|
||||||
|
expect(mockScope.$on).toHaveBeenCalledWith("$destroy", controller.destroy);
|
||||||
|
|
||||||
|
controller.destroy();
|
||||||
|
expect(mockTimeConductor.off).toHaveBeenCalledWith("timeSystem", controller.changeTimeSystem);
|
||||||
|
expect(mockTimeConductor.off).toHaveBeenCalledWith("bounds", controller.setFormFromBounds);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("when time system changes, sets time system on scope", function () {
|
it("when time system changes, sets time system on scope", function () {
|
||||||
@ -164,17 +175,6 @@ define(['./TimeConductorController'], function (TimeConductorController) {
|
|||||||
expect(mockScope.boundsModel.start).toEqual(bounds.start);
|
expect(mockScope.boundsModel.start).toEqual(bounds.start);
|
||||||
expect(mockScope.boundsModel.end).toEqual(bounds.end);
|
expect(mockScope.boundsModel.end).toEqual(bounds.end);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("responds to a change in 'follow' state of the time conductor", function () {
|
|
||||||
var followListener = getListener("follow");
|
|
||||||
expect(followListener).toBeDefined();
|
|
||||||
|
|
||||||
followListener(true);
|
|
||||||
expect(mockScope.followMode).toEqual(true);
|
|
||||||
|
|
||||||
followListener(false);
|
|
||||||
expect(mockScope.followMode).toEqual(false);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("when user makes changes from UI", function () {
|
describe("when user makes changes from UI", function () {
|
||||||
|
Reference in New Issue
Block a user