chore: initial commit
Some checks failed
Synchronize to Gitee / repo-sync (push) Has been cancelled
Typos Checking / Spell Check with Typos (push) Has been cancelled

This commit is contained in:
2026-06-23 11:56:23 +08:00
commit 72e2110987
2883 changed files with 367388 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
import axios from 'axios';
import { isFunction } from '../../method/is';
import type { AxiosRequestConfig, Canceler } from 'axios';
let pendingMap = new Map<string, Canceler>();
export const getPendingUrl = (config: AxiosRequestConfig) => [config.method, config.url].join('&');
export class AxiosCanceler {
/**
* 添加请求
* @param {Object} config
*/
addPending(config: AxiosRequestConfig) {
this.removePending(config);
const url = getPendingUrl(config);
config.cancelToken =
config.cancelToken ||
new axios.CancelToken((cancel) => {
if (!pendingMap.has(url)) {
// 非重复请求存入pending中
pendingMap.set(url, cancel);
}
});
}
/**
* @description: 清理全部pending中的请求
*/
removeAllPending() {
pendingMap.forEach((cancel) => {
if (cancel && isFunction(cancel)) {
cancel();
}
});
pendingMap.clear();
}
/**
* 取消并移除指定请求
* @param {Object} config
*/
removePending(config: AxiosRequestConfig) {
const url = getPendingUrl(config);
if (pendingMap.has(url)) {
// 根据标识找到pending中对应的请求并取消
const cancel = pendingMap.get(url);
if (cancel && isFunction(cancel)) {
cancel(url);
}
pendingMap.delete(url);
}
}
/**
* @description: 重置pending列表
*/
static reset(): void {
pendingMap = new Map<string, Canceler>();
}
}