
Security News
PodRocket Podcast: Inside the Recent npm Supply Chain Attacks
Socket CEO Feross Aboukhadijeh discusses the recent npm supply chain attacks on PodRocket, covering novel attack vectors and how developers can protect themselves.
@avatijs/debounce
Advanced tools
A highly configurable debounce utility with TypeScript support, providing features like leading/trailing edge execution, cancellation, immediate flush, maximum wait time, and proper Promise handling.
npm install @avatijs/debounce
import { debounce } from '@your-org/debounce-utility';
// Simple debounce
const debouncedFn = debounce(async (x: number) => x * 2, {
wait: 1000
});
// Call the debounced function
await debouncedFn(5); // Will execute after 1000ms
// With debug logging
const debuggedFn = debounce(async (x: number) => x * 2, {
wait: 1000,
debug: true
});
// With abort controller
const controller = new AbortController();
const abortableFn = debounce(async (x: number) => x * 2, {
wait: 1000,
signal: controller.signal
});
// Cleanup when done
debouncedFn.cleanup();
debounce<T>(func: T, options?: DebounceOptions): DebouncedFunction<T>
Creates a debounced version of the provided function.
func: T
The function to debounce. Can be synchronous or asynchronous.
options: DebounceOptions
Configuration options for the debounced function.
interface DebounceOptions {
readonly wait?: number; // Delay in milliseconds (default: 0)
readonly leading?: boolean; // Execute on leading edge (default: false)
readonly trailing?: boolean; // Execute on trailing edge (default: true)
readonly maxWait?: number; // Maximum time to wait
readonly debug?: boolean; // Enable debug logging (default: false)
readonly signal?: AbortSignal; // AbortController signal
}
Returns a debounced function with the following interface:
interface DebouncedFunction<T> {
(...args: Parameters<T>): Promise<Awaited<ReturnType<T>>>;
readonly cancel: () => void;
readonly flush: (...args: Parameters<T>) => Promise<Awaited<ReturnType<T>>>;
readonly pending: () => boolean;
readonly cleanup: () => void;
}
const leadingDebounce = debounce(
(value: string) => console.log(value),
{
wait: 1000,
leading: true,
trailing: false
}
);
// Executes immediately, then ignores calls for 1000ms
leadingDebounce("First");
leadingDebounce("Second"); // Ignored
leadingDebounce("Third"); // Ignored
const maxWaitDebounce = debounce(
(value: string) => console.log(value),
{
wait: 1000,
maxWait: 5000
}
);
// Will execute after 5000ms maximum, even if called continuously
const interval = setInterval(() => maxWaitDebounce("test"), 100);
const controller = new AbortController();
const abortableDebounce = debounce(
async (value: string) => {
await someAsyncOperation(value);
},
{
wait: 1000,
signal: controller.signal
}
);
// Later, abort all pending operations
controller.abort();
const debugDebounce = debounce(
(value: string) => console.log(value),
{
wait: 1000,
debug: true
}
);
// Will log detailed information about internal state
debugDebounce("test");
const asyncDebounce = debounce(
async (x: number): Promise<number> => {
await delay(100);
return x * 2;
},
{ wait: 1000 }
);
// Get the result
const result = await asyncDebounce(5);
console.log(result); // 10
const debouncedFn = debounce((x: number) => x * 2, { wait: 1000 });
// Use the function
debouncedFn(5);
// Clean up when done
debouncedFn.cleanup();
cleanup()
when you're done with the debounced function to prevent memory leaks:const debouncedFn = debounce(myFunc, { wait: 1000 });
// When done:
debouncedFn.cleanup();
const debouncedFn = debounce(async () => {
try {
await debouncedOperation();
} catch (error) {
// Handle error
}
});
interface MyFuncParams {
id: number;
name: string;
}
const typedDebounce = debounce(
(params: MyFuncParams) => console.log(params),
{ wait: 1000 }
);
// TypeScript will enforce correct parameter types
typedDebounce({ id: 1, name: "test" });
cleanup()
when done can lead to memory leaks.maxWait
less than wait
will throw an error.Contributions are welcome! Please read our contributing guide for details on our code of conduct and the process for submitting pull requests.
This project is licensed under the MIT License - see the LICENSE file for details.
FAQs
Debounce package part of Avati project
We found that @avatijs/debounce demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket CEO Feross Aboukhadijeh discusses the recent npm supply chain attacks on PodRocket, covering novel attack vectors and how developers can protect themselves.
Security News
Maintainers back GitHub’s npm security overhaul but raise concerns about CI/CD workflows, enterprise support, and token management.
Product
Socket Firewall is a free tool that blocks malicious packages at install time, giving developers proactive protection against rising supply chain attacks.