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

Add maxWait option #12

Merged
merged 8 commits into from
Feb 28, 2021
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
9 changes: 9 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ declare namespace debounceFn {
*/
readonly wait?: number;

/**
Maximum time to wait until the `input` function is called.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It needs a clearer description of how it's different from wait.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would also be useful to include a use-case for this option (like you mention in the PR description).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You also need to update the readme.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, make it clear that "time" is in milliseconds.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tried to improve this. I'm not sure the first line is any better, but I don't how to make it better

Only applies when after is true.
Disabled when 0

@default 0
*/
readonly maxWait?: number;

/**
Trigger the function on the leading edge of the `wait` interval.

Expand Down
27 changes: 27 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = (inputFunction, options = {}) => {

const {
wait = 0,
maxWait = 0,
before = false,
after = true
} = options;
Expand All @@ -17,6 +18,7 @@ module.exports = (inputFunction, options = {}) => {
}

let timeout;
let maxTimeout;
let result;

const debouncedFunction = function (...arguments_) {
Expand All @@ -25,15 +27,35 @@ module.exports = (inputFunction, options = {}) => {
const later = () => {
timeout = undefined;

if (maxTimeout) {
clearTimeout(maxTimeout);
maxTimeout = undefined;
}

if (after) {
result = inputFunction.apply(context, arguments_);
}
};

const maxLater = () => {
maxTimeout = undefined;

if (timeout) {
clearTimeout(timeout);
timeout = undefined;
}

result = inputFunction.apply(context, arguments_);
};

const shouldCallNow = before && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);

if (maxWait > 0 && !maxTimeout && after) {
maxTimeout = setTimeout(maxLater, maxWait);
}

if (shouldCallNow) {
result = inputFunction.apply(context, arguments_);
}
Expand All @@ -48,6 +70,11 @@ module.exports = (inputFunction, options = {}) => {
clearTimeout(timeout);
timeout = undefined;
}

if (maxTimeout) {
clearTimeout(maxTimeout);
maxTimeout = undefined;
}
};

return debouncedFunction;
Expand Down
25 changes: 25 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,28 @@ test('.cancel() method', async t => {
t.is(debounced(3), undefined);
t.is(count, 0);
});

test('debounces a function with maxWait', async t => {
let count = 0;

const debounced = debounceFn(value => {
count++;
return value;
}, {
wait: 40,
maxWait: 50
});

t.is(debounced(1), undefined);
await delay(30);
t.is(count, 0);

t.is(debounced(2), undefined);
await delay(30);
t.is(count, 1);

t.is(debounced(3), 1);

await delay(200);
t.is(count, 2);
});