replace ratelimitedfunc with lodash impl
This commit is contained in:
parent
80731a9de4
commit
f34855573e
1 changed files with 16 additions and 42 deletions
|
@ -20,54 +20,28 @@ limitations under the License.
|
||||||
* to update the interface once for all of them.
|
* to update the interface once for all of them.
|
||||||
*
|
*
|
||||||
* Note that the function must not take arguments, since the args
|
* Note that the function must not take arguments, since the args
|
||||||
* could be different for each invocarion of the function.
|
* could be different for each invocation of the function.
|
||||||
*
|
*
|
||||||
* The returned function has a 'cancelPendingCall' property which can be called
|
* The returned function has a 'cancelPendingCall' property which can be called
|
||||||
* on unmount or similar to cancel any pending update.
|
* on unmount or similar to cancel any pending update.
|
||||||
*/
|
*/
|
||||||
module.exports = function(f, minIntervalMs) {
|
|
||||||
this.lastCall = 0;
|
|
||||||
this.scheduledCall = undefined;
|
|
||||||
|
|
||||||
const self = this;
|
import { throttle } from "lodash";
|
||||||
const wrapper = function() {
|
|
||||||
const now = Date.now();
|
|
||||||
|
|
||||||
if (self.lastCall < now - minIntervalMs) {
|
export default function ratelimitedfunc(fn, time) {
|
||||||
f.apply(this);
|
const throttledFn = throttle(fn, time, {
|
||||||
// get the time again now the function has finished, so if it
|
leading: true,
|
||||||
// took longer than the delay time to execute, it doesn't
|
trailing: true,
|
||||||
// immediately become eligible to run again.
|
});
|
||||||
self.lastCall = Date.now();
|
const _bind = throttledFn.bind;
|
||||||
} else if (self.scheduledCall === undefined) {
|
throttledFn.bind = function() {
|
||||||
self.scheduledCall = setTimeout(
|
const boundFn = _bind.apply(throttledFn, arguments);
|
||||||
() => {
|
boundFn.cancelPendingCall = throttledFn.cancelPendingCall;
|
||||||
self.scheduledCall = undefined;
|
return boundFn;
|
||||||
f.apply(this);
|
};
|
||||||
// get time again as per above
|
|
||||||
self.lastCall = Date.now();
|
throttledFn.cancelPendingCall = function() {
|
||||||
},
|
throttledFn.cancel();
|
||||||
(self.lastCall + minIntervalMs) - now,
|
};
|
||||||
);
|
return throttledFn;
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
// add the cancelPendingCall property
|
|
||||||
wrapper.cancelPendingCall = function() {
|
|
||||||
if (self.scheduledCall) {
|
|
||||||
clearTimeout(self.scheduledCall);
|
|
||||||
self.scheduledCall = undefined;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// make sure that cancelPendingCall is copied when react rebinds the
|
|
||||||
// wrapper
|
|
||||||
const _bind = wrapper.bind;
|
|
||||||
wrapper.bind = function() {
|
|
||||||
const rebound = _bind.apply(this, arguments);
|
|
||||||
rebound.cancelPendingCall = wrapper.cancelPendingCall;
|
|
||||||
return rebound;
|
|
||||||
};
|
|
||||||
|
|
||||||
return wrapper;
|
|
||||||
};
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue