MCPcopy
hub / github.com/vitest-dev/vitest / throttle

Function throttle

packages/runner/src/run.ts:502–520  ·  view source on GitHub ↗
(fn: T, ms: number)

Source from the content-addressed store, hash-verified

500}
501
502function throttle<T extends (...args: any[]) => void>(fn: T, ms: number): T {
503 let last = 0
504 let pendingCall: ReturnType<typeof setTimeout> | undefined
505
506 return function call(this: any, ...args: any[]) {
507 const now = unixNow()
508 if (now - last > ms) {
509 last = now
510
511 clearTimeout(pendingCall)
512 pendingCall = undefined
513
514 return fn.apply(this, args)
515 }
516
517 // Make sure fn is still called even if there are no further calls
518 pendingCall ??= setTimeout(() => call.bind(this)(...args), ms)
519 } as any
520}
521
522// throttle based on summary reporter's DURATION_UPDATE_INTERVAL_MS
523const sendTasksUpdateThrottled = throttle(sendTasksUpdate, 100)

Callers 1

run.tsFile · 0.85

Calls 1

bindMethod · 0.80

Tested by

no test coverage detected