hongbo-miao/hongbomiao.com

View on GitHub
web/src/shared/hooks/useRandomInterval.hook.ts

Summary

Maintainability
A
0 mins
Test Coverage
import React from 'react';
import random from '../utils/random';

type Callback = () => void;

const useRandomInterval = (callback: Callback, minDelay: number, maxDelay: number): Callback => {
  const timeoutId = React.useRef(0);
  const savedCallback = React.useRef(callback);

  React.useEffect(() => {
    savedCallback.current = callback;
  });
  React.useEffect(() => {
    const handleTick = (): void => {
      const nextTickAt = random(minDelay, maxDelay);
      timeoutId.current = window.setTimeout(() => {
        savedCallback.current();
        handleTick();
      }, nextTickAt);
    };
    handleTick();
    return (): void => {
      window.clearTimeout(timeoutId.current);
    };
  }, [minDelay, maxDelay]);

  return React.useCallback(() => {
    window.clearTimeout(timeoutId.current);
  }, []);
};

export default useRandomInterval;