ht_hooks
Version:
ht React 业务 Hooks
63 lines • 2.25 kB
JavaScript
//!如果短时间内频繁触发请求,我不立刻发,而是等一段时间用户不再触发时再发请求。
import { __read, __spreadArray } from "tslib";
import { debounce } from 'lodash-es';
import { useEffect, useMemo, useRef } from 'react';
var useDebouncePlugin = function (fetchInstance, _a) {
var debounceWait = _a.debounceWait,
debounceLeading = _a.debounceLeading,
debounceTrailing = _a.debounceTrailing,
debounceMaxWait = _a.debounceMaxWait;
var debouncedRef = useRef();
var options = useMemo(function () {
var ret = {};
if (debounceLeading !== undefined) {
ret.leading = debounceLeading;
}
if (debounceTrailing !== undefined) {
ret.trailing = debounceTrailing;
}
if (debounceMaxWait !== undefined) {
ret.maxWait = debounceMaxWait;
}
return ret;
}, [debounceLeading, debounceTrailing, debounceMaxWait]);
useEffect(function () {
if (debounceWait) {
var _originRunAsync_1 = fetchInstance.runAsync.bind(fetchInstance);
debouncedRef.current = debounce(
//#lodash中的防抖api
function (callback) {
callback();
}, debounceWait, options);
// debounce runAsync should be promise
// https://github.com/lodash/lodash/issues/4400#issuecomment-834800398
fetchInstance.runAsync = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return new Promise(function (resolve, reject) {
var _a;
(_a = debouncedRef.current) === null || _a === void 0 ? void 0 : _a.call(debouncedRef, function () {
_originRunAsync_1.apply(void 0, __spreadArray([], __read(args), false)).then(resolve).catch(reject);
});
});
};
return function () {
var _a;
(_a = debouncedRef.current) === null || _a === void 0 ? void 0 : _a.cancel();
fetchInstance.runAsync = _originRunAsync_1;
};
}
}, [debounceWait, options]);
if (!debounceWait) {
return {};
}
return {
onCancel: function () {
var _a;
(_a = debouncedRef.current) === null || _a === void 0 ? void 0 : _a.cancel();
}
};
};
export default useDebouncePlugin;