| 1 |
import type { TimerHandle } from './timerHandle'; |
| 2 |
type SetTimeoutFunction = (handler: () => void, timeout?: number, ...args: any[]) => TimerHandle; |
| 3 |
type ClearTimeoutFunction = (handle: TimerHandle) => void; |
| 4 |
|
| 5 |
interface TimeoutProvider { |
| 6 |
setTimeout: SetTimeoutFunction; |
| 7 |
clearTimeout: ClearTimeoutFunction; |
| 8 |
delegate: |
| 9 |
| { |
| 10 |
setTimeout: SetTimeoutFunction; |
| 11 |
clearTimeout: ClearTimeoutFunction; |
| 12 |
} |
| 13 |
| undefined; |
| 14 |
} |
| 15 |
|
| 16 |
export const timeoutProvider: TimeoutProvider = { |
| 17 |
// When accessing the delegate, use the variable rather than `this` so that |
| 18 |
// the functions can be called without being bound to the provider. |
| 19 |
setTimeout(handler: () => void, timeout?: number, ...args) { |
| 20 |
const { delegate } = timeoutProvider; |
| 21 |
if (delegate?.setTimeout) { |
| 22 |
return delegate.setTimeout(handler, timeout, ...args); |
| 23 |
} |
| 24 |
return setTimeout(handler, timeout, ...args); |
| 25 |
}, |
| 26 |
clearTimeout(handle) { |
| 27 |
const { delegate } = timeoutProvider; |
| 28 |
return (delegate?.clearTimeout || clearTimeout)(handle as any); |
| 29 |
}, |
| 30 |
delegate: undefined, |
| 31 |
}; |