refactor: ♻️ 系统配置刷新防抖和支持patch请求

系统配置刷新防抖和支持patch请求
This commit is contained in:
Theo
2024-11-23 00:28:42 +08:00
parent ef73f257b6
commit 4528d83a81
3 changed files with 38 additions and 3 deletions

View File

@@ -39,3 +39,32 @@ export function isExternal(path: string) {
const isExternal = /^(https?:|http?:|mailto:|tel:)/.test(path);
return isExternal;
}
/**
* 防抖函数
*
* @param {Function} fn 需要防抖的函数
* @param {number} delay 防抖时间
* @returns {Function}
*/
export const debounce = <T extends (...args: any[]) => any>(
fn: T,
delay: number,
immediate = false
) => {
let timer: NodeJS.Timeout | null = null;
return function (this: any, ...args: Parameters<T>) {
if (timer) clearTimeout(timer);
if (immediate && !timer) {
fn.apply(this, args);
}
timer = setTimeout(() => {
if (!immediate) {
fn.apply(this, args);
}
timer = null;
}, delay);
};
};