Skip to content

Commit

Permalink
fix: remove duplicate drag handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Dec 29, 2020
1 parent d44dd77 commit 73d9ec4
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions lib/minimap-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -1183,28 +1183,29 @@ class MinimapElement {
offsetTop: this.getBoundingClientRect().top
}

// TODO can we avoid adding and removing the listeners every time?

const mousemoveHandler = (e) => this.drag(extractMouseEventData(e), initial)
const mouseupHandler = (e) => this.endDrag()
const dragendHandler = () => this.endDrag()

const touchmoveHandler = (e) => this.drag(extractTouchEventData(e), initial)
const touchendHandler = (e) => this.endDrag()

document.body.addEventListener('mousemove', mousemoveHandler, { passive: true })
document.body.addEventListener('mouseup', mouseupHandler, { passive: true })
document.body.addEventListener('mouseleave', mouseupHandler, { passive: true })
document.body.addEventListener('mouseup', dragendHandler, { passive: true })
document.body.addEventListener('mouseleave', dragendHandler, { passive: true })

document.body.addEventListener('touchmove', touchmoveHandler, { passive: true })
document.body.addEventListener('touchend', touchendHandler, { passive: true })
document.body.addEventListener('touchcancel', touchendHandler, { passive: true })
document.body.addEventListener('touchend', dragendHandler, { passive: true })
document.body.addEventListener('touchcancel', dragendHandler, { passive: true })

this.dragSubscription = new Disposable(function () {
document.body.removeEventListener('mousemove', mousemoveHandler)
document.body.removeEventListener('mouseup', mouseupHandler)
document.body.removeEventListener('mouseleave', mouseupHandler)
document.body.removeEventListener('mouseup', dragendHandler)
document.body.removeEventListener('mouseleave', dragendHandler)

document.body.removeEventListener('touchmove', touchmoveHandler)
document.body.removeEventListener('touchend', touchendHandler)
document.body.removeEventListener('touchcancel', touchendHandler)
document.body.removeEventListener('touchend', dragendHandler)
document.body.removeEventListener('touchcancel', dragendHandler)
})
}

Expand Down

0 comments on commit 73d9ec4

Please sign in to comment.