"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useThrottledValue = useThrottledValue;
var _react = require("react");
/**
 * Returns a throttled version of the value that only updates at most once per `time` ms.
 */
function useThrottledValue(value, time) {
  const [throttledValue, setThrottledValue] = (0, _react.useState)(value);
  const timeoutRef = (0, _react.useRef)(null);
  (0, _react.useEffect)(() => {
    if (value === throttledValue) {
      return;
    }
    if (timeoutRef.current !== null) {
      // A timeout is already pending; it will pick up the latest value via closure.
      return;
    }
    timeoutRef.current = setTimeout(() => {
      timeoutRef.current = null;
      setThrottledValue(value);
    }, time);
    return () => {
      if (timeoutRef.current !== null) {
        clearTimeout(timeoutRef.current);
        timeoutRef.current = null;
      }
    };
  }, [value, time, throttledValue]);
  return throttledValue;
}
//# sourceMappingURL=useThrottledValue.js.map