-
-
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
Merged
Merged
Throttle selectPoints #2040
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a6df912
generalized event throttling routine
alexcjohnson aa434dd
move getGraphDiv to lib so we can use it outside plot_api
alexcjohnson 753b2c9
unwrap [].push.apply
alexcjohnson 256c5c7
throttle just the selectPoints part of box/lasso selection
alexcjohnson 80508e8
throttle.done method and test updates
alexcjohnson 0861736
fix docs for getGraphDiv
alexcjohnson 1968af7
Merge branch 'master' into throttle-select
alexcjohnson 6da3cfa
remove silently broken and obsolete select box node creation tests
alexcjohnson b6d64be
alter argument order of throttle.throttle
alexcjohnson 811073e
clean up geo_test for new throttle
alexcjohnson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* 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 by its id string | ||
* | ||
* @param {HTMLDivElement|string} gd: a graph element or its id | ||
* | ||
* @returns {HTMLDivElement} the DOM element of the graph | ||
*/ | ||
module.exports = function(gd) { | ||
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 | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/** | ||
* 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 {string} id: an identifier to mark events to throttle together | ||
* @param {number} minInterval: minimum time, in milliseconds, between | ||
* invocations of `callback` | ||
* @param {function} callback: the function to throttle. `callback` itself | ||
* should be a purely synchronous function. | ||
*/ | ||
exports.throttle = function throttle(id, minInterval, callback) { | ||
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); | ||
|
||
function exec() { | ||
callback(); | ||
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. So 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 |
||
cache.ts = Date.now(); | ||
if(cache.onDone) { | ||
cache.onDone(); | ||
cache.onDone = null; | ||
} | ||
} | ||
|
||
if(now > cache.ts + minInterval) { | ||
exec(); | ||
return; | ||
} | ||
|
||
cache.timer = setTimeout(function() { | ||
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: | ||
* 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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
NIce 🌴