@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.
@@ -0,0 +1,15 @@
1
+ import { ExtractOutcome, Keyable, Outcome } from "./UtilInterfaces";
2
+ /**创建一个outcome */
3
+ export declare function outcome<K extends Keyable, V>(key: K, value: V): Outcome<K, V>;
4
+ /**处理联合 简单值
5
+ * @param t - 目标值
6
+ * @param procObj - 所有可能的id组成的处理函数映射
7
+ * @returns 任意处理函数的返回值
8
+ */
9
+ export declare function match<T extends Keyable | Outcome<Keyable, unknown>, P extends (T extends Keyable ? {
10
+ [K in T]: (k: K) => unknown;
11
+ } : T extends Outcome<Keyable, unknown> ? {
12
+ [K in T['status']]: (k: K, v: ExtractOutcome<T, K>['result']) => unknown;
13
+ } : never)>(t: T, procObj: P): P extends Record<any, (...args: any) => any> ? {
14
+ [K in keyof P]: ReturnType<P[K]>;
15
+ }[keyof P] : never;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.match = exports.outcome = void 0;
4
+ /**创建一个outcome */
5
+ function outcome(key, value) {
6
+ return { status: key, result: value };
7
+ }
8
+ exports.outcome = outcome;
9
+ /**处理联合 简单值
10
+ * @param t - 目标值
11
+ * @param procObj - 所有可能的id组成的处理函数映射
12
+ * @returns 任意处理函数的返回值
13
+ */
14
+ function match(t, procObj) {
15
+ if (typeof t === 'string' || typeof t === 'number' || typeof t === 'symbol')
16
+ return procObj[t](t);
17
+ else
18
+ return procObj[t.status](t.status, t.result);
19
+ }
20
+ exports.match = match;
21
+ if (false) {
22
+ let a = null;
23
+ const r = match(a, {
24
+ "asd": (a) => "ssa",
25
+ "dsa": (b) => "ssb",
26
+ });
27
+ }
@@ -1,5 +1,15 @@
1
1
  type TDTg<T> = (target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T>;
2
2
  type DTg = (target: Object, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
3
+ /**用于打印方法运行时间
4
+ * @param flag - 时间标签
5
+ * @param suffixUID - 是否尾随uid
6
+ */
7
+ export declare function LogTime(flag: string, suffixUID?: boolean): DTg;
8
+ /**用于打印异步方法运行时间
9
+ * @param flag - 时间标签
10
+ * @param suffixUID - 是否尾随uid
11
+ */
12
+ export declare function LogTimeAsync(flag: string, suffixUID?: boolean): DTg;
3
13
  /**用于打印方法的调用 */
4
14
  export declare function LogCall(): DTg;
5
15
  /**用于打印异步方法的调用 */
@@ -9,8 +9,45 @@ var __metadata = (this && this.__metadata) || function (k, v) {
9
9
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.CatchAsync = exports.DCatch = exports.DeferAsync = exports.Defer = exports.LogErrAsync = exports.LogErr = exports.LogCallAsync = exports.LogCall = void 0;
12
+ exports.CatchAsync = exports.DCatch = exports.DeferAsync = exports.Defer = exports.LogErrAsync = exports.LogErr = exports.LogCallAsync = exports.LogCall = exports.LogTimeAsync = exports.LogTime = void 0;
13
+ const UtilFunctions_1 = require("./UtilFunctions");
13
14
  const UtilLogger_1 = require("./UtilLogger");
15
+ /**用于打印方法运行时间
16
+ * @param flag - 时间标签
17
+ * @param suffixUID - 是否尾随uid
18
+ */
19
+ function LogTime(flag, suffixUID) {
20
+ return function (target, propertyKey, descriptor) {
21
+ const originalMethod = descriptor.value;
22
+ descriptor.value = function (...args) {
23
+ const uid = suffixUID ? UtilFunctions_1.UtilFunc.genUUID() : "";
24
+ UtilLogger_1.SLogger.time(flag + uid);
25
+ let result = originalMethod.apply(this, args);
26
+ UtilLogger_1.SLogger.timeEnd(flag + uid);
27
+ return result;
28
+ };
29
+ return descriptor;
30
+ };
31
+ }
32
+ exports.LogTime = LogTime;
33
+ /**用于打印异步方法运行时间
34
+ * @param flag - 时间标签
35
+ * @param suffixUID - 是否尾随uid
36
+ */
37
+ function LogTimeAsync(flag, suffixUID) {
38
+ return function (target, propertyKey, descriptor) {
39
+ const originalMethod = descriptor.value;
40
+ descriptor.value = async function (...args) {
41
+ const uid = suffixUID ? UtilFunctions_1.UtilFunc.genUUID() : "";
42
+ UtilLogger_1.SLogger.time(flag + uid);
43
+ let result = await originalMethod.apply(this, args);
44
+ UtilLogger_1.SLogger.timeEnd(flag + uid);
45
+ return result;
46
+ };
47
+ return descriptor;
48
+ };
49
+ }
50
+ exports.LogTimeAsync = LogTimeAsync;
14
51
  /**用于打印方法的调用 */
15
52
  function LogCall() {
16
53
  return function (target, propertyKey, descriptor) {
@@ -158,7 +195,7 @@ class Example {
158
195
  myMethod(num) {
159
196
  return num;
160
197
  }
161
- async myMethod1(num, ss) {
198
+ static async myMethod1(num, ss) {
162
199
  return 312;
163
200
  }
164
201
  }
@@ -174,7 +211,7 @@ __decorate([
174
211
  __metadata("design:type", Function),
175
212
  __metadata("design:paramtypes", [Number, String]),
176
213
  __metadata("design:returntype", Promise)
177
- ], Example.prototype, "myMethod1", null);
214
+ ], Example, "myMethod1", null);
178
215
  //let e = new Example();
179
216
  //e.myMethod1(1,"");
180
217
  //console.log(123);
@@ -1,10 +1,13 @@
1
- import { ComposedClass, ComposedMixinable, ExtractOutcome, IJData, JObject, JToken, Mixinable, Outcome, PromiseVerifyFn } from "./UtilInterfaces";
1
+ import { ComposedClass, ComposedMixinable, IJData, JObject, JToken, Mixinable, Outcome, PromiseVerifyFn } from "./UtilInterfaces";
2
+ import { Completed, Timeout } from "./UtilSymbol";
3
+ type CompleteCome<T> = Outcome<Completed, T>;
4
+ type TimeoutCome<T> = Outcome<Timeout, Promise<T>>;
2
5
  /**常用函数 */
3
- export declare namespace UtilFunc {
6
+ export declare class UtilFunc {
4
7
  /**获取当前时间戳
5
8
  * @returns 时间戳
6
9
  */
7
- function getTime(): number;
10
+ static getTime(): number;
8
11
  /**初始化对象的字段
9
12
  * 会改变obj
10
13
  * @param obj - 所要初始化的对象
@@ -12,50 +15,57 @@ export declare namespace UtilFunc {
12
15
  * @param defaultVal - 默认值
13
16
  * @returns 最终值
14
17
  */
15
- function initField<T extends object, K extends keyof T>(obj: T, field: K, defaultVal: T[K]): T[K];
18
+ static initField<T extends object, K extends keyof T>(obj: T, field: K, defaultVal: T[K]): T[K];
16
19
  /**初始化一个数据对象
17
20
  * @param obj - 目标对象
18
21
  * @param checkObj - 用于检测的对象 在对应key缺失时赋予对应值 如果值为函数, 则赋予执行结果 如果结果为 undefined 则不赋值
19
22
  * @returns 完成初始化的对象
20
23
  */
21
- function initObject<T extends JObject>(obj: T, checkObj: {
24
+ static initObject<T extends JObject>(obj: T, checkObj: {
22
25
  [P in keyof T]: T[P] | (() => void | T[P]);
23
26
  }): any;
24
27
  /**生成一串uuid
25
28
  * @returns uuid
26
29
  */
27
- function genUUID(): string;
30
+ static genUUID(): string;
28
31
  /**计算Hash
29
32
  * @param str - 待计算的字符串
30
33
  * @returns hash
31
34
  */
32
- function calcHash(str: string): string;
35
+ static calcHash(str: string): string;
33
36
  /**深克隆 序列化并反序列化
34
37
  * @template T - JToken类型的泛型
35
38
  * @param obj - 克隆目标
36
39
  * @returns 克隆结果
37
40
  */
38
- function deepClone<T extends JToken>(obj: T): T;
41
+ static deepClone<T extends JToken>(obj: T): T;
39
42
  /**是否为安全的数字
40
43
  * @param num - 所要检测的数字
41
44
  * @returns 是否安全
42
45
  */
43
- function isSafeNumber(num: number): boolean;
46
+ static isSafeNumber(num: number): boolean;
44
47
  /**等待 timeMs 毫秒
45
48
  * @async
46
49
  * @param timeMs - 等待的毫秒数
47
50
  * @returns
48
51
  */
49
- function sleep(timeMs: number): Promise<boolean>;
52
+ static sleep<T>(timeMs: number): Promise<void>;
53
+ /**等待 timeMs 毫秒
54
+ * @async
55
+ * @param timeMs - 等待的毫秒数
56
+ * @param result - 结果
57
+ * @returns
58
+ */
59
+ static sleep<T>(timeMs: number, result: T): Promise<T>;
50
60
  /**封装的 cp.exec 执行一段指令 指令完成后返回 Promise
51
61
  * @param command 指令文本
52
62
  */
53
- function exec(command: string): Promise<{
63
+ static exec(command: string): Promise<{
54
64
  stdout: string;
55
65
  stderr: string;
56
66
  }>;
57
67
  /**获得一个永不完成的Promise单例 */
58
- function getNeverResolvedPromise<T>(): Promise<T>;
68
+ static getNeverResolvedPromise<T>(): Promise<T>;
59
69
  /**重复尝试promise
60
70
  * @async
61
71
  * @param procFn - 发起函数
@@ -64,7 +74,13 @@ export declare namespace UtilFunc {
64
74
  * @param repeatTime - 超时时间/秒 最小为10秒
65
75
  * @returns 结果 null 为全部失败/超时
66
76
  */
67
- function repeatPromise<T>(procFn: () => Promise<T>, verifyFn?: PromiseVerifyFn<T>, repeatCount?: number, repeatTime?: number): Promise<T | null>;
77
+ static repeatPromise<T>(procFn: () => Promise<T>, verifyFn?: PromiseVerifyFn<T>, repeatCount?: number, repeatTime?: number): Promise<T | null>;
78
+ /**创建一个限时的Promise
79
+ * @param func - 处理函数
80
+ * @param timeLimit - 毫秒限时
81
+ * @returns 超时则返回 处理函数委托 完成则返回结果
82
+ */
83
+ static timelimitPromise<T>(func: () => Promise<T> | T, timeLimit?: number): Promise<CompleteCome<T> | TimeoutCome<T>>;
68
84
  /**部分类组合
69
85
  * 将mixin的部分字段混入base
70
86
  * @param base - 基础类
@@ -73,40 +89,21 @@ export declare namespace UtilFunc {
73
89
  * @param fields - 需要混入的字段
74
90
  * @returns 混合完成的类
75
91
  */
76
- function composeClassPart<Base extends object, Mixin extends object, Field extends keyof Mixin>(base: Base, mixin: Mixin, key: string, ...fields: Field[]): ComposedClass<Base, Mixin, typeof key, Field>;
92
+ static composeClassPart<Base extends object, Mixin extends object, Field extends keyof Mixin>(base: Base, mixin: Mixin, key: string, ...fields: Field[]): ComposedClass<Base, Mixin, typeof key, Field>;
77
93
  /**根据 MIXIN_FIELDS 自动混入 */
78
- function composeMixinable<Base extends object, Mixins extends Mixinable<any>[]>(base: Base, ...mixins: Mixins): ComposedMixinable<Base, Mixins>;
94
+ static composeMixinable<Base extends object, Mixins extends Mixinable<any>[]>(base: Base, ...mixins: Mixins): ComposedMixinable<Base, Mixins>;
79
95
  /**对对象的每个属性应用映射函数,并返回一个新的对象。
80
96
  * @template T - 对象的类型
81
97
  * @param obj - 要处理的对象
82
98
  * @param mapper - 映射函数,接受一个值和一个键,返回一个新的值
83
99
  * @returns - 一个新的对象,它的属性是原对象的属性经过映射函数处理后的结果
84
100
  */
85
- function mapEntries<T extends Object>(obj: T, mapper: (key: keyof T, value: T[keyof T]) => T[keyof T]): T;
86
- /**处理联合 Outcome
87
- * @param t - 目标值
88
- * @param procObj - 所有可能的id组成的处理函数映射
89
- * @returns 任意处理函数的返回值
90
- */
91
- function matchOutcome<T extends Outcome<string, unknown>, P extends {
92
- [K in T['status']]: (k: K, v: ExtractOutcome<T, K>['result']) => unknown;
93
- }>(t: T, procObj: P): {
94
- [K in keyof P]: ReturnType<P[K]>;
95
- }[keyof P];
96
- /**处理联合 字符串
97
- * @param t - 目标值
98
- * @param procObj - 所有可能的id组成的处理函数映射
99
- * @returns 任意处理函数的返回值
100
- */
101
- function matchStr<T extends string, P extends {
102
- [K in T]: (k: K) => unknown;
103
- }>(t: T, procObj: P): {
104
- [K in keyof P]: ReturnType<P[K]>;
105
- }[keyof P];
101
+ static mapEntries<T extends Object>(obj: T, mapper: (key: keyof T, value: T[keyof T]) => T[keyof T]): T;
106
102
  /**将JToken转换为字符串
107
103
  * @param token - 待转换的Token
108
104
  * @param space - 插入的空格 数字为空格数量 默认为制表符\t
109
105
  * @returns 转换完成的字符串
110
106
  */
111
- function stringifyJToken(token: JToken | IJData, space?: string | number | null | undefined): string;
107
+ static stringifyJToken(token: JToken | IJData, space?: string | number | null | undefined): string;
112
108
  }
109
+ export {};
@@ -1,19 +1,31 @@
1
1
  "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
12
  exports.UtilFunc = void 0;
4
13
  const crypto = require("crypto");
5
14
  const cp = require("child_process");
6
15
  const UtilLogger_1 = require("./UtilLogger");
16
+ const UtilSymbol_1 = require("./UtilSymbol");
17
+ const QuickFunction_1 = require("./QuickFunction");
18
+ const UtilDecorators_1 = require("./UtilDecorators");
19
+ /**永不完成的Promise单例 */
20
+ const NeverResolvedPromise = new Promise(() => { });
7
21
  /**常用函数 */
8
- var UtilFunc;
9
- (function (UtilFunc) {
22
+ class UtilFunc {
10
23
  /**获取当前时间戳
11
24
  * @returns 时间戳
12
25
  */
13
- function getTime() {
26
+ static getTime() {
14
27
  return new Date().getTime();
15
28
  }
16
- UtilFunc.getTime = getTime;
17
29
  /**初始化对象的字段
18
30
  * 会改变obj
19
31
  * @param obj - 所要初始化的对象
@@ -21,18 +33,17 @@ var UtilFunc;
21
33
  * @param defaultVal - 默认值
22
34
  * @returns 最终值
23
35
  */
24
- function initField(obj, field, defaultVal) {
36
+ static initField(obj, field, defaultVal) {
25
37
  if (!(field in obj))
26
38
  obj[field] = defaultVal;
27
39
  return obj[field];
28
40
  }
29
- UtilFunc.initField = initField;
30
41
  /**初始化一个数据对象
31
42
  * @param obj - 目标对象
32
43
  * @param checkObj - 用于检测的对象 在对应key缺失时赋予对应值 如果值为函数, 则赋予执行结果 如果结果为 undefined 则不赋值
33
44
  * @returns 完成初始化的对象
34
45
  */
35
- function initObject(obj, checkObj) {
46
+ static initObject(obj, checkObj) {
36
47
  const fixobj = obj;
37
48
  for (const key in checkObj) {
38
49
  if (obj[key] !== undefined)
@@ -48,60 +59,49 @@ var UtilFunc;
48
59
  }
49
60
  return fixobj;
50
61
  }
51
- UtilFunc.initObject = initObject;
52
62
  /**生成一串uuid
53
63
  * @returns uuid
54
64
  */
55
- function genUUID() {
65
+ static genUUID() {
56
66
  return crypto.randomBytes(16).toString("hex");
57
67
  }
58
- UtilFunc.genUUID = genUUID;
59
68
  /**计算Hash
60
69
  * @param str - 待计算的字符串
61
70
  * @returns hash
62
71
  */
63
- function calcHash(str) {
72
+ static calcHash(str) {
64
73
  return crypto.createHash('md5').update(str).digest('hex');
65
74
  }
66
- UtilFunc.calcHash = calcHash;
67
75
  /**深克隆 序列化并反序列化
68
76
  * @template T - JToken类型的泛型
69
77
  * @param obj - 克隆目标
70
78
  * @returns 克隆结果
71
79
  */
72
- function deepClone(obj) {
80
+ static deepClone(obj) {
73
81
  return JSON.parse(JSON.stringify(obj));
74
82
  }
75
- UtilFunc.deepClone = deepClone;
76
83
  /**是否为安全的数字
77
84
  * @param num - 所要检测的数字
78
85
  * @returns 是否安全
79
86
  */
80
- function isSafeNumber(num) {
87
+ static isSafeNumber(num) {
81
88
  if (num === undefined || num == null || isNaN(num))
82
89
  return false;
83
90
  if (typeof num === 'number')
84
91
  return true;
85
92
  return false;
86
93
  }
87
- UtilFunc.isSafeNumber = isSafeNumber;
88
- /**等待 timeMs 毫秒
89
- * @async
90
- * @param timeMs - 等待的毫秒数
91
- * @returns
92
- */
93
- async function sleep(timeMs) {
94
+ static async sleep(timeMs, result) {
94
95
  return new Promise(function (resolve, rejecte) {
95
96
  let timer = setTimeout(function () {
96
- resolve(true);
97
+ resolve(result);
97
98
  }, timeMs);
98
99
  });
99
100
  }
100
- UtilFunc.sleep = sleep;
101
101
  /**封装的 cp.exec 执行一段指令 指令完成后返回 Promise
102
102
  * @param command 指令文本
103
103
  */
104
- function exec(command) {
104
+ static exec(command) {
105
105
  return new Promise((resolve, reject) => {
106
106
  cp.exec(command, (error, stdout, stderr) => {
107
107
  if (error)
@@ -111,14 +111,10 @@ var UtilFunc;
111
111
  });
112
112
  });
113
113
  }
114
- UtilFunc.exec = exec;
115
- /**永不完成的Promise单例 */
116
- const NeverResolvedPromise = new Promise(() => { });
117
114
  /**获得一个永不完成的Promise单例 */
118
- function getNeverResolvedPromise() {
115
+ static getNeverResolvedPromise() {
119
116
  return NeverResolvedPromise;
120
117
  }
121
- UtilFunc.getNeverResolvedPromise = getNeverResolvedPromise;
122
118
  /**重复尝试promise
123
119
  * @async
124
120
  * @param procFn - 发起函数
@@ -127,10 +123,7 @@ var UtilFunc;
127
123
  * @param repeatTime - 超时时间/秒 最小为10秒
128
124
  * @returns 结果 null 为全部失败/超时
129
125
  */
130
- async function repeatPromise(procFn, verifyFn, repeatCount = 3, repeatTime = 180) {
131
- //计时
132
- const timeflag = "repeatPromise " + UtilFunc.genUUID();
133
- UtilLogger_1.SLogger.time(timeflag);
126
+ static async repeatPromise(procFn, verifyFn, repeatCount = 3, repeatTime = 180) {
134
127
  /**是否含有超时时间 */
135
128
  const hasRepeatTime = (repeatTime >= 10);
136
129
  //转换为毫秒
@@ -138,21 +131,7 @@ var UtilFunc;
138
131
  repeatTime *= 1000;
139
132
  //验证处理函数
140
133
  if (verifyFn === undefined)
141
- verifyFn = () => "Completed";
142
- /**计时器 */
143
- let timer = null;
144
- /**计时器 Promise */
145
- let timerP = null;
146
- /**计时器 Promise 的返回函数 */
147
- let resolveFn = null;
148
- /**清理计时器 */
149
- const clearTimer = () => {
150
- if (timer)
151
- clearInterval(timer);
152
- if (resolveFn)
153
- resolveFn("Timeout");
154
- timerP = timer = resolveFn = null;
155
- };
134
+ verifyFn = () => UtilSymbol_1.Completed;
156
135
  //进行中的请求
157
136
  const plist = [];
158
137
  //开始处理
@@ -162,53 +141,55 @@ var UtilFunc;
162
141
  UtilLogger_1.SLogger.info(`开始第 ${i + 1} 次 repeatPromise`);
163
142
  //如果 plist 中当前下标的任务还未创建 则 创建当前任务
164
143
  if (plist.length < i + 1) {
165
- plist.push(procFn().then(result => ({ result, stat: verifyFn(result), index: i })));
166
- }
167
- //创建定时器
168
- if (timerP == null) {
169
- timerP = new Promise((resolve, rejecte) => {
170
- resolveFn = resolve;
171
- timer = setTimeout(() => resolve("Timeout"), hasRepeatTime ? repeatTime : Infinity); //无限制则无限时间
172
- });
144
+ plist.push(UtilFunc.timelimitPromise(async () => {
145
+ const index = i;
146
+ const result = await procFn();
147
+ const stat = await verifyFn(result);
148
+ return { result, stat, index };
149
+ }, hasRepeatTime ? repeatTime : undefined));
173
150
  }
174
151
  //等待任意任务 或当前计时器完成
175
- const currObj = await Promise.race([...plist, timerP]);
152
+ const currObj = await Promise.race([...plist]);
176
153
  //超时处理
177
- if (currObj === "Timeout") {
154
+ if (currObj.status === UtilSymbol_1.Timeout) {
155
+ //解除timeout替换原参数
156
+ plist[i] = new Promise(async (rslove) => {
157
+ const res = await currObj.result;
158
+ rslove((0, QuickFunction_1.outcome)(UtilSymbol_1.Completed, res));
159
+ });
178
160
  UtilLogger_1.SLogger.warn(`第 ${i + 1} 次 repeatPromise 超时 ${repeatTime} ms 开始重试`);
179
- clearTimer();
180
161
  i++;
181
162
  continue;
182
163
  }
183
164
  //路由请求状态
184
- const poststat = await currObj.stat;
185
- const result = matchStr(poststat, {
186
- Completed() {
187
- UtilLogger_1.SLogger.info(`第 ${currObj.index + 1} 次 repeatPromise 成功`);
165
+ const postresult = currObj.result;
166
+ const result = (0, QuickFunction_1.match)(postresult.stat, {
167
+ [UtilSymbol_1.Completed]() {
168
+ UtilLogger_1.SLogger.info(`第 ${postresult.index + 1} 次 repeatPromise 成功`);
188
169
  //非当前
189
- if (currObj.index != i)
170
+ if (postresult.index != i)
190
171
  UtilLogger_1.SLogger.info(`成功的 promise 非当前 promise 考虑增大重试间隔\n当前index: ${i}\n当前间隔: ${repeatTime}`);
191
- return currObj.result;
172
+ return postresult.result;
192
173
  },
193
- Terminated() {
194
- UtilLogger_1.SLogger.warn(`第 ${currObj.index + 1} 次 repeatPromise 终止 停止重试`);
195
- return currObj.result;
174
+ [UtilSymbol_1.Terminated]() {
175
+ UtilLogger_1.SLogger.warn(`第 ${postresult.index + 1} 次 repeatPromise 终止 停止重试`);
176
+ return postresult.result;
196
177
  },
197
- Failed() {
178
+ [UtilSymbol_1.Failed]() {
198
179
  //抛弃失败
199
- plist[currObj.index] = UtilFunc.getNeverResolvedPromise();
180
+ plist[postresult.index] = UtilFunc.getNeverResolvedPromise();
200
181
  //是当前
201
- if (currObj.index == i) {
202
- UtilLogger_1.SLogger.warn(`第 ${currObj.index + 1} 次 repeatPromise 失败 开始重试`);
203
- clearTimer();
182
+ if (postresult.index == i) {
183
+ UtilLogger_1.SLogger.warn(`第 ${postresult.index + 1} 次 repeatPromise 失败 开始重试`);
204
184
  i++;
205
- return;
185
+ return UtilSymbol_1.None;
206
186
  }
207
187
  //非当前
208
- UtilLogger_1.SLogger.warn(`第 ${currObj.index + 1} 次 repeatPromise 失败`);
188
+ UtilLogger_1.SLogger.warn(`第 ${postresult.index + 1} 次 repeatPromise 失败`);
189
+ return UtilSymbol_1.None;
209
190
  },
210
191
  });
211
- if (result !== undefined)
192
+ if (result !== UtilSymbol_1.None)
212
193
  return result;
213
194
  }
214
195
  //全部失败或超时则返回null
@@ -219,13 +200,35 @@ var UtilFunc;
219
200
  UtilLogger_1.SLogger.warn(`repeatPromise 发生错误`, err);
220
201
  return null;
221
202
  }
222
- finally {
223
- //清理
224
- clearTimer();
225
- UtilLogger_1.SLogger.timeEnd(timeflag);
226
- }
227
203
  }
228
- UtilFunc.repeatPromise = repeatPromise;
204
+ /**创建一个限时的Promise
205
+ * @param func - 处理函数
206
+ * @param timeLimit - 毫秒限时
207
+ * @returns 超时则返回 处理函数委托 完成则返回结果
208
+ */
209
+ static timelimitPromise(func, timeLimit) {
210
+ return new Promise((reslove) => {
211
+ let clearTimer = null;
212
+ const procer = (async () => await func())();
213
+ const procerP = new Promise(async (resolve) => {
214
+ const res = await procer;
215
+ resolve((0, QuickFunction_1.outcome)(UtilSymbol_1.Completed, res));
216
+ if (clearTimer)
217
+ clearTimer();
218
+ });
219
+ const timerP = timeLimit
220
+ ? new Promise((resolve) => {
221
+ const timer = setTimeout(() => resolve((0, QuickFunction_1.outcome)(UtilSymbol_1.Timeout, procer)), timeLimit); //无限制则无限时间
222
+ clearTimer = () => {
223
+ resolve((0, QuickFunction_1.outcome)(UtilSymbol_1.Timeout, procer));
224
+ clearInterval(timer);
225
+ };
226
+ })
227
+ : UtilFunc.getNeverResolvedPromise();
228
+ const result = Promise.race([procerP, timerP]);
229
+ reslove(result);
230
+ });
231
+ }
229
232
  /**部分类组合
230
233
  * 将mixin的部分字段混入base
231
234
  * @param base - 基础类
@@ -234,7 +237,7 @@ var UtilFunc;
234
237
  * @param fields - 需要混入的字段
235
238
  * @returns 混合完成的类
236
239
  */
237
- function composeClassPart(base, mixin, key, ...fields) {
240
+ static composeClassPart(base, mixin, key, ...fields) {
238
241
  const compObj = base;
239
242
  if (compObj[key] !== undefined)
240
243
  UtilLogger_1.SLogger.warn(`key: ${key} 已存在于基础类中, 混入可能导致类型问题`);
@@ -266,9 +269,8 @@ var UtilFunc;
266
269
  }
267
270
  return compObj;
268
271
  }
269
- UtilFunc.composeClassPart = composeClassPart;
270
272
  /**根据 MIXIN_FIELDS 自动混入 */
271
- function composeMixinable(base, ...mixins) {
273
+ static composeMixinable(base, ...mixins) {
272
274
  let out = base;
273
275
  const fieldsSet = new Set();
274
276
  for (const rawmixin of mixins) {
@@ -280,51 +282,37 @@ var UtilFunc;
280
282
  else
281
283
  fieldsSet.add(fixField);
282
284
  }
283
- out = composeClassPart(base, mixin, mixin.MIXIN_KEY, ...mixin.MIXIN_FIELDS);
285
+ out = UtilFunc.composeClassPart(base, mixin, mixin.MIXIN_KEY, ...mixin.MIXIN_FIELDS);
284
286
  }
285
287
  return out;
286
288
  }
287
- UtilFunc.composeMixinable = composeMixinable;
288
289
  /**对对象的每个属性应用映射函数,并返回一个新的对象。
289
290
  * @template T - 对象的类型
290
291
  * @param obj - 要处理的对象
291
292
  * @param mapper - 映射函数,接受一个值和一个键,返回一个新的值
292
293
  * @returns - 一个新的对象,它的属性是原对象的属性经过映射函数处理后的结果
293
294
  */
294
- function mapEntries(obj, mapper) {
295
+ static mapEntries(obj, mapper) {
295
296
  return Object.entries(obj).reduce((result, [key, value]) => {
296
297
  result[key] = mapper(key, value);
297
298
  return result;
298
299
  }, {});
299
300
  }
300
- UtilFunc.mapEntries = mapEntries;
301
- /**处理联合 Outcome
302
- * @param t - 目标值
303
- * @param procObj - 所有可能的id组成的处理函数映射
304
- * @returns 任意处理函数的返回值
305
- */
306
- function matchOutcome(t, procObj) {
307
- return procObj[t.status](t.status, t?.result);
308
- }
309
- UtilFunc.matchOutcome = matchOutcome;
310
- /**处理联合 字符串
311
- * @param t - 目标值
312
- * @param procObj - 所有可能的id组成的处理函数映射
313
- * @returns 任意处理函数的返回值
314
- */
315
- function matchStr(t, procObj) {
316
- return procObj[t](t);
317
- }
318
- UtilFunc.matchStr = matchStr;
319
301
  /**将JToken转换为字符串
320
302
  * @param token - 待转换的Token
321
303
  * @param space - 插入的空格 数字为空格数量 默认为制表符\t
322
304
  * @returns 转换完成的字符串
323
305
  */
324
- function stringifyJToken(token, space = "\t") {
306
+ static stringifyJToken(token, space = "\t") {
325
307
  if (space == null)
326
308
  space = undefined;
327
309
  return JSON.stringify(token, null, space);
328
310
  }
329
- UtilFunc.stringifyJToken = stringifyJToken;
330
- })(UtilFunc = exports.UtilFunc || (exports.UtilFunc = {}));
311
+ }
312
+ __decorate([
313
+ (0, UtilDecorators_1.LogTimeAsync)("repeatPromise ", true),
314
+ __metadata("design:type", Function),
315
+ __metadata("design:paramtypes", [Function, Function, Number, Number]),
316
+ __metadata("design:returntype", Promise)
317
+ ], UtilFunc, "repeatPromise", null);
318
+ exports.UtilFunc = UtilFunc;