@zwa73/utils 1.0.77 → 1.0.79
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/QuickFunction.d.ts +15 -0
- package/dist/QuickFunction.js +27 -0
- package/dist/UtilDecorators.d.ts +10 -0
- package/dist/UtilDecorators.js +40 -3
- package/dist/UtilFunctions.d.ts +34 -37
- package/dist/UtilFunctions.js +99 -111
- package/dist/UtilInterfaces.d.ts +6 -3
- package/dist/UtilSymbol.d.ts +15 -0
- package/dist/UtilSymbol.js +13 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/test/repeatTest.d.ts +1 -0
- package/dist/test/repeatTest.js +29 -0
- package/package.json +1 -1
- package/src/QuickFunction.ts +36 -0
- package/src/UtilDecorators.ts +37 -1
- package/src/UtilFunctions.ts +121 -106
- package/src/UtilInterfaces.ts +7 -3
- package/src/UtilSymbol.ts +15 -0
- package/src/index.ts +3 -1
- package/src/test/repeatTest.ts +29 -0
- package/test/test.bat +2 -0
- package/test/test.js +37 -0
package/dist/UtilInterfaces.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Completed, Failed, Terminated } from "./UtilSymbol";
|
|
1
2
|
/**可以序列化为JSON文件的对象 */
|
|
2
3
|
export type JToken = JObject | JArray | JValue | IJData;
|
|
3
4
|
/**在stringify输出时 undefine 会被转为 null */
|
|
@@ -14,6 +15,8 @@ export interface IJData {
|
|
|
14
15
|
*/
|
|
15
16
|
toJSON(): JToken;
|
|
16
17
|
}
|
|
18
|
+
/**任意可作为键值的类型 */
|
|
19
|
+
export type Keyable = string | number | symbol;
|
|
17
20
|
/**转为可写的 */
|
|
18
21
|
export type Writeable<T> = {
|
|
19
22
|
-readonly [P in keyof T]: T[P];
|
|
@@ -53,7 +56,7 @@ export type ExclusiveJObject<B extends JObject, T extends JToken, K extends stri
|
|
|
53
56
|
* 终止 将直接返回 null
|
|
54
57
|
* 失败 将重试
|
|
55
58
|
*/
|
|
56
|
-
export type PromiseStat =
|
|
59
|
+
export type PromiseStat = Completed | Failed | Terminated;
|
|
57
60
|
/**promise验证函数 */
|
|
58
61
|
export type PromiseVerifyFn<T> = (obj: T) => Promise<PromiseStat> | PromiseStat;
|
|
59
62
|
/**类型中任意函数的字符串名称 */
|
|
@@ -107,12 +110,12 @@ export type WithPrefix<T, P extends string> = {
|
|
|
107
110
|
* @template K - 类型键值
|
|
108
111
|
* @template V - 值
|
|
109
112
|
*/
|
|
110
|
-
export type Outcome<K extends
|
|
113
|
+
export type Outcome<K extends Keyable, V> = {
|
|
111
114
|
/**状态类型 */
|
|
112
115
|
status: K;
|
|
113
116
|
/**值 */
|
|
114
117
|
result: V;
|
|
115
118
|
};
|
|
116
119
|
/**从联合 Outcome 中 根据 id 提取对应 value 的 type */
|
|
117
|
-
export type ExtractOutcome<T, K extends
|
|
120
|
+
export type ExtractOutcome<T, K extends Keyable> = T extends Outcome<K, unknown> ? T : never;
|
|
118
121
|
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**完成 */
|
|
2
|
+
export declare const Completed: unique symbol;
|
|
3
|
+
export type Completed = typeof Completed;
|
|
4
|
+
/**失败 */
|
|
5
|
+
export declare const Failed: unique symbol;
|
|
6
|
+
export type Failed = typeof Failed;
|
|
7
|
+
/**终止 */
|
|
8
|
+
export declare const Terminated: unique symbol;
|
|
9
|
+
export type Terminated = typeof Terminated;
|
|
10
|
+
/**不存在 */
|
|
11
|
+
export declare const None: unique symbol;
|
|
12
|
+
export type None = typeof None;
|
|
13
|
+
/**超时 */
|
|
14
|
+
export declare const Timeout: unique symbol;
|
|
15
|
+
export type Timeout = typeof Timeout;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Timeout = exports.None = exports.Terminated = exports.Failed = exports.Completed = void 0;
|
|
4
|
+
/**完成 */
|
|
5
|
+
exports.Completed = Symbol("Completed");
|
|
6
|
+
/**失败 */
|
|
7
|
+
exports.Failed = Symbol("Failed");
|
|
8
|
+
/**终止 */
|
|
9
|
+
exports.Terminated = Symbol("Terminated");
|
|
10
|
+
/**不存在 */
|
|
11
|
+
exports.None = Symbol("None");
|
|
12
|
+
/**超时 */
|
|
13
|
+
exports.Timeout = Symbol("Timeout");
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export * from './UtilFunctions';
|
|
2
2
|
export * from './UtilInterfaces';
|
|
3
|
+
export * from './UtilSymbol';
|
|
3
4
|
export * from './UtilClass';
|
|
4
5
|
export * from './UtilCom';
|
|
5
6
|
export * from './UtilCodecs';
|
|
@@ -8,3 +9,4 @@ export * from './UtilDecorators';
|
|
|
8
9
|
export * from './UtilFileTools';
|
|
9
10
|
export * from './UtilLogger';
|
|
10
11
|
export * from './UtilFP';
|
|
12
|
+
export * from './QuickFunction';
|
package/dist/index.js
CHANGED
|
@@ -16,6 +16,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./UtilFunctions"), exports);
|
|
18
18
|
__exportStar(require("./UtilInterfaces"), exports);
|
|
19
|
+
__exportStar(require("./UtilSymbol"), exports);
|
|
19
20
|
__exportStar(require("./UtilClass"), exports);
|
|
20
21
|
__exportStar(require("./UtilCom"), exports);
|
|
21
22
|
__exportStar(require("./UtilCodecs"), exports);
|
|
@@ -24,3 +25,4 @@ __exportStar(require("./UtilDecorators"), exports);
|
|
|
24
25
|
__exportStar(require("./UtilFileTools"), exports);
|
|
25
26
|
__exportStar(require("./UtilLogger"), exports);
|
|
26
27
|
__exportStar(require("./UtilFP"), exports);
|
|
28
|
+
__exportStar(require("./QuickFunction"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const __1 = require("..");
|
|
4
|
+
// 模拟的异步函数,有一定几率失败
|
|
5
|
+
async function mockAsyncFunction() {
|
|
6
|
+
return new Promise((resolve, reject) => {
|
|
7
|
+
setTimeout(() => {
|
|
8
|
+
Math.random() > 0.5 ? resolve("Success") : reject("Failure");
|
|
9
|
+
}, Math.random() % 2000);
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
// 验证函数,只有当结果为 "Success" 时才认为成功
|
|
13
|
+
function mockVerifyFunction(result) {
|
|
14
|
+
return result === "Success" ? __1.Completed : __1.Failed;
|
|
15
|
+
}
|
|
16
|
+
// 测试 repeatPromise 函数
|
|
17
|
+
__1.UtilFunc.repeatPromise(mockAsyncFunction, mockVerifyFunction, 3, 10)
|
|
18
|
+
.then(result => {
|
|
19
|
+
console.log("then");
|
|
20
|
+
if (result === null) {
|
|
21
|
+
console.log("全部超时");
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
console.log("成功:", result);
|
|
25
|
+
}
|
|
26
|
+
})
|
|
27
|
+
.catch(err => {
|
|
28
|
+
console.error("出现错误", err);
|
|
29
|
+
});
|
package/package.json
CHANGED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { ExtractOutcome, Keyable, Outcome } from "./UtilInterfaces";
|
|
2
|
+
|
|
3
|
+
/**创建一个outcome */
|
|
4
|
+
export function outcome<K extends Keyable,V> (key:K,value:V):Outcome<K,V>{
|
|
5
|
+
return {status:key,result:value}
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**处理联合 简单值
|
|
9
|
+
* @param t - 目标值
|
|
10
|
+
* @param procObj - 所有可能的id组成的处理函数映射
|
|
11
|
+
* @returns 任意处理函数的返回值
|
|
12
|
+
*/
|
|
13
|
+
export function match<
|
|
14
|
+
T extends Keyable | Outcome<Keyable, unknown>,
|
|
15
|
+
P extends (T extends Keyable
|
|
16
|
+
? {[K in T]:(k:K)=>unknown}
|
|
17
|
+
: T extends Outcome<Keyable,unknown>
|
|
18
|
+
? {[K in T['status']]:(k:K,v:ExtractOutcome<T,K>['result'])=>unknown}
|
|
19
|
+
: never)>
|
|
20
|
+
(t:T, procObj:P):
|
|
21
|
+
P extends Record<any,(...args:any)=>any>
|
|
22
|
+
? {[K in keyof P]: ReturnType<P[K]>}[keyof P]
|
|
23
|
+
: never {
|
|
24
|
+
if (typeof t === 'string' || typeof t === 'number' || typeof t === 'symbol')
|
|
25
|
+
return (procObj as any)[t](t);
|
|
26
|
+
else
|
|
27
|
+
return (procObj as any)[t.status](t.status,t.result);
|
|
28
|
+
}
|
|
29
|
+
if(false){
|
|
30
|
+
let a:"asd"|"dsa" = null as any;
|
|
31
|
+
const r = match(a,{
|
|
32
|
+
"asd":(a)=>"ssa" as const,
|
|
33
|
+
"dsa":(b)=>"ssb" as const,
|
|
34
|
+
})
|
|
35
|
+
}
|
|
36
|
+
|
package/src/UtilDecorators.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { UtilFunc } from "./UtilFunctions";
|
|
1
2
|
import { SLogger } from "./UtilLogger";
|
|
2
3
|
|
|
3
4
|
type TDTg<T> =
|
|
@@ -6,6 +7,41 @@ type TDTg<T> =
|
|
|
6
7
|
type DTg = (target:Object, propertyKey:string, descriptor:PropertyDescriptor)=>
|
|
7
8
|
PropertyDescriptor;
|
|
8
9
|
|
|
10
|
+
/**用于打印方法运行时间
|
|
11
|
+
* @param flag - 时间标签
|
|
12
|
+
* @param suffixUID - 是否尾随uid
|
|
13
|
+
*/
|
|
14
|
+
export function LogTime(flag:string,suffixUID?:boolean):DTg{
|
|
15
|
+
return function (target, propertyKey, descriptor){
|
|
16
|
+
const originalMethod = descriptor.value;
|
|
17
|
+
descriptor.value = function(...args:any[]){
|
|
18
|
+
const uid = suffixUID ? UtilFunc.genUUID() : "";
|
|
19
|
+
SLogger.time(flag+uid);
|
|
20
|
+
let result = originalMethod.apply(this, args);
|
|
21
|
+
SLogger.timeEnd(flag+uid);
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
return descriptor;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**用于打印异步方法运行时间
|
|
28
|
+
* @param flag - 时间标签
|
|
29
|
+
* @param suffixUID - 是否尾随uid
|
|
30
|
+
*/
|
|
31
|
+
export function LogTimeAsync(flag:string,suffixUID?:boolean):DTg{
|
|
32
|
+
return function (target, propertyKey, descriptor){
|
|
33
|
+
const originalMethod = descriptor.value;
|
|
34
|
+
descriptor.value = async function(...args:any[]){
|
|
35
|
+
const uid = suffixUID ? UtilFunc.genUUID() : "";
|
|
36
|
+
SLogger.time(flag+uid);
|
|
37
|
+
let result = await originalMethod.apply(this, args);
|
|
38
|
+
SLogger.timeEnd(flag+uid);
|
|
39
|
+
return result;
|
|
40
|
+
}
|
|
41
|
+
return descriptor;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
9
45
|
/**用于打印方法的调用 */
|
|
10
46
|
export function LogCall():DTg{
|
|
11
47
|
return function (target, propertyKey, descriptor){
|
|
@@ -162,7 +198,7 @@ class Example {
|
|
|
162
198
|
}
|
|
163
199
|
@LogCall()
|
|
164
200
|
@Defer((a,b)=>null)
|
|
165
|
-
async myMethod1(num: number,ss:string) {
|
|
201
|
+
static async myMethod1(num: number,ss:string) {
|
|
166
202
|
return 312;
|
|
167
203
|
}
|
|
168
204
|
}
|
package/src/UtilFunctions.ts
CHANGED
|
@@ -1,14 +1,31 @@
|
|
|
1
1
|
import * as crypto from "crypto";
|
|
2
|
-
import { ComposedClass, ComposedMixinable, ExtractOutcome,
|
|
2
|
+
import { ComposedClass, ComposedMixinable, ExtractOutcome, IJData, JObject, JToken, Keyable, Mixinable, Outcome, PromiseStat, PromiseVerifyFn } from "@src/UtilInterfaces";
|
|
3
3
|
import * as cp from "child_process";
|
|
4
4
|
import { SLogger } from "@src/UtilLogger";
|
|
5
|
+
import { Completed, Failed, None, Terminated, Timeout } from "./UtilSymbol";
|
|
6
|
+
import { match, outcome } from "./QuickFunction";
|
|
7
|
+
import { LogTimeAsync } from "./UtilDecorators";
|
|
5
8
|
|
|
9
|
+
|
|
10
|
+
type CompleteCome<T> = Outcome<Completed,T>;
|
|
11
|
+
type TimeoutCome<T> = Outcome<Timeout,Promise<T>>;
|
|
12
|
+
/**永不完成的Promise单例 */
|
|
13
|
+
const NeverResolvedPromise = new Promise(()=>{});
|
|
14
|
+
/**进行中的请求 */
|
|
15
|
+
type ProcessingPromise<T> = {
|
|
16
|
+
/**主体请求 */
|
|
17
|
+
result:T;
|
|
18
|
+
/**请求状态 */
|
|
19
|
+
stat:PromiseStat;
|
|
20
|
+
/**请求下标/序号 */
|
|
21
|
+
index:number;
|
|
22
|
+
};
|
|
6
23
|
/**常用函数 */
|
|
7
|
-
export
|
|
24
|
+
export class UtilFunc{
|
|
8
25
|
/**获取当前时间戳
|
|
9
26
|
* @returns 时间戳
|
|
10
27
|
*/
|
|
11
|
-
|
|
28
|
+
static getTime(): number {
|
|
12
29
|
return new Date().getTime();
|
|
13
30
|
}
|
|
14
31
|
|
|
@@ -19,7 +36,7 @@ export function getTime(): number {
|
|
|
19
36
|
* @param defaultVal - 默认值
|
|
20
37
|
* @returns 最终值
|
|
21
38
|
*/
|
|
22
|
-
|
|
39
|
+
static initField<T extends object,K extends keyof T>(
|
|
23
40
|
obj: T,
|
|
24
41
|
field: K,
|
|
25
42
|
defaultVal: T[K]
|
|
@@ -33,7 +50,7 @@ export function initField<T extends object,K extends keyof T>(
|
|
|
33
50
|
* @param checkObj - 用于检测的对象 在对应key缺失时赋予对应值 如果值为函数, 则赋予执行结果 如果结果为 undefined 则不赋值
|
|
34
51
|
* @returns 完成初始化的对象
|
|
35
52
|
*/
|
|
36
|
-
|
|
53
|
+
static initObject<T extends JObject>
|
|
37
54
|
(obj:T,checkObj:{[P in keyof T]:T[P]|(()=>void|T[P])}){
|
|
38
55
|
const fixobj = obj as any;
|
|
39
56
|
for(const key in checkObj){
|
|
@@ -52,14 +69,14 @@ export function initObject<T extends JObject>
|
|
|
52
69
|
/**生成一串uuid
|
|
53
70
|
* @returns uuid
|
|
54
71
|
*/
|
|
55
|
-
|
|
72
|
+
static genUUID() {
|
|
56
73
|
return crypto.randomBytes(16).toString("hex");
|
|
57
74
|
}
|
|
58
75
|
/**计算Hash
|
|
59
76
|
* @param str - 待计算的字符串
|
|
60
77
|
* @returns hash
|
|
61
78
|
*/
|
|
62
|
-
|
|
79
|
+
static calcHash(str:string) {
|
|
63
80
|
return crypto.createHash('md5').update(str).digest('hex');
|
|
64
81
|
}
|
|
65
82
|
|
|
@@ -68,7 +85,7 @@ export function calcHash(str:string) {
|
|
|
68
85
|
* @param obj - 克隆目标
|
|
69
86
|
* @returns 克隆结果
|
|
70
87
|
*/
|
|
71
|
-
|
|
88
|
+
static deepClone<T extends JToken>(obj: T): T {
|
|
72
89
|
return JSON.parse(JSON.stringify(obj));
|
|
73
90
|
}
|
|
74
91
|
|
|
@@ -76,7 +93,7 @@ export function deepClone<T extends JToken>(obj: T): T {
|
|
|
76
93
|
* @param num - 所要检测的数字
|
|
77
94
|
* @returns 是否安全
|
|
78
95
|
*/
|
|
79
|
-
|
|
96
|
+
static isSafeNumber(num: number): boolean {
|
|
80
97
|
if (num === undefined || num == null || isNaN(num)) return false;
|
|
81
98
|
if(typeof num === 'number') return true;
|
|
82
99
|
return false;
|
|
@@ -87,10 +104,18 @@ export function isSafeNumber(num: number): boolean {
|
|
|
87
104
|
* @param timeMs - 等待的毫秒数
|
|
88
105
|
* @returns
|
|
89
106
|
*/
|
|
90
|
-
|
|
107
|
+
static async sleep<T>(timeMs: number): Promise<void>
|
|
108
|
+
/**等待 timeMs 毫秒
|
|
109
|
+
* @async
|
|
110
|
+
* @param timeMs - 等待的毫秒数
|
|
111
|
+
* @param result - 结果
|
|
112
|
+
* @returns
|
|
113
|
+
*/
|
|
114
|
+
static async sleep<T>(timeMs: number, result:T): Promise<T>
|
|
115
|
+
static async sleep<T>(timeMs: number, result?:T): Promise<T|undefined> {
|
|
91
116
|
return new Promise(function (resolve, rejecte) {
|
|
92
117
|
let timer = setTimeout(function () {
|
|
93
|
-
resolve(
|
|
118
|
+
resolve(result);
|
|
94
119
|
}, timeMs);
|
|
95
120
|
});
|
|
96
121
|
}
|
|
@@ -98,7 +123,7 @@ export async function sleep(timeMs: number): Promise<boolean> {
|
|
|
98
123
|
/**封装的 cp.exec 执行一段指令 指令完成后返回 Promise
|
|
99
124
|
* @param command 指令文本
|
|
100
125
|
*/
|
|
101
|
-
|
|
126
|
+
static exec(command: string) {
|
|
102
127
|
return new Promise<{ stdout:string, stderr:string }>
|
|
103
128
|
((resolve, reject) => {
|
|
104
129
|
cp.exec(command, (error, stdout, stderr) => {
|
|
@@ -110,23 +135,12 @@ export function exec(command: string) {
|
|
|
110
135
|
});
|
|
111
136
|
}
|
|
112
137
|
|
|
113
|
-
|
|
114
|
-
const NeverResolvedPromise = new Promise(()=>{});
|
|
138
|
+
|
|
115
139
|
/**获得一个永不完成的Promise单例 */
|
|
116
|
-
|
|
140
|
+
static getNeverResolvedPromise<T>():Promise<T>{
|
|
117
141
|
return NeverResolvedPromise as Promise<T>;
|
|
118
142
|
}
|
|
119
143
|
|
|
120
|
-
/**进行中的请求 */
|
|
121
|
-
type ProcessingPromise<T> = {
|
|
122
|
-
/**主体请求 */
|
|
123
|
-
result:T;
|
|
124
|
-
/**请求状态 */
|
|
125
|
-
stat:PromiseStat|Promise<PromiseStat>;
|
|
126
|
-
/**请求下标/序号 */
|
|
127
|
-
index:number;
|
|
128
|
-
};
|
|
129
|
-
|
|
130
144
|
/**重复尝试promise
|
|
131
145
|
* @async
|
|
132
146
|
* @param procFn - 发起函数
|
|
@@ -135,13 +149,10 @@ type ProcessingPromise<T> = {
|
|
|
135
149
|
* @param repeatTime - 超时时间/秒 最小为10秒
|
|
136
150
|
* @returns 结果 null 为全部失败/超时
|
|
137
151
|
*/
|
|
138
|
-
|
|
152
|
+
@LogTimeAsync("repeatPromise ",true)
|
|
153
|
+
static async repeatPromise<T>(procFn:()=>Promise<T>,verifyFn?:PromiseVerifyFn<T>,
|
|
139
154
|
repeatCount:number=3,repeatTime:number=180):Promise<T|null>{
|
|
140
155
|
|
|
141
|
-
//计时
|
|
142
|
-
const timeflag = "repeatPromise "+UtilFunc.genUUID();
|
|
143
|
-
SLogger.time(timeflag);
|
|
144
|
-
|
|
145
156
|
/**是否含有超时时间 */
|
|
146
157
|
const hasRepeatTime = (repeatTime>=10);
|
|
147
158
|
//转换为毫秒
|
|
@@ -149,23 +160,13 @@ export async function repeatPromise<T>(procFn:()=>Promise<T>,verifyFn?:PromiseVe
|
|
|
149
160
|
|
|
150
161
|
//验证处理函数
|
|
151
162
|
if(verifyFn===undefined)
|
|
152
|
-
verifyFn = ()=>
|
|
153
|
-
|
|
154
|
-
/**计时器 */
|
|
155
|
-
let timer:NodeJS.Timer|null = null;
|
|
156
|
-
/**计时器 Promise */
|
|
157
|
-
let timerP:Promise<"Timeout">|null=null;
|
|
158
|
-
/**计时器 Promise 的返回函数 */
|
|
159
|
-
let resolveFn:((value: "Timeout")=>void)|null = null;
|
|
160
|
-
/**清理计时器 */
|
|
161
|
-
const clearTimer = ()=>{
|
|
162
|
-
if(timer) clearInterval(timer);
|
|
163
|
-
if(resolveFn) resolveFn("Timeout");
|
|
164
|
-
timerP = timer = resolveFn = null;
|
|
165
|
-
}
|
|
163
|
+
verifyFn = ()=>Completed;
|
|
166
164
|
|
|
167
165
|
//进行中的请求
|
|
168
|
-
const plist:Promise<
|
|
166
|
+
const plist:Promise<
|
|
167
|
+
CompleteCome<ProcessingPromise<T>>|
|
|
168
|
+
TimeoutCome<ProcessingPromise<T>>
|
|
169
|
+
>[] = [];
|
|
169
170
|
|
|
170
171
|
//开始处理
|
|
171
172
|
try{
|
|
@@ -175,57 +176,58 @@ export async function repeatPromise<T>(procFn:()=>Promise<T>,verifyFn?:PromiseVe
|
|
|
175
176
|
|
|
176
177
|
//如果 plist 中当前下标的任务还未创建 则 创建当前任务
|
|
177
178
|
if(plist.length<i+1){
|
|
178
|
-
plist.push(
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
timerP = new Promise<"Timeout">((resolve, rejecte)=>{
|
|
185
|
-
resolveFn = resolve;
|
|
186
|
-
timer = setTimeout(()=>resolve("Timeout"),
|
|
187
|
-
hasRepeatTime ? repeatTime : Infinity);//无限制则无限时间
|
|
188
|
-
})
|
|
179
|
+
plist.push(UtilFunc.timelimitPromise<ProcessingPromise<T>>(async ()=>{
|
|
180
|
+
const index = i;
|
|
181
|
+
const result = await procFn();
|
|
182
|
+
const stat = await verifyFn!(result);
|
|
183
|
+
return {result, stat, index}
|
|
184
|
+
},hasRepeatTime?repeatTime:undefined));
|
|
189
185
|
}
|
|
190
186
|
|
|
191
187
|
//等待任意任务 或当前计时器完成
|
|
192
|
-
const currObj = await Promise.race([...plist
|
|
188
|
+
const currObj = await Promise.race([...plist]);
|
|
193
189
|
|
|
194
190
|
//超时处理
|
|
195
|
-
if(currObj===
|
|
191
|
+
if(currObj.status===Timeout){
|
|
192
|
+
//解除timeout替换原参数
|
|
193
|
+
plist[i]=new Promise(async (rslove)=>{
|
|
194
|
+
const res = await currObj.result;
|
|
195
|
+
rslove(outcome(Completed,res));
|
|
196
|
+
})
|
|
196
197
|
SLogger.warn(`第 ${i+1} 次 repeatPromise 超时 ${repeatTime} ms 开始重试`);
|
|
197
|
-
|
|
198
|
+
i++;
|
|
198
199
|
continue;
|
|
199
200
|
}
|
|
200
201
|
|
|
201
202
|
//路由请求状态
|
|
202
|
-
const
|
|
203
|
-
const result =
|
|
204
|
-
Completed(){
|
|
205
|
-
SLogger.info(`第 ${
|
|
203
|
+
const postresult = currObj.result;
|
|
204
|
+
const result = match(postresult.stat,{
|
|
205
|
+
[Completed](){
|
|
206
|
+
SLogger.info(`第 ${postresult.index+1} 次 repeatPromise 成功`);
|
|
206
207
|
//非当前
|
|
207
|
-
if(
|
|
208
|
+
if(postresult.index!=i)
|
|
208
209
|
SLogger.info(`成功的 promise 非当前 promise 考虑增大重试间隔\n当前index: ${i}\n当前间隔: ${repeatTime}`);
|
|
209
|
-
return
|
|
210
|
+
return postresult.result;
|
|
210
211
|
},
|
|
211
|
-
Terminated(){
|
|
212
|
-
SLogger.warn(`第 ${
|
|
213
|
-
return
|
|
212
|
+
[Terminated](){
|
|
213
|
+
SLogger.warn(`第 ${postresult.index+1} 次 repeatPromise 终止 停止重试`);
|
|
214
|
+
return postresult.result;
|
|
214
215
|
},
|
|
215
|
-
Failed(){
|
|
216
|
+
[Failed]():None{
|
|
216
217
|
//抛弃失败
|
|
217
|
-
plist[
|
|
218
|
+
plist[postresult.index] = UtilFunc.getNeverResolvedPromise();
|
|
218
219
|
//是当前
|
|
219
|
-
if(
|
|
220
|
-
SLogger.warn(`第 ${
|
|
221
|
-
|
|
222
|
-
return;
|
|
220
|
+
if(postresult.index==i){
|
|
221
|
+
SLogger.warn(`第 ${postresult.index+1} 次 repeatPromise 失败 开始重试`);
|
|
222
|
+
i++;
|
|
223
|
+
return None;
|
|
223
224
|
}
|
|
224
225
|
//非当前
|
|
225
|
-
SLogger.warn(`第 ${
|
|
226
|
+
SLogger.warn(`第 ${postresult.index+1} 次 repeatPromise 失败`);
|
|
227
|
+
return None;
|
|
226
228
|
},
|
|
227
229
|
});
|
|
228
|
-
if(result !==
|
|
230
|
+
if(result !== None) return result;
|
|
229
231
|
}
|
|
230
232
|
//全部失败或超时则返回null
|
|
231
233
|
SLogger.warn(`${repeatCount} 次 repeatPromise 尝试均失败`);
|
|
@@ -233,13 +235,47 @@ export async function repeatPromise<T>(procFn:()=>Promise<T>,verifyFn?:PromiseVe
|
|
|
233
235
|
}catch(err){
|
|
234
236
|
SLogger.warn(`repeatPromise 发生错误`,err);
|
|
235
237
|
return null;
|
|
236
|
-
}finally{
|
|
237
|
-
//清理
|
|
238
|
-
clearTimer();
|
|
239
|
-
SLogger.timeEnd(timeflag);
|
|
240
238
|
}
|
|
241
239
|
}
|
|
242
240
|
|
|
241
|
+
|
|
242
|
+
/**创建一个限时的Promise
|
|
243
|
+
* @param func - 处理函数
|
|
244
|
+
* @param timeLimit - 毫秒限时
|
|
245
|
+
* @returns 超时则返回 处理函数委托 完成则返回结果
|
|
246
|
+
*/
|
|
247
|
+
static timelimitPromise<T>
|
|
248
|
+
(func:()=>Promise<T>|T,timeLimit?:number):Promise<CompleteCome<T>|TimeoutCome<T>>{
|
|
249
|
+
return new Promise<CompleteCome<T>|TimeoutCome<T>>((reslove)=>{
|
|
250
|
+
let clearTimer:(()=>void)| null = null;
|
|
251
|
+
const procer = (async ()=>await func())();
|
|
252
|
+
|
|
253
|
+
const procerP = new Promise<CompleteCome<T>>(async (resolve)=>{
|
|
254
|
+
const res = await procer;
|
|
255
|
+
resolve(outcome(Completed,res));
|
|
256
|
+
if(clearTimer) clearTimer();
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
const timerP = timeLimit
|
|
260
|
+
? new Promise<TimeoutCome<T>>((resolve)=>{
|
|
261
|
+
const timer = setTimeout(()=>
|
|
262
|
+
resolve(outcome(Timeout,procer))
|
|
263
|
+
,timeLimit);//无限制则无限时间
|
|
264
|
+
|
|
265
|
+
clearTimer = ()=>{
|
|
266
|
+
resolve(outcome(Timeout,procer))
|
|
267
|
+
clearInterval(timer)
|
|
268
|
+
}
|
|
269
|
+
})
|
|
270
|
+
: UtilFunc.getNeverResolvedPromise<TimeoutCome<T>>();
|
|
271
|
+
|
|
272
|
+
const result = Promise.race([procerP,timerP]);
|
|
273
|
+
reslove(result);
|
|
274
|
+
})
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
|
|
243
279
|
/**部分类组合
|
|
244
280
|
* 将mixin的部分字段混入base
|
|
245
281
|
* @param base - 基础类
|
|
@@ -248,7 +284,7 @@ export async function repeatPromise<T>(procFn:()=>Promise<T>,verifyFn?:PromiseVe
|
|
|
248
284
|
* @param fields - 需要混入的字段
|
|
249
285
|
* @returns 混合完成的类
|
|
250
286
|
*/
|
|
251
|
-
|
|
287
|
+
static composeClassPart
|
|
252
288
|
<Base extends object,Mixin extends object,Field extends keyof Mixin>
|
|
253
289
|
(base:Base,mixin:Mixin,key:string,...fields:Field[]):ComposedClass<Base,Mixin,typeof key,Field>{
|
|
254
290
|
const compObj = base as any;
|
|
@@ -283,7 +319,7 @@ export function composeClassPart
|
|
|
283
319
|
}
|
|
284
320
|
|
|
285
321
|
/**根据 MIXIN_FIELDS 自动混入 */
|
|
286
|
-
|
|
322
|
+
static composeMixinable
|
|
287
323
|
<Base extends object, Mixins extends Mixinable<any>[]>
|
|
288
324
|
(base:Base,...mixins:Mixins):
|
|
289
325
|
ComposedMixinable<Base,Mixins>{
|
|
@@ -298,7 +334,7 @@ export function composeMixinable
|
|
|
298
334
|
else
|
|
299
335
|
fieldsSet.add(fixField);
|
|
300
336
|
}
|
|
301
|
-
out = composeClassPart(base,mixin,mixin.MIXIN_KEY,...mixin.MIXIN_FIELDS);
|
|
337
|
+
out = UtilFunc.composeClassPart(base,mixin,mixin.MIXIN_KEY,...mixin.MIXIN_FIELDS);
|
|
302
338
|
}
|
|
303
339
|
return out as any;
|
|
304
340
|
}
|
|
@@ -310,7 +346,7 @@ export function composeMixinable
|
|
|
310
346
|
* @param mapper - 映射函数,接受一个值和一个键,返回一个新的值
|
|
311
347
|
* @returns - 一个新的对象,它的属性是原对象的属性经过映射函数处理后的结果
|
|
312
348
|
*/
|
|
313
|
-
|
|
349
|
+
static mapEntries<T extends Object>
|
|
314
350
|
(obj: T, mapper: (key: keyof T, value: T[keyof T]) => T[keyof T]): T {
|
|
315
351
|
return Object.entries(obj).reduce((result, [key, value]) => {
|
|
316
352
|
result[key as keyof T] = mapper(key as keyof T, value);
|
|
@@ -318,33 +354,12 @@ export function mapEntries<T extends Object>
|
|
|
318
354
|
}, {} as T);
|
|
319
355
|
}
|
|
320
356
|
|
|
321
|
-
/**处理联合 Outcome
|
|
322
|
-
* @param t - 目标值
|
|
323
|
-
* @param procObj - 所有可能的id组成的处理函数映射
|
|
324
|
-
* @returns 任意处理函数的返回值
|
|
325
|
-
*/
|
|
326
|
-
export function matchOutcome<
|
|
327
|
-
T extends Outcome<string,unknown>,
|
|
328
|
-
P extends {[K in T['status']]:(k:K,v:ExtractOutcome<T,K>['result'])=>unknown}>
|
|
329
|
-
(t:T,procObj:P):{[K in keyof P]:ReturnType<P[K]>}[keyof P]{
|
|
330
|
-
return (procObj as any)[t.status](t.status,(t as any)?.result)
|
|
331
|
-
}
|
|
332
|
-
/**处理联合 字符串
|
|
333
|
-
* @param t - 目标值
|
|
334
|
-
* @param procObj - 所有可能的id组成的处理函数映射
|
|
335
|
-
* @returns 任意处理函数的返回值
|
|
336
|
-
*/
|
|
337
|
-
export function matchStr<T extends string,P extends {[K in T]:(k:K)=>unknown}>
|
|
338
|
-
(t:T,procObj:P):{[K in keyof P]:ReturnType<P[K]>}[keyof P]{
|
|
339
|
-
return (procObj as any)[t](t)
|
|
340
|
-
}
|
|
341
|
-
|
|
342
357
|
/**将JToken转换为字符串
|
|
343
358
|
* @param token - 待转换的Token
|
|
344
359
|
* @param space - 插入的空格 数字为空格数量 默认为制表符\t
|
|
345
360
|
* @returns 转换完成的字符串
|
|
346
361
|
*/
|
|
347
|
-
|
|
362
|
+
static stringifyJToken(token:JToken|IJData,space:string|number|null|undefined="\t"){
|
|
348
363
|
if(space==null)
|
|
349
364
|
space=undefined;
|
|
350
365
|
return JSON.stringify(token,null,space);
|
package/src/UtilInterfaces.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Completed, Failed, Terminated } from "./UtilSymbol";
|
|
1
2
|
|
|
2
3
|
|
|
3
4
|
/**可以序列化为JSON文件的对象 */
|
|
@@ -17,6 +18,9 @@ export interface IJData{
|
|
|
17
18
|
toJSON():JToken;
|
|
18
19
|
}
|
|
19
20
|
|
|
21
|
+
/**任意可作为键值的类型 */
|
|
22
|
+
export type Keyable = string | number | symbol;
|
|
23
|
+
|
|
20
24
|
/**转为可写的 */
|
|
21
25
|
export type Writeable<T> = {
|
|
22
26
|
-readonly [P in keyof T]: T[P]
|
|
@@ -67,7 +71,7 @@ export type ExclusiveJObject<B extends JObject,T extends JToken,K extends string
|
|
|
67
71
|
* 终止 将直接返回 null
|
|
68
72
|
* 失败 将重试
|
|
69
73
|
*/
|
|
70
|
-
export type PromiseStat =
|
|
74
|
+
export type PromiseStat = Completed|Failed|Terminated;
|
|
71
75
|
/**promise验证函数 */
|
|
72
76
|
export type PromiseVerifyFn<T> = (obj:T)=>Promise<PromiseStat>|PromiseStat;
|
|
73
77
|
|
|
@@ -139,7 +143,7 @@ export type WithPrefix<T,P extends string> = {
|
|
|
139
143
|
* @template K - 类型键值
|
|
140
144
|
* @template V - 值
|
|
141
145
|
*/
|
|
142
|
-
export type Outcome<K extends
|
|
146
|
+
export type Outcome<K extends Keyable,V> = {
|
|
143
147
|
/**状态类型 */
|
|
144
148
|
status:K;
|
|
145
149
|
/**值 */
|
|
@@ -147,6 +151,6 @@ export type Outcome<K extends string,V> = {
|
|
|
147
151
|
}
|
|
148
152
|
|
|
149
153
|
/**从联合 Outcome 中 根据 id 提取对应 value 的 type */
|
|
150
|
-
export type ExtractOutcome<T,K extends
|
|
154
|
+
export type ExtractOutcome<T,K extends Keyable> =
|
|
151
155
|
T extends Outcome<K,unknown> ? T : never;
|
|
152
156
|
|