Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 41c9d00

Browse files
Splaktarjelbourn
authored andcommitted
fix(theming): md-theme leaks when child elements are removed (#11326)
only register a theme name watcher if we are watching for theme changes or it is an async theme use element's scope to listen for $destroy events instead of the element fallback to using the listener on the element due to g3 e2e failures Fixes #11325
1 parent 869bc21 commit 41c9d00

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

src/core/services/theming/theming.js

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,7 @@ function ThemingProvider($mdColorPalette, $$mdMetaProvider) {
744744
*/
745745
function inheritTheme (el, parent) {
746746
var ctrl = parent.controller('mdTheme') || el.data('$mdThemeController');
747+
var scope = el.scope();
747748

748749
updateThemeClass(lookupThemeName());
749750

@@ -752,24 +753,36 @@ function ThemingProvider($mdColorPalette, $$mdMetaProvider) {
752753
ctrl.$shouldWatch ||
753754
$mdUtil.parseAttributeBoolean(el.attr('md-theme-watch'));
754755

755-
var unwatch = ctrl.registerChanges(function (name) {
756-
updateThemeClass(name);
756+
if (watchTheme || ctrl.isAsyncTheme) {
757+
var clearNameWatcher = function () {
758+
if (unwatch) {
759+
unwatch();
760+
unwatch = undefined;
761+
}
762+
};
757763

758-
if (!watchTheme) {
759-
unwatch();
760-
}
761-
else {
762-
el.on('$destroy', unwatch);
764+
var unwatch = ctrl.registerChanges(function(name) {
765+
updateThemeClass(name);
766+
767+
if (!watchTheme) {
768+
clearNameWatcher();
769+
}
770+
});
771+
772+
if (scope) {
773+
scope.$on('$destroy', clearNameWatcher);
774+
} else {
775+
el.on('$destroy', clearNameWatcher);
763776
}
764-
});
777+
}
765778
}
766779

767780
/**
768781
* Find the theme name from the parent controller or element data
769782
*/
770783
function lookupThemeName() {
771784
// As a few components (dialog) add their controllers later, we should also watch for a controller init.
772-
return ctrl && ctrl.$mdTheme || (defaultTheme == 'default' ? '' : defaultTheme);
785+
return ctrl && ctrl.$mdTheme || (defaultTheme === 'default' ? '' : defaultTheme);
773786
}
774787

775788
/**
@@ -819,7 +832,13 @@ function ThemingDirective($mdTheming, $interpolate, $parse, $mdUtil, $q, $log) {
819832
.trim()
820833
.substr(0, oneTimeOperator.length) === oneTimeOperator;
821834

835+
var getTheme = function () {
836+
var interpolation = $interpolate(attrs.mdTheme)(scope);
837+
return $parse(interpolation)(scope) || interpolation;
838+
};
839+
822840
var ctrl = {
841+
isAsyncTheme: angular.isFunction(getTheme()) || angular.isFunction(getTheme().then),
823842
registerChanges: function (cb, context) {
824843
if (context) {
825844
cb = angular.bind(context, cb);
@@ -844,7 +863,8 @@ function ThemingDirective($mdTheming, $interpolate, $parse, $mdUtil, $q, $log) {
844863

845864
// Iterating backwards to support unregistering during iteration
846865
// http://stackoverflow.com/a/9882349/890293
847-
// we don't use `reverse()` of array because it mutates the array and we don't want it to get re-indexed
866+
// we don't use `reverse()` of array because it mutates the array and we don't want it
867+
// to get re-indexed
848868
for (var i = registeredCallbacks.length; i--;) {
849869
registeredCallbacks[i](theme);
850870
}
@@ -856,18 +876,13 @@ function ThemingDirective($mdTheming, $interpolate, $parse, $mdUtil, $q, $log) {
856876

857877
el.data('$mdThemeController', ctrl);
858878

859-
var getTheme = function () {
860-
var interpolation = $interpolate(attrs.mdTheme)(scope);
861-
return $parse(interpolation)(scope) || interpolation;
862-
};
863-
864879
var setParsedTheme = function (theme) {
865880
if (typeof theme === 'string') {
866881
return ctrl.$setTheme(theme);
867882
}
868883

869884
$q.when( angular.isFunction(theme) ? theme() : theme )
870-
.then(function(name){
885+
.then(function(name) {
871886
ctrl.$setTheme(name);
872887
});
873888
};

0 commit comments

Comments
 (0)