Skip to content

Commit 80508e8

Browse files
committed
throttle.done method and test updates
1 parent 256c5c7 commit 80508e8

File tree

5 files changed

+309
-282
lines changed

5 files changed

+309
-282
lines changed

src/lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ lib.counterRegex = regexModule.counter;
9999

100100
var throttleModule = require('./throttle');
101101
lib.throttle = throttleModule.throttle;
102+
lib.throttleDone = throttleModule.done;
102103
lib.clearThrottle = throttleModule.clear;
103104

104105
lib.getGraphDiv = require('./get_graph_div');

src/lib/throttle.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,40 @@ exports.throttle = function throttle(callback, minInterval, id) {
4343

4444
_clearTimeout(cache);
4545

46-
if(now > cache.ts + minInterval) {
46+
function exec() {
4747
callback();
48-
cache.ts = now;
48+
cache.ts = Date.now();
49+
if(cache.onDone) {
50+
cache.onDone();
51+
cache.onDone = null;
52+
}
53+
}
54+
55+
if(now > cache.ts + minInterval) {
56+
exec();
4957
return;
5058
}
5159

5260
cache.timer = setTimeout(function() {
53-
callback();
54-
cache.ts = Date.now();
61+
exec();
5562
cache.timer = null;
5663
}, minInterval);
5764
};
5865

66+
exports.done = function(id) {
67+
var cache = timerCache[id];
68+
if(!cache || !cache.timer) return Promise.resolve();
69+
70+
return new Promise(function(resolve) {
71+
var previousOnDone = cache.onDone;
72+
cache.onDone = function onDone() {
73+
if(previousOnDone) previousOnDone();
74+
resolve();
75+
cache.onDone = null;
76+
};
77+
});
78+
};
79+
5980
/**
6081
* Clear the throttle cache for one or all timers
6182
* @param {optional string} id:

src/plots/cartesian/select.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -208,21 +208,23 @@ module.exports = function prepSelect(e, startX, startY, dragOptions, mode) {
208208

209209
dragOptions.doneFn = function(dragged, numclicks) {
210210
corners.remove();
211-
throttle.clear(throttleID);
212-
213-
if(!dragged && numclicks === 2) {
214-
// clear selection on doubleclick
215-
outlines.remove();
216-
for(i = 0; i < searchTraces.length; i++) {
217-
searchInfo = searchTraces[i];
218-
searchInfo.selectPoints(searchInfo, false);
219-
}
211+
throttle.done(throttleID).then(function() {
212+
throttle.clear(throttleID);
220213

221-
gd.emit('plotly_deselect', null);
222-
}
223-
else {
224-
dragOptions.gd.emit('plotly_selected', eventData);
225-
}
214+
if(!dragged && numclicks === 2) {
215+
// clear selection on doubleclick
216+
outlines.remove();
217+
for(i = 0; i < searchTraces.length; i++) {
218+
searchInfo = searchTraces[i];
219+
searchInfo.selectPoints(searchInfo, false);
220+
}
221+
222+
gd.emit('plotly_deselect', null);
223+
}
224+
else {
225+
dragOptions.gd.emit('plotly_selected', eventData);
226+
}
227+
});
226228
};
227229
};
228230

test/jasmine/tests/gl2d_click_test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,10 +496,12 @@ describe('@noCI Test gl2d lasso/select:', function() {
496496
function drag(path) {
497497
var len = path.length;
498498

499+
Lib.clearThrottle();
499500
mouseEvent('mousemove', path[0][0], path[0][1]);
500501
mouseEvent('mousedown', path[0][0], path[0][1]);
501502

502503
path.slice(1, len).forEach(function(pt) {
504+
Lib.clearThrottle();
503505
mouseEvent('mousemove', pt[0], pt[1]);
504506
});
505507

0 commit comments

Comments
 (0)