Skip to content

Commit 7cb7b0e

Browse files
committed
fix($mdInteraction): clean up events on $rootScope destroy
this prevents unit tests from leaking memory Fixes: angular#11493
1 parent 7cde443 commit 7cb7b0e

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

src/core/services/interaction/interaction.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,20 @@ angular
3333
* </hljs>
3434
*
3535
*/
36-
function MdInteractionService($timeout, $mdUtil) {
36+
function MdInteractionService($timeout, $mdUtil, $rootScope) {
3737
this.$timeout = $timeout;
3838
this.$mdUtil = $mdUtil;
39+
this.$rootScope = $rootScope;
3940

41+
// IE browsers can also trigger pointer events, which also leads to an interaction.
42+
this.pointerEvent = 'MSPointerEvent' in window ? 'MSPointerDown' : 'PointerEvent' in window ? 'pointerdown' : null;
4043
this.bodyElement = angular.element(document.body);
4144
this.isBuffering = false;
4245
this.bufferTimeout = null;
4346
this.lastInteractionType = null;
4447
this.lastInteractionTime = null;
48+
this.inputHandler = this.onInputEvent.bind(this);
49+
this.bufferedInputHandler = this.onBufferInputEvent.bind(this);
4550

4651
// Type Mappings for the different events
4752
// There will be three three interaction types
@@ -65,24 +70,41 @@ function MdInteractionService($timeout, $mdUtil) {
6570
};
6671

6772
this.initializeEvents();
73+
this.$rootScope.$on('$destroy', this.deregister.bind(this));
6874
}
6975

76+
/**
77+
* Removes all event listeners created by $mdInteration on the
78+
* body element.
79+
*/
80+
MdInteractionService.prototype.deregister = function() {
81+
82+
this.bodyElement.off('keydown mousedown', this.inputHandler);
83+
84+
if ('ontouchstart' in document.documentElement) {
85+
this.bodyElement.off('touchstart', this.bufferedInputHandler);
86+
}
87+
88+
if (this.pointerEvent) {
89+
this.bodyElement.off(this.pointerEvent, this.inputHandler);
90+
}
91+
92+
};
93+
7094
/**
7195
* Initializes the interaction service, by registering all interaction events to the
7296
* body element.
7397
*/
7498
MdInteractionService.prototype.initializeEvents = function() {
75-
// IE browsers can also trigger pointer events, which also leads to an interaction.
76-
var pointerEvent = 'MSPointerEvent' in window ? 'MSPointerDown' : 'PointerEvent' in window ? 'pointerdown' : null;
7799

78-
this.bodyElement.on('keydown mousedown', this.onInputEvent.bind(this));
100+
this.bodyElement.on('keydown mousedown', this.inputHandler);
79101

80102
if ('ontouchstart' in document.documentElement) {
81-
this.bodyElement.on('touchstart', this.onBufferInputEvent.bind(this));
103+
this.bodyElement.on('touchstart', this.bufferedInputHandler);
82104
}
83105

84-
if (pointerEvent) {
85-
this.bodyElement.on(pointerEvent, this.onInputEvent.bind(this));
106+
if (this.pointerEvent) {
107+
this.bodyElement.on(this.pointerEvent, this.inputHandler);
86108
}
87109

88110
};

0 commit comments

Comments
 (0)