Skip to content

Commit c748c8f

Browse files
committed
Clean up $rootScope.$on listeners
Fixes a memory leak where we weren't properly unbinding listeners for $rootScope.$on events.
1 parent 03c7a39 commit c748c8f

File tree

6 files changed

+139
-77
lines changed

6 files changed

+139
-77
lines changed

app/scripts/directives/deploymentMetrics.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ angular.module('openshiftConsole')
464464
}
465465
};
466466

467-
$rootScope.$on('metrics.charts.resize', function(){
467+
var unbindResizeHandler = $rootScope.$on('metrics.charts.resize', function(){
468468
MetricsCharts.redraw(chartByMetric);
469469
});
470470

@@ -474,6 +474,11 @@ angular.module('openshiftConsole')
474474
intervalPromise = null;
475475
}
476476

477+
if (unbindResizeHandler) {
478+
unbindResizeHandler();
479+
unbindResizeHandler = null;
480+
}
481+
477482
angular.forEach(chartByMetric, function(chart) {
478483
chart.destroy();
479484
});

app/scripts/directives/eventsSidebar.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ angular.module('openshiftConsole')
2424
$scope.collapsed = true;
2525
};
2626

27-
$rootScope.$on('event.resource.highlight', function(evt, data) {
27+
var messageHandlers = [];
28+
messageHandlers.push($rootScope.$on('event.resource.highlight', function(evt, data) {
2829
var targetKind = _.get(data, 'kind');
2930
var targetName = _.get(data, 'metadata.name');
3031
if (!targetKind || !targetName) {
@@ -35,9 +36,9 @@ angular.module('openshiftConsole')
3536
$scope.highlightedEvents[targetKind + "/" + targetName] = true;
3637
}
3738
});
38-
});
39+
}));
3940

40-
$rootScope.$on('event.resource.clear-highlight', function(event, data) {
41+
messageHandlers.push($rootScope.$on('event.resource.clear-highlight', function(event, data) {
4142
var targetKind = _.get(data, 'kind');
4243
var targetName = _.get(data, 'metadata.name');
4344
if (!targetKind || !targetName) {
@@ -48,10 +49,14 @@ angular.module('openshiftConsole')
4849
$scope.highlightedEvents[targetKind + "/" + targetName] = false;
4950
}
5051
});
51-
});
52+
}));
5253

5354
$scope.$on('$destroy', function() {
5455
DataService.unwatchAll(watches);
56+
_.each(messageHandlers, function(unbind) {
57+
unbind();
58+
});
59+
messageHandlers = null;
5560
});
5661
},
5762
};

app/scripts/directives/podMetrics.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,10 @@ angular.module('openshiftConsole')
290290
var failureCount = 0;
291291

292292
function metricsSucceeded() {
293+
if (destroyed) {
294+
return;
295+
}
296+
293297
// Reset the number of failures on a successful request.
294298
failureCount = 0;
295299

@@ -519,7 +523,7 @@ angular.module('openshiftConsole')
519523
intervalPromise = $interval(update, MetricsCharts.getDefaultUpdateInterval(), false);
520524
});
521525

522-
$rootScope.$on('metrics.charts.resize', function() {
526+
var unbindResizeHandler = $rootScope.$on('metrics.charts.resize', function() {
523527
MetricsCharts.redraw(donutByMetric);
524528
MetricsCharts.redraw(sparklineByMetric);
525529
});
@@ -530,6 +534,11 @@ angular.module('openshiftConsole')
530534
intervalPromise = null;
531535
}
532536

537+
if (unbindResizeHandler) {
538+
unbindResizeHandler();
539+
unbindResizeHandler = null;
540+
}
541+
533542
angular.forEach(donutByMetric, function(chart) {
534543
chart.destroy();
535544
});

dist/scripts/scripts.js

+20-12
Original file line numberDiff line numberDiff line change
@@ -9944,18 +9944,22 @@ type:"Warning"
99449944
})), b.log("events (subscribe)", d.events);
99459945
})), d.highlightedEvents = {}, d.collapseSidebar = function() {
99469946
d.collapsed = !0;
9947-
}, c.$on("event.resource.highlight", function(a, b) {
9947+
};
9948+
var f = [];
9949+
f.push(c.$on("event.resource.highlight", function(a, b) {
99489950
var c = _.get(b, "kind"), e = _.get(b, "metadata.name");
99499951
c && e && _.each(d.events, function(a) {
99509952
a.involvedObject.kind === c && a.involvedObject.name === e && (d.highlightedEvents[c + "/" + e] = !0);
99519953
});
9952-
}), c.$on("event.resource.clear-highlight", function(a, b) {
9954+
})), f.push(c.$on("event.resource.clear-highlight", function(a, b) {
99539955
var c = _.get(b, "kind"), e = _.get(b, "metadata.name");
99549956
c && e && _.each(d.events, function(a) {
99559957
a.involvedObject.kind === c && a.involvedObject.name === e && (d.highlightedEvents[c + "/" + e] = !1);
99569958
});
9957-
}), d.$on("$destroy", function() {
9958-
a.unwatchAll(e);
9959+
})), d.$on("$destroy", function() {
9960+
a.unwatchAll(e), _.each(f, function(a) {
9961+
a();
9962+
}), f = null;
99599963
});
99609964
} ]
99619965
};
@@ -11927,9 +11931,9 @@ containerName:a.containerMetric ? m.options.selectedContainer.name :"pod"
1192711931
}) :null;
1192811932
}
1192911933
function u() {
11930-
L = 0, _.each(m.metrics, function(a) {
11934+
H || (L = 0, _.each(m.metrics, function(a) {
1193111935
p(a), o(a);
11932-
});
11936+
}));
1193311937
}
1193411938
function v(a) {
1193511939
if (!H) {
@@ -12095,10 +12099,12 @@ delete a.data;
1209512099
});
1209612100
}), delete m.metricsError, A();
1209712101
}, !0), B = b(A, i.getDefaultUpdateInterval(), !1);
12098-
}), f.$on("metrics.charts.resize", function() {
12102+
});
12103+
var M = f.$on("metrics.charts.resize", function() {
1209912104
i.redraw(C), i.redraw(D);
12100-
}), m.$on("$destroy", function() {
12101-
B && (b.cancel(B), B = null), angular.forEach(C, function(a) {
12105+
});
12106+
m.$on("$destroy", function() {
12107+
B && (b.cancel(B), B = null), M && (M(), M = null), angular.forEach(C, function(a) {
1210212108
a.destroy();
1210312109
}), C = null, angular.forEach(D, function(a) {
1210412110
a.destroy();
@@ -12308,10 +12314,12 @@ b.$watch("options", function() {
1230812314
A = {}, y = null, delete b.metricsError, s();
1230912315
}, !0), t = a(s, h.getDefaultUpdateInterval(), !1), b.updateInView = function(a) {
1231012316
B = !a, a && (!z || Date.now() > z + h.getDefaultUpdateInterval()) && s();
12311-
}, e.$on("metrics.charts.resize", function() {
12317+
};
12318+
var G = e.$on("metrics.charts.resize", function() {
1231212319
h.redraw(u);
12313-
}), b.$on("$destroy", function() {
12314-
t && (a.cancel(t), t = null), angular.forEach(u, function(a) {
12320+
});
12321+
b.$on("$destroy", function() {
12322+
t && (a.cancel(t), t = null), G && (G(), G = null), angular.forEach(u, function(a) {
1231512323
a.destroy();
1231612324
}), u = null, x = !0;
1231712325
});

0 commit comments

Comments
 (0)