Files
CordysCRM/frontend/packages/lib-shared/api/http/axiosCancel.ts
2025-03-26 21:02:19 +08:00

64 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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>();
}
}