5433e0f1816f18b485a8a125c8328810d0d16402e6e7e35bba96c52e84a3bb4cd8701d7a4cd888422ab9a3cdc9513ecc61bcc92bfb741706e3b3083a7caa6d 1.1 KB

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