vue-admin-core
Version:
A Component Library for Vue 3
45 lines (40 loc) • 1.11 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var lodashEs = require('lodash-es');
const useThrottlePlugin = (fetchInstance, { throttleWait, throttleLeading, throttleTrailing }) => {
let throttled;
const options = {};
if (throttleLeading !== void 0) {
options.leading = throttleLeading;
}
if (throttleTrailing !== void 0) {
options.trailing = throttleTrailing;
}
if (throttleWait) {
const _originRunAsync = fetchInstance.runAsync.bind(fetchInstance);
throttled = lodashEs.throttle(
(callback) => {
callback();
},
throttleWait,
options
);
fetchInstance.runAsync = (...args) => {
return new Promise((resolve, reject) => {
throttled == null ? void 0 : throttled(() => {
_originRunAsync(...args).then(resolve).catch(reject);
});
});
};
}
if (!throttleWait) {
return {};
}
return {
onCancel: () => {
throttled == null ? void 0 : throttled.cancel();
}
};
};
exports.default = useThrottlePlugin;
//# sourceMappingURL=useThrottlePlugin.js.map