Files
NapCatQQ/.pnpm-store/v3/files/68/90d9bc37da7e08a7826b9929f65fb478c52af08b57a3937a494ab77b5c8d418d1d5514e28876fa02ff1c7f2138e623fdafbaa7e0e5b2fcbeceebf142cbe563
T
TanTan e311ca3275
Build NapCat Artifacts / Type Check & Test (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:25:58 +08:00

67 lines
2.4 KiB
Plaintext

"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _tslib = require("tslib");
var _debounce = _interopRequireDefault(require("lodash/debounce"));
var _react = require("react");
var useDebouncePlugin = function useDebouncePlugin(fetchInstance, _a) {
var debounceWait = _a.debounceWait,
debounceLeading = _a.debounceLeading,
debounceTrailing = _a.debounceTrailing,
debounceMaxWait = _a.debounceMaxWait;
var debouncedRef = (0, _react.useRef)(undefined);
var options = (0, _react.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]);
(0, _react.useEffect)(function () {
if (debounceWait) {
var _originRunAsync_1 = fetchInstance.runAsync.bind(fetchInstance);
debouncedRef.current = (0, _debounce["default"])(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, (0, _tslib.__spreadArray)([], (0, _tslib.__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 onCancel() {
var _a;
(_a = debouncedRef.current) === null || _a === void 0 ? void 0 : _a.cancel();
}
};
};
var _default = exports["default"] = useDebouncePlugin;