4b85916563811e6857db991fbda1db8b641a4d3f3e9989c44170ece1fc27221cf7170db46b1467ef6d7b2e3b838d758e9531671303f550388f377076f14aa7 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. import type { TimerHandle } from './timerHandle';
  2. type SetTimeoutFunction = (handler: () => void, timeout?: number, ...args: any[]) => TimerHandle;
  3. type ClearTimeoutFunction = (handle: TimerHandle) => void;
  4. interface TimeoutProvider {
  5. setTimeout: SetTimeoutFunction;
  6. clearTimeout: ClearTimeoutFunction;
  7. delegate:
  8. | {
  9. setTimeout: SetTimeoutFunction;
  10. clearTimeout: ClearTimeoutFunction;
  11. }
  12. | undefined;
  13. }
  14. export const timeoutProvider: TimeoutProvider = {
  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. setTimeout(handler: () => void, timeout?: number, ...args) {
  18. const { delegate } = timeoutProvider;
  19. if (delegate?.setTimeout) {
  20. return delegate.setTimeout(handler, timeout, ...args);
  21. }
  22. return setTimeout(handler, timeout, ...args);
  23. },
  24. clearTimeout(handle) {
  25. const { delegate } = timeoutProvider;
  26. return (delegate?.clearTimeout || clearTimeout)(handle as any);
  27. },
  28. delegate: undefined,
  29. };