-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Throttle selectPoints #2040
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Throttle selectPoints #2040
Changes from 1 commit
a6df912
aa434dd
753b2c9
256c5c7
80508e8
0861736
1968af7
6da3cfa
b6d64be
811073e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* Copyright 2012-2017, Plotly, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
/** | ||
* Allow referencing a graph DOM element either directly | ||
* or its id string | ||
* | ||
* @param {HTMLDivElement|string} gd: a graph element or its id | ||
* | ||
* @returns {HTMLDivElement} the DOM element of the graph | ||
*/ | ||
// Get the container div: we store all variables for this plot as | ||
// properties of this div | ||
// some callers send this in by DOM element, others by id (string) | ||
module.exports = function(gd) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
var gdElement; | ||
|
||
if(typeof gd === 'string') { | ||
gdElement = document.getElementById(gd); | ||
|
||
if(gdElement === null) { | ||
throw new Error('No DOM element with id \'' + gd + '\' exists on the page.'); | ||
} | ||
|
||
return gdElement; | ||
} | ||
else if(gd === null || gd === undefined) { | ||
throw new Error('DOM element provided is null or undefined'); | ||
} | ||
|
||
return gd; // otherwise assume that gd is a DOM element | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* Copyright 2012-2017, Plotly, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var timerCache = {}; | ||
|
||
/** | ||
* Throttle a callback. `callback` executes synchronously only if | ||
* more than `minInterval` milliseconds have already elapsed since the latest | ||
* call (if any). Otherwise we wait until `minInterval` is over and execute the | ||
* last callback received while waiting. | ||
* So the first and last events in a train are always executed (eventually) | ||
* but some of the events in the middle can be dropped. | ||
* | ||
* @param {function} callback: the function to throttle | ||
* @param {string} id: an identifier to mark events to throttle together | ||
* @param {number} minInterval: minimum time, in milliseconds, between | ||
* invocations of `callback` | ||
*/ | ||
exports.throttle = function throttle(callback, minInterval, id) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -> b6d64be |
||
var cache = timerCache[id]; | ||
var now = Date.now(); | ||
|
||
if(!cache) { | ||
/* | ||
* Throw out old items before making a new one, to prevent the cache | ||
* getting overgrown, for example from old plots that have been replaced. | ||
* 1 minute age is arbitrary. | ||
*/ | ||
for(var idi in timerCache) { | ||
if(timerCache[idi].ts < now - 60000) { | ||
delete timerCache[idi]; | ||
} | ||
} | ||
cache = timerCache[id] = {ts: 0, timer: null}; | ||
} | ||
|
||
_clearTimeout(cache); | ||
|
||
if(now > cache.ts + minInterval) { | ||
callback(); | ||
cache.ts = now; | ||
return; | ||
} | ||
|
||
cache.timer = setTimeout(function() { | ||
callback(); | ||
cache.ts = Date.now(); | ||
cache.timer = null; | ||
}, minInterval); | ||
}; | ||
|
||
/** | ||
* Clear the throttle cache for one or all timers | ||
* @param {optional string} id: | ||
* if provided, clear just this timer | ||
* if omitted, clear all timers (mainly useful for testing) | ||
*/ | ||
exports.clear = function(id) { | ||
if(id) { | ||
_clearTimeout(timerCache[id]); | ||
delete timerCache[id]; | ||
} | ||
else { | ||
for(var idi in timerCache) exports.clear(idi); | ||
} | ||
}; | ||
|
||
function _clearTimeout(cache) { | ||
if(cache && cache.timer !== null) { | ||
clearTimeout(cache.timer); | ||
cache.timer = null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1327,8 +1327,6 @@ plots.purge = function(gd) { | |
delete gd.hmlumcount; | ||
delete gd.hmpixcount; | ||
delete gd.numboxes; | ||
delete gd._hoverTimer; | ||
delete gd._lastHoverTime; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎉 |
||
delete gd._transitionData; | ||
delete gd._transitioning; | ||
delete gd._initialAutoSize; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -446,7 +446,7 @@ describe('Event data:', function() { | |
function _hover(px, py) { | ||
return new Promise(function(resolve, reject) { | ||
gd.once('plotly_hover', function(d) { | ||
delete gd._lastHoverTime; | ||
Lib.clearThrottle(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Smooth ⛵ |
||
resolve(d); | ||
}); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using
fullLayout._uid
to construct the key to what we're throttling means we can 🔪gd._lastHoverTime
andgd._hoverTimer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Loving this ❤️