You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
2.7 KiB
74 lines
2.7 KiB
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.default = void 0;
|
|
|
|
var _react = require("react");
|
|
|
|
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
|
|
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
|
|
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
|
|
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
|
|
|
|
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
|
|
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
|
|
var useUpdateEffect = function useUpdateEffect(effect, deps) {
|
|
var isMounted = (0, _react.useRef)(false);
|
|
(0, _react.useEffect)(function () {
|
|
if (!isMounted.current) {
|
|
isMounted.current = true;
|
|
} else {
|
|
return effect();
|
|
}
|
|
|
|
return function () {
|
|
return undefined;
|
|
};
|
|
}, deps);
|
|
};
|
|
|
|
function useDebounceFn(fn, deps, wait) {
|
|
// eslint-disable-next-line no-underscore-dangle
|
|
var hooksDeps = Array.isArray(deps) ? deps : []; // eslint-disable-next-line no-underscore-dangle
|
|
|
|
var hookWait = typeof deps === 'number' ? deps : wait || 0;
|
|
var timer = (0, _react.useRef)();
|
|
var fnRef = (0, _react.useRef)(fn);
|
|
fnRef.current = fn;
|
|
var cancel = (0, _react.useCallback)(function () {
|
|
if (timer.current) {
|
|
clearTimeout(timer.current);
|
|
}
|
|
}, []);
|
|
var run = (0, _react.useCallback)(function () {
|
|
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
args[_key] = arguments[_key];
|
|
}
|
|
|
|
cancel();
|
|
timer.current = setTimeout(function () {
|
|
fnRef.current.apply(fnRef, args);
|
|
}, hookWait);
|
|
}, [hookWait, cancel]);
|
|
useUpdateEffect(function () {
|
|
run();
|
|
return cancel;
|
|
}, [].concat(_toConsumableArray(hooksDeps), [run]));
|
|
(0, _react.useEffect)(function () {
|
|
return cancel;
|
|
}, []);
|
|
return {
|
|
run: run,
|
|
cancel: cancel
|
|
};
|
|
}
|
|
|
|
var _default = useDebounceFn;
|
|
exports.default = _default; |