diff options
| author | Andrej Mihajlov <and@codeispoetry.ru> | 2017-07-18 21:05:10 +0100 |
|---|---|---|
| committer | Erik Larkö <erik@mullvad.net> | 2017-07-20 09:46:13 +0200 |
| commit | ce767c669272fe9e7fe8f016d1be1f45b4d01dd4 (patch) | |
| tree | bfef31a3084fb2e15482d2eba12cee1472df766c /app/components | |
| parent | 26a20266e5e18ad93c3864a1a0b1449f5fd9dd6b (diff) | |
| download | mullvadvpn-ce767c669272fe9e7fe8f016d1be1f45b4d01dd4.tar.xz mullvadvpn-ce767c669272fe9e7fe8f016d1be1f45b4d01dd4.zip | |
Refactor mouse events capturing
Diffstat (limited to 'app/components')
| -rw-r--r-- | app/components/Switch.js | 29 |
1 files changed, 14 insertions, 15 deletions
diff --git a/app/components/Switch.js b/app/components/Switch.js index 4d6ec6202f..59f997d879 100644 --- a/app/components/Switch.js +++ b/app/components/Switch.js @@ -18,30 +18,26 @@ export default class Switch extends Component { onChange: null } + isCapturingMouseEvents = false; ref: ?HTMLInputElement; onRef = (e: HTMLInputElement) => this.ref = e; state = { - isTracking: false, ignoreChange: false, - initialPos: (null: ?Point2d), + initialPos: ({x: 0, y: 0}: Point2d), startTime: (null: ?number) } handleMouseDown = (e: MouseEvent) => { const { clientX: x, clientY: y } = e; + this.startCapturingMouseEvents(); this.setState({ - isTracking: true, initialPos: { x, y }, startTime: e.timeStamp }); } handleMouseMove = (e: MouseEvent) => { - if(!this.state.isTracking) { - return; - } - const inputElement = this.ref; const { x: x0 } = this.state.initialPos; const { clientX: x, clientY: y } = e; @@ -75,12 +71,7 @@ export default class Switch extends Component { } handleMouseUp = () => { - if(this.state.isTracking) { - this.setState({ - isTracking: false, - initialPos: null - }); - } + this.stopCapturingMouseEvents(); } handleChange = (e: Event) => { @@ -114,14 +105,22 @@ export default class Switch extends Component { } } - componentDidMount() { + startCapturingMouseEvents() { + if(this.isCapturingMouseEvents) { + throw new Error('startCapturingMouseEvents() is called out of order.'); + } document.addEventListener('mousemove', this.handleMouseMove); document.addEventListener('mouseup', this.handleMouseUp); + this.isCapturingMouseEvents = true; } - componentWillUnmount() { + stopCapturingMouseEvents() { + if(!this.isCapturingMouseEvents) { + throw new Error('stopCapturingMouseEvents() is called out of order.'); + } document.removeEventListener('mousemove', this.handleMouseMove); document.removeEventListener('mouseup', this.handleMouseUp); + this.isCapturingMouseEvents = false; } render(): React.Element<*> { |
