feat: 数据库sql执行支持取消执行操作

This commit is contained in:
meilin.huang
2023-12-07 01:07:34 +08:00
parent e1e03dc09a
commit a376a82240
15 changed files with 202 additions and 67 deletions

View File

@@ -1,4 +1,5 @@
import request from './request';
import { randomUuid } from './utils/string';
/**
* 可用于各模块定义各自api请求
@@ -20,6 +21,8 @@ class Api {
*/
beforeHandler: Function;
static abortControllers: Map<string, AbortController> = new Map();
constructor(url: string, method: string) {
this.url = url;
this.method = method;
@@ -53,8 +56,63 @@ class Api {
return request.request(this.method, this.url, param, headers, options);
}
/**
* 请求对应的该api
* @param {Object} param 请求该api的参数
*/
requestCanCancel(key: string, param: any = null, options: any = null, headers: any = null): Promise<any> {
let controller = Api.abortControllers.get(key);
if (!controller) {
controller = new AbortController();
Api.abortControllers.set(key, controller);
}
if (options) {
options.signal = controller.signal;
} else {
options = {
signal: controller.signal,
};
}
return this.request(param, options, headers);
}
/** 静态方法 **/
/**
* 取消请求
* @param key 请求key
*/
static cancelReq(key: string) {
let controller = Api.abortControllers.get(key);
if (controller) {
controller.abort();
Api.removeAbortKey(key);
}
}
static removeAbortKey(key: string) {
if (key) {
console.log('remove abort key: ', key);
Api.abortControllers.delete(key);
}
}
/**
* 根据旧key生成新的abort key可能旧key未取消造成多余无用对象
* @param oldKey 旧key
* @returns key
*/
static genAbortKey(oldKey: string) {
if (!oldKey) {
return randomUuid();
}
if (Api.abortControllers.get(oldKey)) {
return oldKey;
}
return randomUuid();
}
/**
* 静态工厂返回Api对象并设置url与method属性
* @param url url