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 @@ -10,6 +10,7 @@ The returned function has the following methods:
- `.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.
- `.isDebouncing()` checks if the debounce delay is still active.
*/
declare function debounce<F extends AnyFunction>(
function_: F,
Expand All @@ -23,6 +24,7 @@ declare namespace debounce {
clear(): void;
flush(): void;
trigger(): void;
isDebouncing(): boolean;
};
}

Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ function debounce(function_, wait = 100, options = {}) {
debounced.clear();
};

debounced.isDebouncing = () => Boolean(timeoutId);
vadim-isakov marked this conversation as resolved.
Show resolved Hide resolved

return debounced;
}

Expand Down
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ To execute immediately and reset the timer if it was previously set:
window.onresize.trigger();
```

To checks if the debounce delay is still active:

```js
window.onresize.isDebouncing();
```

## API

### debounce(fn, wait, options?)
Expand All @@ -54,6 +60,7 @@ The returned function has the following methods:
- `.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.
- `.isDebouncing()` checks if the debounce delay is still active.

## Related

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.isDebouncing(), false, 'Callback should return false for first execution');

fn();
assert.strictEqual(fn.isDebouncing(), true, 'Callback should return true within debounce delay');

fn.trigger();
assert.strictEqual(fn.isDebouncing(), false, 'Callback should return false after debounce triggered');
});