mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-03 16:00:25 +08:00
feat: 数据库sql执行支持取消执行操作
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user