Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benchmarks/bundle-size/debounce.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
});
});
2 changes: 1 addition & 1 deletion benchmarks/bundle-size/throttle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
});
});
24 changes: 24 additions & 0 deletions src/compat/function/debounce.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
5 changes: 4 additions & 1 deletion src/compat/function/debounce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,10 @@ export function debounce<F extends (...args: any[]) => any>(
}

if (Date.now() - pendingAt >= maxWait) {
result = func.apply(this, args);
if (leading || trailing) {
result = func.apply(this, args);
}

pendingAt = Date.now();

_debounced.cancel();
Expand Down
24 changes: 24 additions & 0 deletions src/compat/function/throttle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading