Skip to content
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

Make React Refresh debounce call on the leading edge #8593

Merged
merged 1 commit into from Nov 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 17 additions & 5 deletions packages/transformers/react-refresh-wrap/src/helpers/helpers.js
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