mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-06 01:47:48 +00:00
Cleanup _.debounce.
This commit is contained in:
41
lodash.js
41
lodash.js
@@ -9030,13 +9030,13 @@
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
function leadingEdge() {
|
function leadingEdge(time) {
|
||||||
// Reset any `maxWait` timer.
|
// Reset any `maxWait` timer.
|
||||||
lastInvokeTime = lastCallTime;
|
lastInvokeTime = time;
|
||||||
// Start the timer to the trailing edge.
|
// Start the timer for the trailing edge.
|
||||||
timerId = setTimeout(timerExpired, wait);
|
timerId = setTimeout(timerExpired, wait);
|
||||||
// Invoke the leading edge.
|
// Invoke the leading edge.
|
||||||
return leading ? invokeFunc(lastCallTime) : result;
|
return leading ? invokeFunc(time) : result;
|
||||||
}
|
}
|
||||||
|
|
||||||
function trailingEdge(time) {
|
function trailingEdge(time) {
|
||||||
@@ -9044,8 +9044,8 @@
|
|||||||
clearTimeout(timerId);
|
clearTimeout(timerId);
|
||||||
timerId = undefined;
|
timerId = undefined;
|
||||||
}
|
}
|
||||||
// Only invoke if we have `lastArgs`, which means there has been a call
|
// Only invoke if we have `lastArgs` which means `func` has been
|
||||||
// to `func` since the last invocation
|
// debounced at least once.
|
||||||
if (trailing && lastArgs) {
|
if (trailing && lastArgs) {
|
||||||
return invokeFunc(time);
|
return invokeFunc(time);
|
||||||
}
|
}
|
||||||
@@ -9054,24 +9054,21 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function checkTimes(time) {
|
function checkTimes(time) {
|
||||||
var timeSinceLastInvoke = time - lastInvokeTime,
|
var timeSinceLastCall = time - lastCallTime,
|
||||||
waitTime = time - lastCallTime;
|
timeSinceLastInvoke = time - lastInvokeTime;
|
||||||
|
|
||||||
if (waitTime >= wait) {
|
// Either activity has stopped and we're at the trailing edge or the system
|
||||||
// Activity has stopped. We are at the trailing edge.
|
// time has gone backwards and we're treating it as the trailing edge.
|
||||||
|
if (timeSinceLastCall >= wait || timeSinceLastCall < 0) {
|
||||||
trailingEdge(time);
|
trailingEdge(time);
|
||||||
return;
|
return null;
|
||||||
}
|
}
|
||||||
if (waitTime < 0) {
|
var remainingWait = wait - timeSinceLastCall,
|
||||||
// The system time has gone backwards. Treat it as the trailing edge.
|
shouldInvoke = false;
|
||||||
trailingEdge(time);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var shouldInvoke = (maxWait !== false && timeSinceLastInvoke >= maxWait);
|
|
||||||
|
|
||||||
// Restart the timer to the smaller of remaining maxWait and remaining wait.
|
// Restart the timer to the smaller of remaining `wait` and `maxWait`.
|
||||||
var remainingWait = wait - waitTime;
|
|
||||||
if (maxWait !== false) {
|
if (maxWait !== false) {
|
||||||
|
shouldInvoke = timeSinceLastInvoke >= maxWait;
|
||||||
remainingWait = nativeMin(remainingWait, maxWait - timeSinceLastInvoke);
|
remainingWait = nativeMin(remainingWait, maxWait - timeSinceLastInvoke);
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
@@ -9085,7 +9082,7 @@
|
|||||||
check = checkTimes(time);
|
check = checkTimes(time);
|
||||||
|
|
||||||
timerId = undefined;
|
timerId = undefined;
|
||||||
if (check !== undefined) {
|
if (check) {
|
||||||
// Restart the timer.
|
// Restart the timer.
|
||||||
timerId = setTimeout(timerExpired, check.remainingWait);
|
timerId = setTimeout(timerExpired, check.remainingWait);
|
||||||
if (check.shouldInvoke) {
|
if (check.shouldInvoke) {
|
||||||
@@ -9111,9 +9108,9 @@
|
|||||||
lastCallTime = now();
|
lastCallTime = now();
|
||||||
|
|
||||||
if (timerId === undefined) {
|
if (timerId === undefined) {
|
||||||
return leadingEdge();
|
return leadingEdge(lastCallTime);
|
||||||
}
|
}
|
||||||
// Check the current times to handle invocations in a tight loop.
|
// Check times to handle invocations in a tight loop.
|
||||||
var check = checkTimes(lastCallTime);
|
var check = checkTimes(lastCallTime);
|
||||||
return (check && check.shouldInvoke)
|
return (check && check.shouldInvoke)
|
||||||
? invokeFunc(lastCallTime)
|
? invokeFunc(lastCallTime)
|
||||||
|
|||||||
Reference in New Issue
Block a user