Skip to content

Commit

Permalink
Fix interaction state when using mouse wheel (#840)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pessimistress authored and Xiaoji Chen committed Jul 8, 2019
1 parent 2217aed commit 4459ff0
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/utils/debounce.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* global setTimeout, clearTimeout */
/* eslint-disable consistent-this, func-names */
export default function debounce(func, delay) {
let _this;
let _arguments;
let timeout;

const executeNow = () => {
timeout = null;
return func.apply(_this, _arguments);
};

return function() {
_this = this;
_arguments = arguments;

if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(executeNow, delay);
};
}
10 changes: 8 additions & 2 deletions src/utils/map-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import MapState from './map-state';
import {LinearInterpolator} from './transition';
import TransitionManager, {TRANSITION_EVENTS} from './transition-manager';
import debounce from './debounce';

import type {MjolnirEvent} from 'mjolnir.js';

Expand Down Expand Up @@ -75,6 +76,7 @@ export default class MapController {

constructor() {
(this: any).handleEvent = this.handleEvent.bind(this);
(this: any)._onWheelEnd = debounce(this._onWheelEnd, 100);
}

/**
Expand Down Expand Up @@ -316,11 +318,15 @@ export default class MapController {

const newMapState = this.mapState.zoom({pos, scale});
this.updateViewport(newMapState, NO_TRANSITION_PROPS, {isZooming: true});
// This is a one-off event, state should not persist
this.setState({isZooming: false});
// Wheel events are discrete, let's wait a little before resetting isZooming
this._onWheelEnd();
return true;
}

_onWheelEnd() {
this.setState({isZooming: false});
}

// Default handler for the `pinchstart` event.
_onPinchStart(event: MjolnirEvent) {
const pos = this.getCenter(event);
Expand Down
24 changes: 24 additions & 0 deletions test/src/utils/debounce.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* global setTimeout */
import test from 'tape-catch';
import debounce from 'react-map-gl/utils/debounce';

test('debounce', t => {
const funcCalled = [];

function func(x) {
funcCalled.push({context: this, arg: x});
}

const debounced = debounce(func, 1);

debounced.call('0', 0);
debounced.call('1', 1);
debounced.call('2', 2);

t.deepEquals(funcCalled, [], 'function is not called yet');

setTimeout(() => {
t.deepEquals(funcCalled, [{context: '2', arg: 2}], 'function is called once');
t.end();
}, 5);
});
1 change: 1 addition & 0 deletions test/src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ import './map-state.spec';
import './map-constraints.spec';
import './dynamic-position.spec';
import './transition-manager.spec';
import './debounce.spec';

0 comments on commit 4459ff0

Please sign in to comment.