diff --git a/benchmarks/bundle-size/debounce.spec.ts b/benchmarks/bundle-size/debounce.spec.ts index ac3dcbbf9..f73112638 100644 --- a/benchmarks/bundle-size/debounce.spec.ts +++ b/benchmarks/bundle-size/debounce.spec.ts @@ -14,6 +14,6 @@ describe('debounce bundle size', () => { it('es-toolkit/compat', async () => { const bundleSize = await getBundleSize('es-toolkit/compat', 'debounce'); - expect(bundleSize).toMatchInlineSnapshot(`961`); + expect(bundleSize).toMatchInlineSnapshot(`971`); }); }); diff --git a/benchmarks/bundle-size/throttle.spec.ts b/benchmarks/bundle-size/throttle.spec.ts index b7e81b7c4..0ea3c5cb2 100644 --- a/benchmarks/bundle-size/throttle.spec.ts +++ b/benchmarks/bundle-size/throttle.spec.ts @@ -14,6 +14,6 @@ describe('throttle bundle size', () => { it('es-toolkit/compat', async () => { const bundleSize = await getBundleSize('es-toolkit/compat', 'throttle'); - expect(bundleSize).toMatchInlineSnapshot(`1065`); + expect(bundleSize).toMatchInlineSnapshot(`1075`); }); }); diff --git a/src/compat/function/debounce.spec.ts b/src/compat/function/debounce.spec.ts index f4eb11b1f..e5b881826 100644 --- a/src/compat/function/debounce.spec.ts +++ b/src/compat/function/debounce.spec.ts @@ -557,4 +557,28 @@ describe('debounce', () => { await delay(64); expect(callCount).toBe(0); }); + + it('should not invoke the function with `maxWait` when both `leading` and `trailing` are `false`', async () => { + let callCount = 0; + + const debounced = debounce( + () => { + callCount++; + }, + 64, + { leading: false, trailing: false, maxWait: 64 } + ); + + debounced(); + debounced(); + + await delay(96); + + debounced(); + debounced(); + + await delay(96); + + expect(callCount).toBe(0); + }); }); diff --git a/src/compat/function/debounce.ts b/src/compat/function/debounce.ts index d137c2114..2273124ee 100644 --- a/src/compat/function/debounce.ts +++ b/src/compat/function/debounce.ts @@ -198,7 +198,10 @@ export function debounce any>( } if (Date.now() - pendingAt >= maxWait) { - result = func.apply(this, args); + if (leading || trailing) { + result = func.apply(this, args); + } + pendingAt = Date.now(); _debounced.cancel(); diff --git a/src/compat/function/throttle.spec.ts b/src/compat/function/throttle.spec.ts index addd72d2c..9cd437337 100644 --- a/src/compat/function/throttle.spec.ts +++ b/src/compat/function/throttle.spec.ts @@ -348,6 +348,30 @@ describe('throttle', () => { expect(fn).toHaveBeenCalledTimes(4); }); + it('should not invoke the function when both `leading` and `trailing` are `false`', async () => { + let callCount = 0; + + const throttled = throttle( + () => { + callCount++; + }, + 64, + { leading: false, trailing: false } + ); + + throttled(); + throttled(); + + await delay(96); + + throttled(); + throttled(); + + await delay(96); + + expect(callCount).toBe(0); + }); + it('should not invoke the function even after flush is called if timer is going', async () => { let callCount = 0; const throttled = throttle(() => ++callCount, 32);