Skip to content
1 change: 1 addition & 0 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ lib.counterRegex = regexModule.counter;

var throttleModule = require('./throttle');
lib.throttle = throttleModule.throttle;
lib.throttleDone = throttleModule.done;
lib.clearThrottle = throttleModule.clear;

lib.getGraphDiv = require('./get_graph_div');
Expand Down
29 changes: 25 additions & 4 deletions src/lib/throttle.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,40 @@ exports.throttle = function throttle(callback, minInterval, id) {

_clearTimeout(cache);

if(now > cache.ts + minInterval) {
function exec() {
callback();
Copy link
Contributor

@etpinard etpinard Sep 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So callback can't itself be async - which is fine but deserves a mention in the docstring.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-> b6d64be

cache.ts = now;
cache.ts = Date.now();
if(cache.onDone) {
cache.onDone();
cache.onDone = null;
}
}

if(now > cache.ts + minInterval) {
exec();
return;
}

cache.timer = setTimeout(function() {
callback();
cache.ts = Date.now();
exec();
cache.timer = null;
}, minInterval);
};

exports.done = function(id) {
var cache = timerCache[id];
if(!cache || !cache.timer) return Promise.resolve();

return new Promise(function(resolve) {
var previousOnDone = cache.onDone;
cache.onDone = function onDone() {
if(previousOnDone) previousOnDone();
resolve();
cache.onDone = null;
};
});
};

/**
* Clear the throttle cache for one or all timers
* @param {optional string} id:
Expand Down
30 changes: 16 additions & 14 deletions src/plots/cartesian/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,21 +208,23 @@ module.exports = function prepSelect(e, startX, startY, dragOptions, mode) {

dragOptions.doneFn = function(dragged, numclicks) {
corners.remove();
throttle.clear(throttleID);

if(!dragged && numclicks === 2) {
// clear selection on doubleclick
outlines.remove();
for(i = 0; i < searchTraces.length; i++) {
searchInfo = searchTraces[i];
searchInfo.selectPoints(searchInfo, false);
}
throttle.done(throttleID).then(function() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to wait for any throttling-delayed move event before we process the mouseup event, so we have the correct data.

throttle.clear(throttleID);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe throttle.done() should call throttle.clear() before returning a promise?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe throttle.done() should call throttle.clear() before returning a promise?

I don't think so. It doesn't come into play here, but you could imagine wanting to finish whatever event had been throttled when done was called, without cutting off throttling of future events in the train. I guess that's saying I didn't mean done to mean you were necessarily done forever, just done with whatever is in the queue right now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Thanks!


gd.emit('plotly_deselect', null);
}
else {
dragOptions.gd.emit('plotly_selected', eventData);
}
if(!dragged && numclicks === 2) {
// clear selection on doubleclick
outlines.remove();
for(i = 0; i < searchTraces.length; i++) {
searchInfo = searchTraces[i];
searchInfo.selectPoints(searchInfo, false);
}

gd.emit('plotly_deselect', null);
}
else {
dragOptions.gd.emit('plotly_selected', eventData);
}
});
};
};

Expand Down
2 changes: 2 additions & 0 deletions test/jasmine/tests/gl2d_click_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,10 +496,12 @@ describe('@noCI Test gl2d lasso/select:', function() {
function drag(path) {
var len = path.length;

Lib.clearThrottle();
mouseEvent('mousemove', path[0][0], path[0][1]);
mouseEvent('mousedown', path[0][0], path[0][1]);

path.slice(1, len).forEach(function(pt) {
Lib.clearThrottle();
mouseEvent('mousemove', pt[0], pt[1]);
});

Expand Down
Loading