Skip to content

Commit 527fd79

Browse files
committed
feat(packages): @sa/axios: replace CancelTokenSource by AbortController. close #530, close #532
1 parent 06971f3 commit 527fd79

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

packages/axios/src/index.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import axios, { AxiosError } from 'axios';
2-
import type { AxiosResponse, CancelTokenSource, CreateAxiosDefaults, InternalAxiosRequestConfig } from 'axios';
2+
import type { AxiosResponse, CreateAxiosDefaults, InternalAxiosRequestConfig } from 'axios';
33
import axiosRetry from 'axios-retry';
44
import { nanoid } from '@sa/utils';
55
import { createAxiosConfig, createDefaultOptions, createRetryOptions } from './options';
@@ -22,7 +22,7 @@ function createCommonRequest<ResponseData = any>(
2222
const axiosConf = createAxiosConfig(axiosConfig);
2323
const instance = axios.create(axiosConf);
2424

25-
const cancelTokenSourceMap = new Map<string, CancelTokenSource>();
25+
const abortControllerMap = new Map<string, AbortController>();
2626

2727
// config axios retry
2828
const retryOptions = createRetryOptions(axiosConf);
@@ -35,10 +35,12 @@ function createCommonRequest<ResponseData = any>(
3535
const requestId = nanoid();
3636
config.headers.set(REQUEST_ID_KEY, requestId);
3737

38-
// config cancel token
39-
const cancelTokenSource = axios.CancelToken.source();
40-
config.cancelToken = cancelTokenSource.token;
41-
cancelTokenSourceMap.set(requestId, cancelTokenSource);
38+
// config abort controller
39+
if (!config.signal) {
40+
const abortController = new AbortController();
41+
config.signal = abortController.signal;
42+
abortControllerMap.set(requestId, abortController);
43+
}
4244

4345
// handle config by hook
4446
const handledConfig = opts.onRequest?.(config) || config;
@@ -79,18 +81,18 @@ function createCommonRequest<ResponseData = any>(
7981
);
8082

8183
function cancelRequest(requestId: string) {
82-
const cancelTokenSource = cancelTokenSourceMap.get(requestId);
83-
if (cancelTokenSource) {
84-
cancelTokenSource.cancel();
85-
cancelTokenSourceMap.delete(requestId);
84+
const abortController = abortControllerMap.get(requestId);
85+
if (abortController) {
86+
abortController.abort();
87+
abortControllerMap.delete(requestId);
8688
}
8789
}
8890

8991
function cancelAllRequest() {
90-
cancelTokenSourceMap.forEach(cancelTokenSource => {
91-
cancelTokenSource.cancel();
92+
abortControllerMap.forEach(abortController => {
93+
abortController.abort();
9294
});
93-
cancelTokenSourceMap.clear();
95+
abortControllerMap.clear();
9496
}
9597

9698
return {

packages/axios/src/type.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,19 @@ export type CustomAxiosRequestConfig<R extends ResponseType = 'json'> = Omit<Axi
6969
};
7070

7171
export interface RequestInstanceCommon<T> {
72+
/**
73+
* cancel the request by request id
74+
*
75+
* if the request provide abort controller sign from config, it will not collect in the abort controller map
76+
*
77+
* @param requestId
78+
*/
7279
cancelRequest: (requestId: string) => void;
80+
/**
81+
* cancel all request
82+
*
83+
* if the request provide abort controller sign from config, it will not collect in the abort controller map
84+
*/
7385
cancelAllRequest: () => void;
7486
/** you can set custom state in the request instance */
7587
state: T;

pnpm-lock.yaml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)