| 1 |
import type { TimerHandle } from './timerHandle'; |
| 2 |
type SetIntervalFunction = (handler: () => void, timeout?: number, ...args: any[]) => TimerHandle; |
| 3 |
type ClearIntervalFunction = (handle: TimerHandle) => void; |
| 4 |
|
| 5 |
interface IntervalProvider { |
| 6 |
setInterval: SetIntervalFunction; |
| 7 |
clearInterval: ClearIntervalFunction; |
| 8 |
delegate: |
| 9 |
| { |
| 10 |
setInterval: SetIntervalFunction; |
| 11 |
clearInterval: ClearIntervalFunction; |
| 12 |
} |
| 13 |
| undefined; |
| 14 |
} |
| 15 |
|
| 16 |
export const intervalProvider: IntervalProvider = { |
| 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 |
setInterval(handler: () => void, timeout?: number, ...args) { |
| 20 |
const { delegate } = intervalProvider; |
| 21 |
if (delegate?.setInterval) { |
| 22 |
return delegate.setInterval(handler, timeout, ...args); |
| 23 |
} |
| 24 |
return setInterval(handler, timeout, ...args); |
| 25 |
}, |
| 26 |
clearInterval(handle) { |
| 27 |
const { delegate } = intervalProvider; |
| 28 |
return (delegate?.clearInterval || clearInterval)(handle as any); |
| 29 |
}, |
| 30 |
delegate: undefined, |
| 31 |
}; |