Skip to content

Commit

Permalink
Add .isPending property to the debounced function (#44)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
vadim-isakov and sindresorhus authored Oct 14, 2024
1 parent dc9d9ec commit 1151e14
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
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');
});

0 comments on commit 1151e14

Please sign in to comment.