import raf from 'raf';

class TouchHandler {
  constructor(onTouchStart, onTouchMove, onTouchEnd) {
    this._currentAnimationFrame = null;
    this._currentlyAnimating = false;

    this._onTouchStart = onTouchStart;
    this._onTouchMove = onTouchMove;
    this._onTouchEnd = onTouchEnd;

    this.handleTouchStart = this.handleTouchStart.bind(this);
    this.handleTouchMove = this.handleTouchMove.bind(this);
    this.handleTouchEnd = this.handleTouchEnd.bind(this);
  }

  _removeListeners() {
    document.removeEventListener('touchmove', this.handleTouchMove);
    document.removeEventListener('touchend', this.handleTouchEnd);
    document.removeEventListener('touchcancel', this.handleTouchEnd);
  }

  _addListeners() {
    document.addEventListener('touchmove', this.handleTouchMove);
    document.addEventListener('touchend', this.handleTouchEnd);
    document.addEventListener('touchcancel', this.handleTouchEnd);
  }

  handleTouchStart(evt, child) {
    this._addListeners();
    this._onTouchStart(evt, child);
  }

  handleTouchMove(evt) {
    evt.preventDefault();
    if (!this._currentlyAnimating) {
      this._currentAnimationFrame = raf(() => {
        this._currentlyAnimating = false;
        this._onTouchMove(evt);
      });
    }
    this._currentlyAnimating = true;
  }

  handleTouchEnd(evt) {
    this._currentlyAnimating = false;
    this._removeListeners();
    this._onTouchEnd(evt);
  }

  removeListeners() {
    this._removeListeners();
  }

  cancelAnimationFrame() {
    raf.cancel(this._animationFrame);
    this._animationFrame = null;
  }
}

export default TouchHandler;

@@ -136,46 +136,8 @@
 ull;
-%0A    this._currentlyAnimating = false;
 %0A%0A  
@@ -1060,27 +1060,30 @@
 _current
-ly
 Animati
-ng
+onFrame
 ) %7B%0A    
@@ -1147,35 +1147,37 @@
 _current
-ly
 Animati
-ng = false
+onFrame = null
 ;%0A      
@@ -1222,45 +1222,8 @@
   %7D%0A
-    this._currentlyAnimating = true;%0A
   %7D%0A
@@ -1260,35 +1260,30 @@
 his.
-_currentlyAnimating = false
+cancelAnimationFrame()
 ;%0A  
@@ -1441,25 +1441,32 @@
 ancel(this._
-a
+currentA
 nimationFram
@@ -1479,17 +1479,24 @@
   this._
-a
+currentA
 nimation

remove extra code / squash typo bug