vue-admin-core
Version:
A Component Library for Vue 3
49 lines (46 loc) • 1.24 kB
JavaScript
import { debounce } from 'lodash-es';
import { onUnmounted } from 'vue';
const useDebouncePlugin = (fetchInstance, { debounceWait, debounceLeading, debounceTrailing, debounceMaxWait }) => {
let debounced;
const options = {};
if (debounceLeading !== void 0) {
options.leading = debounceLeading;
}
if (debounceTrailing !== void 0) {
options.trailing = debounceTrailing;
}
if (debounceMaxWait !== void 0) {
options.maxWait = debounceMaxWait;
}
if (debounceWait) {
const _originRunAsync = fetchInstance.runAsync.bind(fetchInstance);
debounced = debounce(
(callback) => {
callback();
},
debounceWait,
options
);
fetchInstance.runAsync = (...args) => {
return new Promise((resolve, reject) => {
debounced(() => {
_originRunAsync(...args).then(resolve).catch(reject);
});
});
};
onUnmounted(() => {
debounced.cancel();
fetchInstance.runAsync = _originRunAsync;
});
}
if (!debounceWait) {
return {};
}
return {
onCancel: () => {
debounced == null ? void 0 : debounced.cancel();
}
};
};
export { useDebouncePlugin as default };
//# sourceMappingURL=useDebouncePlugin.mjs.map