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 2 commits
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
13 changes: 12 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
declare namespace debounceFn {
interface Options {
/**
Time to wait until the `input` function is called.
Time in milliseconds to wait until the `input` function is called.

@default 0
*/
readonly wait?: number;

/**
Maximum time in milliseconds to wait between calls to the `input` function.
Disabled when 0

This can be used to limit the number of calls handled in a constant stream.
For example, a media player sending updates every few milliseconds but wants to be handled only once a second.

@default 0
Julusian marked this conversation as resolved.
Show resolved Hide resolved
*/
readonly maxWait?: number;

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

Expand Down
29 changes: 29 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,6 +27,24 @@ 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;
}

if (after) {
result = inputFunction.apply(context, arguments_);
}
Expand All @@ -34,6 +54,10 @@ module.exports = (inputFunction, options = {}) => {
clearTimeout(timeout);
timeout = setTimeout(later, wait);

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

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

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

return debouncedFunction;
Expand Down
10 changes: 10 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ Default: `0`

Time to wait until the `input` function is called.

##### maxWait

Type: `number`\
Default: `0` (disabled)
Copy link
Owner

Choose a reason for hiding this comment

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

Readme and index.d.ts should be in sync.


Maximum time in milliseconds to wait between calls to the `input` function.

This can be used to limit the number of calls handled in a constant stream.
For example, a media player sending updates every few milliseconds but wants to be handled only once a second.

##### before

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

test('before:false after:true `maxWait` option', async t => {
let count = 0;

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

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);
});

test('before:true after:false `maxWait` option', async t => {
Julusian marked this conversation as resolved.
Show resolved Hide resolved
let count = 0;

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

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

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

t.is(debounced(3), 3);
t.is(count, 2);

await delay(50);

t.is(debounced(4), 4);
t.is(count, 3);
});