( callback: () => void, delay: number, )
| 80 | const I32_MAX = 2 ** 31 - 1; |
| 81 | |
| 82 | function setArbitraryLengthTimeout( |
| 83 | callback: () => void, |
| 84 | delay: number, |
| 85 | ): { valueOf(): number } { |
| 86 | // ensure non-negative integer (but > I32_MAX is OK, even if Infinity) |
| 87 | delay = Math.trunc(Math.max(delay, 0) || 0); |
| 88 | |
| 89 | if (delay <= I32_MAX) { |
| 90 | const id = Number(setTimeout(callback, delay)); |
| 91 | return { valueOf: () => id }; |
| 92 | } |
| 93 | |
| 94 | const start = Date.now(); |
| 95 | let timeoutId: number; |
| 96 | |
| 97 | const queueTimeout = () => { |
| 98 | const currentDelay = delay - (Date.now() - start); |
| 99 | timeoutId = currentDelay > I32_MAX |
| 100 | ? Number(setTimeout(queueTimeout, I32_MAX)) |
| 101 | : Number(setTimeout(callback, currentDelay)); |
| 102 | }; |
| 103 | |
| 104 | queueTimeout(); |
| 105 | |
| 106 | return { valueOf: () => timeoutId }; |
| 107 | } |
no test coverage detected