MCPcopy
hub / github.com/colbymchenry/codegraph / throttle

Function throttle

src/utils.ts:477–503  ·  view source on GitHub ↗
(
  fn: T,
  limit: number
)

Source from the content-addressed store, hash-verified

475 * @returns Throttled function
476 */
477export function throttle<T extends (...args: unknown[]) => unknown>(
478 fn: T,
479 limit: number
480): (...args: Parameters<T>) => void {
481 let lastCall = 0;
482 let timeoutId: ReturnType<typeof setTimeout> | null = null;
483
484 return (...args: Parameters<T>) => {
485 const now = Date.now();
486 const remaining = limit - (now - lastCall);
487
488 if (remaining <= 0) {
489 if (timeoutId) {
490 clearTimeout(timeoutId);
491 timeoutId = null;
492 }
493 lastCall = now;
494 fn(...args);
495 } else if (!timeoutId) {
496 timeoutId = setTimeout(() => {
497 lastCall = Date.now();
498 timeoutId = null;
499 fn(...args);
500 }, remaining);
501 }
502 };
503}
504
505/**
506 * Estimate memory usage of an object (rough approximation)

Callers

nothing calls this directly

Calls 1

fnFunction · 0.50

Tested by

no test coverage detected