From 1151e14ecddcda689eec9f2827cde8fc3fcdb640 Mon Sep 17 00:00:00 2001 From: Vadim Isakov Date: Mon, 14 Oct 2024 18:57:17 +0300 Subject: [PATCH] Add `.isPending` property to the debounced function (#44) Co-authored-by: Sindre Sorhus --- index.d.ts | 2 ++ index.js | 6 ++++++ readme.md | 7 +++++++ test.js | 12 ++++++++++++ 4 files changed, 27 insertions(+) diff --git a/index.d.ts b/index.d.ts index 8edf0c3..24c2478 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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. @@ -20,6 +21,7 @@ declare function debounce( declare namespace debounce { type DebouncedFunction = { (...arguments_: Parameters): ReturnType | undefined; + readonly isPending: boolean; clear(): void; flush(): void; trigger(): void; diff --git a/index.js b/index.js index 21e4513..4dfdcb9 100644 --- a/index.js +++ b/index.js @@ -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; diff --git a/readme.md b/readme.md index 0e9e8e2..6c07a52 100644 --- a/readme.md +++ b/readme.md @@ -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 @@ -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. diff --git a/test.js b/test.js index 26f89dd..379581e 100644 --- a/test.js +++ b/test.js @@ -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'); +});