| 500 | } |
| 501 | |
| 502 | function 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 |
| 523 | const sendTasksUpdateThrottled = throttle(sendTasksUpdate, 100) |