Skip to content

Commit

Permalink
Merge pull request #563 from Kitware/add-throttle-helper
Browse files Browse the repository at this point in the history
fix(macro): add throttle helper function
  • Loading branch information
jourdain authored Feb 6, 2018
2 parents 858c29a + 352c747 commit bca9fd9
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Sources/macro.js
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,35 @@ export function debounce(func, wait, immediate) {
};
}

// ----------------------------------------------------------------------------
// Creates a throttled function that only invokes `func` at most once per
// every `wait` milliseconds.

export function throttle(callback, delay) {
let isThrottled = false;
let argsToUse = null;

function next() {
isThrottled = false;
if (argsToUse !== null) {
wrapper(...argsToUse); // eslint-disable-line
argsToUse = null;
}
}

function wrapper(...args) {
if (isThrottled) {
argsToUse = args;
return;
}
isThrottled = true;
callback(...args);
setTimeout(next, delay);
}

return wrapper;
}

// ----------------------------------------------------------------------------
// keystore(publicAPI, model, initialKeystore)
//
Expand Down Expand Up @@ -1265,6 +1294,7 @@ export default {
vtkLogMacro,
vtkWarningMacro,
debounce,
throttle,
proxy,
proxyPropertyMapping,
proxyPropertyState,
Expand Down

0 comments on commit bca9fd9

Please sign in to comment.