"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var react_1 = require("react"); var useUnmount_1 = tslib_1.__importDefault(require("./useUnmount")); var useThrottle = function (value, ms) { if (ms === void 0) { ms = 200; } var _a = react_1.useState(value), state = _a[0], setState = _a[1]; var timeout = react_1.useRef(); var nextValue = react_1.useRef(null); var hasNextValue = react_1.useRef(0); react_1.useEffect(function () { if (!timeout.current) { setState(value); var timeoutCallback_1 = function () { if (hasNextValue.current) { hasNextValue.current = false; setState(nextValue.current); timeout.current = setTimeout(timeoutCallback_1, ms); } else { timeout.current = undefined; } }; timeout.current = setTimeout(timeoutCallback_1, ms); } else { nextValue.current = value; hasNextValue.current = true; } }, [value]); useUnmount_1.default(function () { timeout.current && clearTimeout(timeout.current); }); return state; }; exports.default = useThrottle;