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 debounce state check function #44

Merged
merged 7 commits into from
Oct 14, 2024
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Set the `immediate` option to `true` to execute the function immediately at the

The returned function has the following methods:

- `.isPending` indicates whether the debounce delay is currently active.
- `.clear()` cancels any scheduled executions.
- `.flush()` if an execution is scheduled then it will be immediately executed and the timer will be cleared.
- `.trigger()` executes the function immediately and clears the timer if it was previously set.
Expand All @@ -20,6 +21,7 @@ declare function debounce<F extends AnyFunction>(
declare namespace debounce {
type DebouncedFunction<F extends AnyFunction> = {
(...arguments_: Parameters<F>): ReturnType<F> | undefined;
readonly isPending: boolean;
clear(): void;
flush(): void;
trigger(): void;
Expand Down
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ function debounce(function_, wait = 100, options = {}) {
return result;
};

Object.defineProperty(debounced, 'isPending', {
get() {
return timeoutId !== undefined;
},
});

debounced.clear = () => {
if (!timeoutId) {
return;
Expand Down
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ window.onresize = debounce(resize, 200);

*(You can also use `const debounce = require('debounce')`)*

To check if the debounce delay is currently active:

```js
window.onresize.isPending;
```

To later clear the timer and cancel currently scheduled executions:

```js
Expand Down Expand Up @@ -51,6 +57,7 @@ Set the `immediate` option to `true` to execute the function immediately at the

The returned function has the following methods:

- `.isPending` indicates whether the debounce delay is currently active.
- `.clear()` cancels any scheduled executions.
- `.flush()` if an execution is scheduled then it will be immediately executed and the timer will be cleared.
- `.trigger()` executes the function immediately and clears the timer if it was previously set.
Expand Down
12 changes: 12 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,3 +449,15 @@ test('calling the trigger should not affect future function calls', async () =>

clock.restore();
});

test('should correctly handle debounce state transitions', async () => {
const callback = sinon.spy();
const fn = debounce(callback, 100);
assert.strictEqual(fn.isPending, false, 'isPending should be false for first execution');

fn();
assert.strictEqual(fn.isPending, true, 'isPending should be true within debounce delay');

fn.trigger();
assert.strictEqual(fn.isPending, false, 'isPending should be false after debounce triggered');
});