Skip to content

Commit

Permalink
fix: Remove any orphaned event listeners on disable (#1337) (#1340)
Browse files Browse the repository at this point in the history
* remove any added event listeners on disable
(#1337)

* change comment
  • Loading branch information
kevinleedrum authored Mar 23, 2021
1 parent f8a9110 commit d375796
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/eventListeners/mouseEventListeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ let isClickEvent = true;
let preventClickTimeout;
const clickDelay = 200;

const addedListeners = new Map();

function getEventButtons(event) {
if (typeof event.buttons === 'number') {
return event.buttons;
Expand Down Expand Up @@ -264,6 +266,8 @@ function mouseDown(e) {

document.removeEventListener('mousemove', onMouseMove);
document.removeEventListener('mouseup', onMouseUp);
addedListeners.delete(onMouseMove);
addedListeners.delete(onMouseUp);

element.addEventListener('mousemove', mouseMove);

Expand All @@ -272,6 +276,8 @@ function mouseDown(e) {

document.addEventListener('mousemove', onMouseMove);
document.addEventListener('mouseup', onMouseUp);
addedListeners.set(onMouseMove, 'mousemove');
addedListeners.set(onMouseUp, 'mouseup');
}

function mouseMove(e) {
Expand Down Expand Up @@ -356,6 +362,11 @@ function disable(element) {
element.removeEventListener('mousedown', mouseDown);
element.removeEventListener('mousemove', mouseMove);
element.removeEventListener('dblclick', mouseDoubleClick);
// Make sure we have removed any listeners that were added within the above listeners (#1337)
addedListeners.forEach((listener, event) => {
document.removeEventListener(event, listener);
});
addedListeners.clear();
}

function enable(element) {
Expand Down

0 comments on commit d375796

Please sign in to comment.