Skip to content

Commit

Permalink
Make React Refresh debounce call on the leading edge (#8593)
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett authored Nov 2, 2022
1 parent 3480705 commit 225face
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions packages/transformers/react-refresh-wrap/src/helpers/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,25 @@ function debounce(func, delay) {
func.call(null, args);
};
} else {
var timeout = undefined;
let timeout = undefined;
let lastTime = 0;
return function (args) {
clearTimeout(timeout);
timeout = setTimeout(function () {
timeout = undefined;
// Call immediately if last call was more than the delay ago.
// Otherwise, set a timeout. This means the first call is fast
// (for the common case of a single update), and subsequent updates
// are batched.
let now = Date.now();
if (now - lastTime > delay) {
lastTime = now;
func.call(null, args);
}, delay);
} else {
clearTimeout(timeout);
timeout = setTimeout(function () {
timeout = undefined;
lastTime = Date.now();
func.call(null, args);
}, delay);
}
};
}
}
Expand Down

0 comments on commit 225face

Please sign in to comment.