refactor: axios Canceler use AbortController (#2676)
This commit is contained in:
parent
ae5f5cb13c
commit
b14a15e66b
|
|
@ -1,60 +1,60 @@
|
||||||
import type { AxiosRequestConfig, Canceler } from 'axios';
|
import type { AxiosRequestConfig } from 'axios';
|
||||||
import axios from 'axios';
|
|
||||||
import { isFunction } from '/@/utils/is';
|
|
||||||
|
|
||||||
// Used to store the identification and cancellation function of each request
|
// 用于存储每个请求的标识和取消函数
|
||||||
let pendingMap = new Map<string, Canceler>();
|
const pendingMap = new Map<string, AbortController>();
|
||||||
|
|
||||||
export const getPendingUrl = (config: AxiosRequestConfig) => [config.method, config.url].join('&');
|
const getPendingUrl = (config: AxiosRequestConfig): string => {
|
||||||
|
return [config.method, config.url].join('&');
|
||||||
|
};
|
||||||
|
|
||||||
export class AxiosCanceler {
|
export class AxiosCanceler {
|
||||||
/**
|
/**
|
||||||
* Add request
|
* 添加请求
|
||||||
* @param {Object} config
|
* @param config 请求配置
|
||||||
*/
|
*/
|
||||||
addPending(config: AxiosRequestConfig) {
|
public addPending(config: AxiosRequestConfig): void {
|
||||||
this.removePending(config);
|
this.removePending(config);
|
||||||
const url = getPendingUrl(config);
|
const url = getPendingUrl(config);
|
||||||
config.cancelToken =
|
const controller = new AbortController();
|
||||||
config.cancelToken ||
|
config.signal = config.signal || controller.signal;
|
||||||
new axios.CancelToken((cancel) => {
|
if (!pendingMap.has(url)) {
|
||||||
if (!pendingMap.has(url)) {
|
// 如果当前请求不在等待中,将其添加到等待中
|
||||||
// If there is no current request in pending, add it
|
pendingMap.set(url, controller);
|
||||||
pendingMap.set(url, cancel);
|
}
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description: Clear all pending
|
* 清除所有等待中的请求
|
||||||
*/
|
*/
|
||||||
removeAllPending() {
|
public removeAllPending(): void {
|
||||||
pendingMap.forEach((cancel) => {
|
pendingMap.forEach((abortController) => {
|
||||||
cancel && isFunction(cancel) && cancel();
|
if (abortController) {
|
||||||
|
abortController.abort();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
pendingMap.clear();
|
this.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removal request
|
* 移除请求
|
||||||
* @param {Object} config
|
* @param config 请求配置
|
||||||
*/
|
*/
|
||||||
removePending(config: AxiosRequestConfig) {
|
public removePending(config: AxiosRequestConfig): void {
|
||||||
const url = getPendingUrl(config);
|
const url = getPendingUrl(config);
|
||||||
|
|
||||||
if (pendingMap.has(url)) {
|
if (pendingMap.has(url)) {
|
||||||
// If there is a current request identifier in pending,
|
// 如果当前请求在等待中,取消它并将其从等待中移除
|
||||||
// the current request needs to be cancelled and removed
|
const abortController = pendingMap.get(url);
|
||||||
const cancel = pendingMap.get(url);
|
if (abortController) {
|
||||||
cancel && cancel(url);
|
abortController.abort(url);
|
||||||
|
}
|
||||||
pendingMap.delete(url);
|
pendingMap.delete(url);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description: reset
|
* 重置
|
||||||
*/
|
*/
|
||||||
reset(): void {
|
public reset(): void {
|
||||||
pendingMap = new Map<string, Canceler>();
|
pendingMap.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue