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

Reimplementing timer optimization #1269 #2892

Merged
merged 5 commits into from
Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions modules/debounce.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,41 @@
import restArguments from './restArguments.js';
import now from './now.js';

// When a sequence of calls of the returned function ends, the argument
// function is triggered. The end of a sequence is defined by the `wait`
// parameter. If `immediate` is passed, the argument function will be
// triggered at the beginning of the sequence instead of at the end.
export default function debounce(func, wait, immediate) {
var timeout, timestamp, args, result, context;
var timeout, previous, args, result, context;

var later = function() {
var last = now() - timestamp;
if (wait > last) {
timeout = setTimeout(later, wait - last);
var passed = now() - previous;
if (wait >= passed) {
kritollm marked this conversation as resolved.
Show resolved Hide resolved
timeout = setTimeout(later, wait - passed);

kritollm marked this conversation as resolved.
Show resolved Hide resolved
} else {
timeout = null;
if (!immediate) result = func.apply(context, args);
// This check is needed because the argument function can recursively invoke debounced
if (!timeout) args = context = null;
}
};
// Remove timer for immediate, samme as for throttle
var debounced = function() {
var callNow = immediate && !timeout;

var debounced = restArguments(function(_args) {
context = this;
args = [].slice.call(arguments, 0);
timestamp = now();
if (!timeout) timeout = setTimeout(later, wait);
if (callNow) result = func.apply(context, args);
args = _args;
previous = now();
if (!timeout) {
timeout = setTimeout(later, wait);
if (immediate) result = func.apply(context, args);
}
return result;
}
});

debounced.cancel = function() {
clearTimeout(timeout);
timeout = null;
timeout = args = context = null;
};

return debounced;
}
}
jgonggrijp marked this conversation as resolved.
Show resolved Hide resolved
9 changes: 5 additions & 4 deletions modules/throttle.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default function throttle(func, wait, options) {
previous = options.leading === false ? 0 : now();
timeout = null;
result = func.apply(context, args);
if (!timeout) context = args = null;
};

var throttled = function() {
Expand All @@ -22,14 +23,14 @@ export default function throttle(func, wait, options) {
var remaining = wait - (_now - previous);
context = this;
args = arguments;
if (remaining <= 0) {
if (remaining <= 0 || remaining > wait) {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
previous = _now;
result = func.apply(context, args);
//if (!timeout) context = args = null;
if (!timeout) context = args = null;
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining);
}
Expand All @@ -39,8 +40,8 @@ export default function throttle(func, wait, options) {
throttled.cancel = function() {
clearTimeout(timeout);
previous = 0;
timeout = null;
timeout = context = args = null;
};

return throttled;
}
}
kritollm marked this conversation as resolved.
Show resolved Hide resolved
38 changes: 22 additions & 16 deletions underscore-esm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion underscore-esm.js.map

Large diffs are not rendered by default.

38 changes: 22 additions & 16 deletions underscore.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion underscore.js.map

Large diffs are not rendered by default.