Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix($animate): avoid memory leak with `$animate.enabled(element, enab…
…led)`

When disabling/enabling animations on a specific element (via
`$animate.enabled(element, enabled)`), the element is added in a map to
track its state. Previously, the element was never removed from the map,
causing AngularJS to hold on to the element even after it is removed
from the DOM, thus preventing it from being garbage collected.

This commit fixes it by removing the element from the map on `$destroy`.

Fixes #16637.
  • Loading branch information
gkalpak committed Jul 27, 2018
commit d12578cf6a675e54424535d228b861af112f2c18
12 changes: 8 additions & 4 deletions src/apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,10 @@ function NgMapShim() {
}
NgMapShim.prototype = {
_idx: function(key) {
if (key === this._lastKey) {
return this._lastIndex;
if (key !== this._lastKey) {
this._lastKey = key;
this._lastIndex = this._keys.indexOf(key);
}
this._lastKey = key;
this._lastIndex = this._keys.indexOf(key);
return this._lastIndex;
},
_transformKey: function(key) {
Expand All @@ -62,6 +61,11 @@ NgMapShim.prototype = {
return this._values[idx];
}
},
has: function(key) {
key = this._transformKey(key);
var idx = this._idx(key);
return idx !== -1;
},
set: function(key, value) {
key = this._transformKey(key);
var idx = this._idx(key);
Expand Down
9 changes: 9 additions & 0 deletions src/ngAnimate/animateQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ var $$AnimateQueueProvider = ['$animateProvider', /** @this */ function($animate
var disabledElementsLookup = new $$Map();
var animationsEnabled = null;

function removeFromDisabledElementsLookup(evt) {
disabledElementsLookup.delete(evt.target);
}

function postDigestTaskFactory() {
var postDigestCalled = false;
return function(fn) {
Expand Down Expand Up @@ -303,6 +307,11 @@ var $$AnimateQueueProvider = ['$animateProvider', /** @this */ function($animate
bool = !disabledElementsLookup.get(node);
} else {
// (element, bool) - Element setter
if (!disabledElementsLookup.has(node)) {
// The element is added to the map for the first time.
// Create a listener to remove it on `$destroy` (to avoid memory leak).
$$jqLite(element).on('$destroy', removeFromDisabledElementsLookup);
}
disabledElementsLookup.set(node, !bool);
}
}
Expand Down
28 changes: 28 additions & 0 deletions test/ApiSpecs.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,34 @@ describe('api', function() {
expect(map.get(keys[2])).toBe(values[2]);
});

it('should return if a key exists or not', function() {
var map = new NgMapShim();
var keys = ['foo', {}];

expect(map.has(keys[0])).toBe(false);
expect(map.has(keys[1])).toBe(false);

map.set(keys[0], 'bar');
expect(map.has(keys[0])).toBe(true);
expect(map.has(keys[1])).toBe(false);

map.set(keys[1], 'baz');
expect(map.has(keys[0])).toBe(true);
expect(map.has(keys[1])).toBe(true);

map.delete(keys[0]);
expect(map.has(keys[0])).toBe(false);
expect(map.has(keys[1])).toBe(true);

map.delete(keys[1]);
expect(map.has(keys[0])).toBe(false);
expect(map.has(keys[1])).toBe(false);

map.set(keys[1], 'qux');
expect(map.has(keys[0])).toBe(false);
expect(map.has(keys[1])).toBe(true);
});

it('should be able to deal with `NaN` keys', function() {
var map = new NgMapShim();

Expand Down
35 changes: 35 additions & 0 deletions test/ngAnimate/animateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,41 @@ describe('animations', function() {
$rootScope.$digest();
expect(capturedAnimation).toBeTruthy();
}));

it('should remove the element from the `disabledElementsLookup` map on `$destroy`',
inject(function($$Map, $animate, $rootScope) {

var setSpy = spyOn($$Map.prototype, 'set').and.callThrough();
var deleteSpy = spyOn($$Map.prototype, 'delete').and.callThrough();

parent.append(element);

$animate.enabled(element, false);
$animate.enabled(element, true);
$animate.enabled(element, false);
expect(setSpy).toHaveBeenCalledWith(element[0], jasmine.any(Boolean));
expect(deleteSpy).not.toHaveBeenCalledWith(element[0]);
expect($animate.enabled(element)).toBe(false);

// No clean-up on `detach` (no `$destroy` event).
element.detach();
expect(deleteSpy).not.toHaveBeenCalledWith(element[0]);
expect($animate.enabled(element)).toBe(false);

// Clean-up on `remove` (causes `$destroy` event).
element.remove();
expect(deleteSpy).toHaveBeenCalledOnceWith(element[0]);
expect($animate.enabled(element)).toBe(true);

deleteSpy.calls.reset();

element.triggerHandler('$destroy');
expect(deleteSpy).not.toHaveBeenCalledWith(element[0]);

$animate.enabled(element, true);
element.triggerHandler('$destroy');
expect(deleteSpy).toHaveBeenCalledOnceWith(element[0]);
}));
});

it('should strip all comment nodes from the animation and not issue an animation if not real elements are found',
Expand Down