Skip to content

Commit 1151e14

Browse files
Add .isPending property to the debounced function (#44)
Co-authored-by: Sindre Sorhus <[email protected]>
1 parent dc9d9ec commit 1151e14

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

index.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Set the `immediate` option to `true` to execute the function immediately at the
77
88
The returned function has the following methods:
99
10+
- `.isPending` indicates whether the debounce delay is currently active.
1011
- `.clear()` cancels any scheduled executions.
1112
- `.flush()` if an execution is scheduled then it will be immediately executed and the timer will be cleared.
1213
- `.trigger()` executes the function immediately and clears the timer if it was previously set.
@@ -20,6 +21,7 @@ declare function debounce<F extends AnyFunction>(
2021
declare namespace debounce {
2122
type DebouncedFunction<F extends AnyFunction> = {
2223
(...arguments_: Parameters<F>): ReturnType<F> | undefined;
24+
readonly isPending: boolean;
2325
clear(): void;
2426
flush(): void;
2527
trigger(): void;

index.js

+6
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ function debounce(function_, wait = 100, options = {}) {
6565
return result;
6666
};
6767

68+
Object.defineProperty(debounced, 'isPending', {
69+
get() {
70+
return timeoutId !== undefined;
71+
},
72+
});
73+
6874
debounced.clear = () => {
6975
if (!timeoutId) {
7076
return;

readme.md

+7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ window.onresize = debounce(resize, 200);
2323

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

26+
To check if the debounce delay is currently active:
27+
28+
```js
29+
window.onresize.isPending;
30+
```
31+
2632
To later clear the timer and cancel currently scheduled executions:
2733

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

5258
The returned function has the following methods:
5359

60+
- `.isPending` indicates whether the debounce delay is currently active.
5461
- `.clear()` cancels any scheduled executions.
5562
- `.flush()` if an execution is scheduled then it will be immediately executed and the timer will be cleared.
5663
- `.trigger()` executes the function immediately and clears the timer if it was previously set.

test.js

+12
Original file line numberDiff line numberDiff line change
@@ -449,3 +449,15 @@ test('calling the trigger should not affect future function calls', async () =>
449449

450450
clock.restore();
451451
});
452+
453+
test('should correctly handle debounce state transitions', async () => {
454+
const callback = sinon.spy();
455+
const fn = debounce(callback, 100);
456+
assert.strictEqual(fn.isPending, false, 'isPending should be false for first execution');
457+
458+
fn();
459+
assert.strictEqual(fn.isPending, true, 'isPending should be true within debounce delay');
460+
461+
fn.trigger();
462+
assert.strictEqual(fn.isPending, false, 'isPending should be false after debounce triggered');
463+
});

0 commit comments

Comments
 (0)