@zwa73/utils 1.0.128 → 1.0.130

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/UtilCom.d.ts CHANGED
@@ -1,4 +1,9 @@
1
1
  import { JObject, ReqVerifyFn } from "./UtilInterfaces";
2
+ import { RepeatPromiseOpt } from "./UtilFunctions";
3
+ type RepeatPostOpt = Partial<{
4
+ /**httppost 超时中断时间限制 */
5
+ post_timelimit?: number;
6
+ }> & RepeatPromiseOpt;
2
7
  /**网络工具 */
3
8
  export declare namespace UtilCom {
4
9
  /**发送一个 https POST请求并接受数据
@@ -19,25 +24,30 @@ export declare namespace UtilCom {
19
24
  function shttpPost(json: JObject, options: Object, timeLimit?: number): Promise<JObject | null>;
20
25
  /**重复一个 https POST请求并接受数据
21
26
  * @async
22
- * @param json - 数据对象
23
- * @param options - 参数对象
24
- * @param timeLimit - 超时时间/秒 最小为10秒
25
- * @param repeatCount - 重试次数
26
- * @param repeatTime - 超时时间/秒 最小为10秒
27
- * @param verifyFn - 判断有效性函数
27
+ * @param json - 数据对象
28
+ * @param options - 参数对象
29
+ * @param verifyFn - 判断有效性函数
30
+ * @param opt - 可选参数
31
+ * @param opt.post_timelimit - 超时时间/秒 最小为10秒 默认无限
32
+ * @param opt.count - 重试次数 默认3
33
+ * @param opt.try_interval - 重试超时时间/秒 最小为5秒 默认180
34
+ * @param opt.try_delay - 重试间隔 秒 默认0
28
35
  * @returns 结果 null 为未能成功接收
29
36
  */
30
- function shttpsRepeatPost(json: JObject, options: Object, timeLimit?: number, repeatCount?: number, repeatTime?: number, verifyFn?: ReqVerifyFn<JObject | null>): Promise<JObject | null>;
37
+ function shttpsRepeatPost(json: JObject, options: Object, verifyFn?: ReqVerifyFn<JObject | null>, opt?: RepeatPostOpt): Promise<JObject | null>;
31
38
  /**重复一个 http POST请求并接受数据
32
39
  * Object ()
33
40
  * @async
34
- * @param json - 数据对象
35
- * @param options - 参数对象
36
- * @param timeLimit - 超时时间/秒 最小为10秒
37
- * @param repeatCount - 重试次数
38
- * @param repeatTime - 超时时间/秒 最小为10秒
39
- * @param verifyFn - 判断有效性函数
41
+ * @param json - 数据对象
42
+ * @param options - 参数对象
43
+ * @param verifyFn - 判断有效性函数
44
+ * @param opt - 可选参数
45
+ * @param opt.post_timelimit - 超时时间/秒 最小为10秒 默认无限
46
+ * @param opt.count - 重试次数 默认3
47
+ * @param opt.try_interval - 重试超时时间/秒 最小为5秒 默认180
48
+ * @param opt.try_delay - 重试间隔 秒 默认0
40
49
  * @returns 结果 null 为未能成功接收
41
50
  */
42
- function shttpRepeatPost(json: JObject, options: Object, timeLimit?: number, repeatCount?: number, repeatTime?: number, verifyFn?: ReqVerifyFn<JObject | null>): Promise<JObject | null>;
51
+ function shttpRepeatPost(json: JObject, options: Object, verifyFn?: ReqVerifyFn<JObject | null>, opt?: RepeatPostOpt): Promise<JObject | null>;
43
52
  }
53
+ export {};
package/dist/UtilCom.js CHANGED
@@ -38,11 +38,12 @@ var UtilCom;
38
38
  * @param timeLimit - 超时时间/秒 最小为10秒
39
39
  * @returns 结果 null 为未能成功接收
40
40
  */
41
- function sPost(posttype, json, options, timeLimit = -1) {
41
+ function sPost(posttype, json, options, timeLimit) {
42
42
  //转换为毫秒
43
- const hasTimeLimit = (timeLimit >= 10);
44
- if (hasTimeLimit)
43
+ const hasTimeLimit = (timeLimit ? timeLimit >= 10 : false);
44
+ if (hasTimeLimit && timeLimit != undefined)
45
45
  timeLimit *= 1000;
46
+ const fixlimit = timeLimit;
46
47
  const jsonStr = UtilFunctions_1.UtilFunc.stringifyJToken(json);
47
48
  const funcName = `s${posttype}Psot`;
48
49
  return new Promise((resolve, rejecte) => {
@@ -50,7 +51,7 @@ var UtilCom;
50
51
  try {
51
52
  //请求超时
52
53
  if (hasTimeLimit) {
53
- res.setTimeout(timeLimit, () => {
54
+ res.setTimeout(fixlimit, () => {
54
55
  //res.abort();
55
56
  UtilLogger_1.SLogger.warn(`${funcName} 接收反馈超时: ${timeLimit} ms`);
56
57
  resolve(null);
@@ -98,7 +99,7 @@ var UtilCom;
98
99
  req = http.request(options, resFunc);
99
100
  //请求超时
100
101
  if (hasTimeLimit) {
101
- req.setTimeout(timeLimit, () => {
102
+ req.setTimeout(fixlimit, () => {
102
103
  UtilLogger_1.SLogger.warn(`${funcName} 发送请求超时: ${timeLimit} ms`);
103
104
  req.destroy();
104
105
  });
@@ -118,7 +119,7 @@ var UtilCom;
118
119
  * @param timeLimit - 超时时间/秒 最小为10秒
119
120
  * @returns 结果 null 为未能成功接收
120
121
  */
121
- function shttpsPost(json, options, timeLimit = -1) {
122
+ function shttpsPost(json, options, timeLimit) {
122
123
  return sPost("https", json, options, timeLimit);
123
124
  }
124
125
  UtilCom.shttpsPost = shttpsPost;
@@ -129,52 +130,61 @@ var UtilCom;
129
130
  * @param timeLimit - 超时时间/秒 最小为10秒
130
131
  * @returns 结果 null 为未能成功接收
131
132
  */
132
- function shttpPost(json, options, timeLimit = -1) {
133
+ function shttpPost(json, options, timeLimit) {
133
134
  return sPost("http", json, options, timeLimit);
134
135
  }
135
136
  UtilCom.shttpPost = shttpPost;
136
137
  /**通用重复post处理
137
138
  * @async
138
- * @param posttype - post类型
139
- * @param json - 数据对象
140
- * @param options - 参数对象
141
- * @param timeLimit - 超时时间/秒 最小为10秒
142
- * @param repeatCount - 重试次数
143
- * @param repeatTime - 超时时间/秒 最小为10秒
144
- * @param verifyFn - 判断有效性函数
139
+ * @param posttype - post类型
140
+ * @param json - 数据对象
141
+ * @param options - 参数对象
142
+ * @param verifyFn - 判断有效性函数
143
+ * @param opt - 可选参数
144
+ * @param opt.post_timelimit - 超时时间/秒 最小为10秒 默认无限
145
+ * @param opt.count - 重试次数 默认3
146
+ * @param opt.try_interval - 重试超时时间/秒 最小为5秒 默认180
147
+ * @param opt.try_delay - 重试间隔 秒 默认0
145
148
  * @returns 结果 null 为未能成功接收
146
149
  */
147
- async function sRepeatPost(posttype, json, options, timeLimit = -1, repeatCount = 3, repeatTime = 180, verifyFn) {
148
- const procFn = () => sPost(posttype, json, options, timeLimit);
149
- return UtilFunctions_1.UtilFunc.repeatPromise(procFn, verifyFn, repeatCount, repeatTime);
150
+ async function sRepeatPost(posttype, json, options, verifyFn, opt = {}) {
151
+ opt.count = opt.count ?? 3;
152
+ opt.try_interval = opt.try_interval ?? 180;
153
+ opt.try_delay = opt.try_delay ?? 1;
154
+ const procFn = () => sPost(posttype, json, options, opt.post_timelimit);
155
+ return UtilFunctions_1.UtilFunc.repeatPromise(procFn, verifyFn, opt);
150
156
  }
151
157
  /**重复一个 https POST请求并接受数据
152
158
  * @async
153
- * @param json - 数据对象
154
- * @param options - 参数对象
155
- * @param timeLimit - 超时时间/秒 最小为10秒
156
- * @param repeatCount - 重试次数
157
- * @param repeatTime - 超时时间/秒 最小为10秒
158
- * @param verifyFn - 判断有效性函数
159
+ * @param json - 数据对象
160
+ * @param options - 参数对象
161
+ * @param verifyFn - 判断有效性函数
162
+ * @param opt - 可选参数
163
+ * @param opt.post_timelimit - 超时时间/秒 最小为10秒 默认无限
164
+ * @param opt.count - 重试次数 默认3
165
+ * @param opt.try_interval - 重试超时时间/秒 最小为5秒 默认180
166
+ * @param opt.try_delay - 重试间隔 秒 默认0
159
167
  * @returns 结果 null 为未能成功接收
160
168
  */
161
- function shttpsRepeatPost(json, options, timeLimit = -1, repeatCount = 3, repeatTime = 180, verifyFn) {
162
- return sRepeatPost("https", json, options, timeLimit, repeatCount, repeatTime, verifyFn);
169
+ function shttpsRepeatPost(json, options, verifyFn, opt) {
170
+ return sRepeatPost("https", json, options, verifyFn, opt);
163
171
  }
164
172
  UtilCom.shttpsRepeatPost = shttpsRepeatPost;
165
173
  /**重复一个 http POST请求并接受数据
166
174
  * Object ()
167
175
  * @async
168
- * @param json - 数据对象
169
- * @param options - 参数对象
170
- * @param timeLimit - 超时时间/秒 最小为10秒
171
- * @param repeatCount - 重试次数
172
- * @param repeatTime - 超时时间/秒 最小为10秒
173
- * @param verifyFn - 判断有效性函数
176
+ * @param json - 数据对象
177
+ * @param options - 参数对象
178
+ * @param verifyFn - 判断有效性函数
179
+ * @param opt - 可选参数
180
+ * @param opt.post_timelimit - 超时时间/秒 最小为10秒 默认无限
181
+ * @param opt.count - 重试次数 默认3
182
+ * @param opt.try_interval - 重试超时时间/秒 最小为5秒 默认180
183
+ * @param opt.try_delay - 重试间隔 秒 默认0
174
184
  * @returns 结果 null 为未能成功接收
175
185
  */
176
- function shttpRepeatPost(json, options, timeLimit = -1, repeatCount = 3, repeatTime = 180, verifyFn) {
177
- return sRepeatPost("http", json, options, timeLimit, repeatCount, repeatTime, verifyFn);
186
+ function shttpRepeatPost(json, options, verifyFn, opt) {
187
+ return sRepeatPost("http", json, options, verifyFn, opt);
178
188
  }
179
189
  UtilCom.shttpRepeatPost = shttpRepeatPost;
180
190
  })(UtilCom || (exports.UtilCom = UtilCom = {}));
@@ -8,6 +8,17 @@ type ExecOpt = Partial<{
8
8
  outlvl: LogLevel;
9
9
  errlvl: LogLevel;
10
10
  }>;
11
+ /**Promise重试选项 */
12
+ export type RepeatPromiseOpt = Partial<{
13
+ /**重试次数 默认3*/
14
+ count?: number;
15
+ /**尝试间隔时间 超过此事件会重新创建新的Promise
16
+ * 同时等待新的与旧的Promise 秒 默认180
17
+ */
18
+ try_interval?: number;
19
+ /**尝试延迟 重新尝试时会先等待此秒数 秒 默认0*/
20
+ try_delay?: number;
21
+ }>;
11
22
  type SuccessOut<T> = Outcome<Success, T>;
12
23
  type TimeoutOut<T> = Outcome<Timeout, Promise<T>>;
13
24
  /**常用函数 */
@@ -75,11 +86,13 @@ export declare class UtilFunc {
75
86
  * @async
76
87
  * @param procFn - 发起函数
77
88
  * @param verifyFn - 验证函数
78
- * @param repeatCount - 重试次数
79
- * @param repeatTime - 超时时间/秒 最小为5秒
89
+ * @param opt - 可选参数
90
+ * @param opt.count - 重试次数 默认3
91
+ * @param opt.timeout - 超时时间/秒 默认180 最小为5秒
92
+ * @param opt.interval - 重试间隔时间 默认0
80
93
  * @returns 结果 null 为全部失败/超时
81
94
  */
82
- static repeatPromise<T>(procFn: () => Promise<T>, verifyFn?: ReqVerifyFn<T>, repeatCount?: number, repeatTime?: number): Promise<T | null>;
95
+ static repeatPromise<T>(procFn: () => Promise<T>, verifyFn?: ReqVerifyFn<T>, opt?: RepeatPromiseOpt): Promise<T | null>;
83
96
  /**创建一个限时的Promise
84
97
  * @param func - 处理函数
85
98
  * @param timeLimit - 毫秒限时
@@ -166,16 +166,21 @@ class UtilFunc {
166
166
  * @async
167
167
  * @param procFn - 发起函数
168
168
  * @param verifyFn - 验证函数
169
- * @param repeatCount - 重试次数
170
- * @param repeatTime - 超时时间/秒 最小为5秒
169
+ * @param opt - 可选参数
170
+ * @param opt.count - 重试次数 默认3
171
+ * @param opt.timeout - 超时时间/秒 默认180 最小为5秒
172
+ * @param opt.interval - 重试间隔时间 默认0
171
173
  * @returns 结果 null 为全部失败/超时
172
174
  */
173
- static async repeatPromise(procFn, verifyFn, repeatCount = 3, repeatTime = 180) {
175
+ static async repeatPromise(procFn, verifyFn, opt = {}) {
176
+ opt.count = opt.count ?? 3;
177
+ opt.try_interval = opt.try_interval ?? 180;
178
+ let { count, try_interval } = opt;
174
179
  /**是否含有超时时间 */
175
- const hasRepeatTime = (repeatTime >= 5);
180
+ const hasRepeatTime = (try_interval >= 5);
176
181
  //转换为毫秒
177
182
  if (hasRepeatTime)
178
- repeatTime *= 1000;
183
+ try_interval *= 1000;
179
184
  //验证处理函数
180
185
  if (verifyFn === undefined)
181
186
  verifyFn = () => UtilSymbol_1.Success;
@@ -184,7 +189,9 @@ class UtilFunc {
184
189
  //开始处理
185
190
  try {
186
191
  //根据最大重试次数限制进行循环
187
- for (let i = 0; i < repeatCount;) {
192
+ for (let i = 0; i < count;) {
193
+ if (i > 0 && opt.try_delay)
194
+ await this.sleep(opt.try_delay * 1000);
188
195
  UtilLogger_1.SLogger.info(`开始第 ${i + 1} 次 repeatPromise`);
189
196
  //如果 plist 中当前下标的任务还未创建 则 创建当前任务
190
197
  if (plist.length < i + 1) {
@@ -193,7 +200,7 @@ class UtilFunc {
193
200
  const result = await procFn();
194
201
  const stat = await verifyFn(result);
195
202
  return { result, stat, index };
196
- }, hasRepeatTime ? repeatTime : undefined));
203
+ }, hasRepeatTime ? try_interval : undefined));
197
204
  }
198
205
  //等待任意任务 或当前计时器完成
199
206
  const currObj = await Promise.race([...plist]);
@@ -204,7 +211,7 @@ class UtilFunc {
204
211
  const res = await currObj.result;
205
212
  rslove(UtilFunc.outcome(UtilSymbol_1.Success, res));
206
213
  });
207
- UtilLogger_1.SLogger.warn(`第 ${i + 1} 次 repeatPromise 超时 ${repeatTime} ms 开始重试`);
214
+ UtilLogger_1.SLogger.warn(`第 ${i + 1} 次 repeatPromise 超时 ${try_interval} ms 开始重试`);
208
215
  i++;
209
216
  continue;
210
217
  }
@@ -215,7 +222,7 @@ class UtilFunc {
215
222
  UtilLogger_1.SLogger.info(`第 ${postresult.index + 1} 次 repeatPromise 成功`);
216
223
  //非当前
217
224
  if (postresult.index != i)
218
- UtilLogger_1.SLogger.info(`成功的 promise 非当前 promise 考虑增大重试间隔\n当前index: ${i}\n当前间隔: ${repeatTime}`);
225
+ UtilLogger_1.SLogger.info(`成功的 promise 非当前 promise 考虑增大重试时间\n当前index: ${i}\n当前重试时间: ${try_interval}`);
219
226
  return postresult.result;
220
227
  },
221
228
  [UtilSymbol_1.Terminated]() {
@@ -240,7 +247,7 @@ class UtilFunc {
240
247
  return result;
241
248
  }
242
249
  //全部失败或超时则返回null
243
- UtilLogger_1.SLogger.warn(`${repeatCount} 次 repeatPromise 尝试均失败`);
250
+ UtilLogger_1.SLogger.warn(`${count} 次 repeatPromise 尝试均失败`);
244
251
  return null;
245
252
  }
246
253
  catch (err) {
@@ -681,6 +688,6 @@ exports.UtilFunc = UtilFunc;
681
688
  __decorate([
682
689
  (0, UtilDecorators_1.LogTimeAsync)("repeatPromise ", true),
683
690
  __metadata("design:type", Function),
684
- __metadata("design:paramtypes", [Function, Function, Number, Number]),
691
+ __metadata("design:paramtypes", [Function, Function, Object]),
685
692
  __metadata("design:returntype", Promise)
686
693
  ], UtilFunc, "repeatPromise", null);
@@ -68,10 +68,10 @@ export type FixedLengthTuple<T, N extends number, R extends unknown[] = []> = R[
68
68
  */
69
69
  export type AnyString = (string & {});
70
70
  /**联合类型转为交叉类型
71
- * 将联合类型映射为联合函数
72
- * 再取得可以传递给任意联合函数的类型
71
+ * 利用分布式条件将U展开为联合函数(k:U1)=>void | (k:U2)=>void
72
+ * 再从中提取可以传递给任意联合函数的类型
73
73
  */
74
- export type UnionToIntersection<U> = ((k: U) => void) extends ((k: infer I) => void) ? I : never;
74
+ export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
75
75
  /**创建一个新的类型,这个新的类型包含了基础类型 B 的所有属性,
76
76
  * 以及一个名为 K[N] 类型为 T 的新属性。
77
77
  * 所有 K 中非 K[N] 的其他属性都是可选的并且不能被赋值(因为它们的类型是 never)。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.128",
3
+ "version": "1.0.130",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/UtilCom.ts CHANGED
@@ -2,9 +2,15 @@ import { JObject, ReqVerifyFn } from "@src/UtilInterfaces";
2
2
  import * as https from 'https';
3
3
  import * as http from 'http';
4
4
  import { SLogger } from "@src/UtilLogger";
5
- import { UtilFunc } from "@src/UtilFunctions";
5
+ import { RepeatPromiseOpt, UtilFunc } from "@src/UtilFunctions";
6
6
 
7
7
 
8
+
9
+ type RepeatPostOpt = Partial<{
10
+ /**httppost 超时中断时间限制 */
11
+ post_timelimit? : number;
12
+ }> & RepeatPromiseOpt;
13
+
8
14
  /**网络工具 */
9
15
  export namespace UtilCom{
10
16
 
@@ -15,11 +21,11 @@ export namespace UtilCom{
15
21
  * @param timeLimit - 超时时间/秒 最小为10秒
16
22
  * @returns 结果 null 为未能成功接收
17
23
  */
18
- function sPost(posttype:"http"|"https",json:JObject,options:Object,timeLimit:number=-1):Promise<JObject|null>{
24
+ function sPost(posttype:"http"|"https",json:JObject,options:Object,timeLimit?:number):Promise<JObject|null>{
19
25
  //转换为毫秒
20
- const hasTimeLimit = (timeLimit>=10);
21
- if(hasTimeLimit)
22
- timeLimit*=1000
26
+ const hasTimeLimit = (timeLimit ? timeLimit>=10 : false );
27
+ if(hasTimeLimit && timeLimit!=undefined) timeLimit*=1000;
28
+ const fixlimit = timeLimit as number;
23
29
 
24
30
  const jsonStr = UtilFunc.stringifyJToken(json);
25
31
  const funcName = `s${posttype}Psot`;
@@ -29,7 +35,7 @@ function sPost(posttype:"http"|"https",json:JObject,options:Object,timeLimit:num
29
35
  try{
30
36
  //请求超时
31
37
  if(hasTimeLimit){
32
- res.setTimeout(timeLimit, ()=>{
38
+ res.setTimeout(fixlimit, ()=>{
33
39
  //res.abort();
34
40
  SLogger.warn(`${funcName} 接收反馈超时: ${timeLimit} ms`);
35
41
  resolve(null);
@@ -80,7 +86,7 @@ function sPost(posttype:"http"|"https",json:JObject,options:Object,timeLimit:num
80
86
 
81
87
  //请求超时
82
88
  if(hasTimeLimit){
83
- req.setTimeout(timeLimit, ()=>{
89
+ req.setTimeout(fixlimit, ()=>{
84
90
  SLogger.warn(`${funcName} 发送请求超时: ${timeLimit} ms`);
85
91
  req.destroy();
86
92
  });
@@ -103,7 +109,7 @@ function sPost(posttype:"http"|"https",json:JObject,options:Object,timeLimit:num
103
109
  * @param timeLimit - 超时时间/秒 最小为10秒
104
110
  * @returns 结果 null 为未能成功接收
105
111
  */
106
- export function shttpsPost(json:JObject,options:Object,timeLimit:number=-1):Promise<JObject|null>{
112
+ export function shttpsPost(json:JObject,options:Object,timeLimit?:number):Promise<JObject|null>{
107
113
  return sPost("https",json,options,timeLimit);
108
114
  }
109
115
 
@@ -114,7 +120,7 @@ export function shttpsPost(json:JObject,options:Object,timeLimit:number=-1):Prom
114
120
  * @param timeLimit - 超时时间/秒 最小为10秒
115
121
  * @returns 结果 null 为未能成功接收
116
122
  */
117
- export function shttpPost(json:JObject,options:Object,timeLimit:number=-1):Promise<JObject|null>{
123
+ export function shttpPost(json:JObject,options:Object,timeLimit?:number):Promise<JObject|null>{
118
124
  return sPost("http",json,options,timeLimit);
119
125
  }
120
126
 
@@ -122,51 +128,58 @@ export function shttpPost(json:JObject,options:Object,timeLimit:number=-1):Promi
122
128
 
123
129
  /**通用重复post处理
124
130
  * @async
125
- * @param posttype - post类型
126
- * @param json - 数据对象
127
- * @param options - 参数对象
128
- * @param timeLimit - 超时时间/秒 最小为10秒
129
- * @param repeatCount - 重试次数
130
- * @param repeatTime - 超时时间/秒 最小为10秒
131
- * @param verifyFn - 判断有效性函数
131
+ * @param posttype - post类型
132
+ * @param json - 数据对象
133
+ * @param options - 参数对象
134
+ * @param verifyFn - 判断有效性函数
135
+ * @param opt - 可选参数
136
+ * @param opt.post_timelimit - 超时时间/秒 最小为10秒 默认无限
137
+ * @param opt.count - 重试次数 默认3
138
+ * @param opt.try_interval - 重试超时时间/秒 最小为5秒 默认180
139
+ * @param opt.try_delay - 重试间隔 秒 默认0
132
140
  * @returns 结果 null 为未能成功接收
133
141
  */
134
- async function sRepeatPost(posttype:"http"|"https",json:JObject,options:Object,timeLimit:number=-1,
135
- repeatCount:number=3,repeatTime:number=180,verifyFn?:ReqVerifyFn<JObject|null>):Promise<JObject|null>{
136
- const procFn = ()=>sPost(posttype,json,options,timeLimit);
137
- return UtilFunc.repeatPromise(procFn,verifyFn,repeatCount,repeatTime);
142
+ async function sRepeatPost(posttype:"http"|"https",json:JObject,options:Object,verifyFn?:ReqVerifyFn<JObject|null>,opt:RepeatPostOpt={}):Promise<JObject|null>{
143
+ opt.count = opt.count??3;
144
+ opt.try_interval = opt.try_interval??180;
145
+ opt.try_delay = opt.try_delay??1;
146
+
147
+ const procFn = ()=>sPost(posttype,json,options,opt.post_timelimit);
148
+ return UtilFunc.repeatPromise(procFn,verifyFn,opt);
138
149
  }
139
150
 
140
151
 
141
152
  /**重复一个 https POST请求并接受数据
142
153
  * @async
143
- * @param json - 数据对象
144
- * @param options - 参数对象
145
- * @param timeLimit - 超时时间/秒 最小为10秒
146
- * @param repeatCount - 重试次数
147
- * @param repeatTime - 超时时间/秒 最小为10秒
148
- * @param verifyFn - 判断有效性函数
154
+ * @param json - 数据对象
155
+ * @param options - 参数对象
156
+ * @param verifyFn - 判断有效性函数
157
+ * @param opt - 可选参数
158
+ * @param opt.post_timelimit - 超时时间/秒 最小为10秒 默认无限
159
+ * @param opt.count - 重试次数 默认3
160
+ * @param opt.try_interval - 重试超时时间/秒 最小为5秒 默认180
161
+ * @param opt.try_delay - 重试间隔 秒 默认0
149
162
  * @returns 结果 null 为未能成功接收
150
163
  */
151
- export function shttpsRepeatPost(json:JObject,options:Object,timeLimit:number=-1,
152
- repeatCount:number=3,repeatTime:number=180,verifyFn?:ReqVerifyFn<JObject|null>):Promise<JObject|null>{
153
- return sRepeatPost("https",json,options,timeLimit,repeatCount,repeatTime,verifyFn);
164
+ export function shttpsRepeatPost(json:JObject,options:Object,verifyFn?:ReqVerifyFn<JObject|null>,opt?:RepeatPostOpt):Promise<JObject|null>{
165
+ return sRepeatPost("https",json,options,verifyFn,opt);
154
166
  }
155
167
 
156
168
  /**重复一个 http POST请求并接受数据
157
169
  * Object ()
158
170
  * @async
159
- * @param json - 数据对象
160
- * @param options - 参数对象
161
- * @param timeLimit - 超时时间/秒 最小为10秒
162
- * @param repeatCount - 重试次数
163
- * @param repeatTime - 超时时间/秒 最小为10秒
164
- * @param verifyFn - 判断有效性函数
171
+ * @param json - 数据对象
172
+ * @param options - 参数对象
173
+ * @param verifyFn - 判断有效性函数
174
+ * @param opt - 可选参数
175
+ * @param opt.post_timelimit - 超时时间/秒 最小为10秒 默认无限
176
+ * @param opt.count - 重试次数 默认3
177
+ * @param opt.try_interval - 重试超时时间/秒 最小为5秒 默认180
178
+ * @param opt.try_delay - 重试间隔 秒 默认0
165
179
  * @returns 结果 null 为未能成功接收
166
180
  */
167
- export function shttpRepeatPost(json:JObject,options:Object,timeLimit:number=-1,
168
- repeatCount:number=3,repeatTime:number=180,verifyFn?:ReqVerifyFn<JObject|null>):Promise<JObject|null>{
169
- return sRepeatPost("http",json,options,timeLimit,repeatCount,repeatTime,verifyFn);
181
+ export function shttpRepeatPost(json:JObject,options:Object,verifyFn?:ReqVerifyFn<JObject|null>,opt?:RepeatPostOpt):Promise<JObject|null>{
182
+ return sRepeatPost("http",json,options,verifyFn,opt);
170
183
  }
171
184
 
172
185
  }
@@ -18,6 +18,18 @@ type ExecOpt = Partial<{
18
18
  errlvl:LogLevel,
19
19
  }>
20
20
 
21
+ /**Promise重试选项 */
22
+ export type RepeatPromiseOpt = Partial<{
23
+ /**重试次数 默认3*/
24
+ count? :number;
25
+ /**尝试间隔时间 超过此事件会重新创建新的Promise
26
+ * 同时等待新的与旧的Promise 秒 默认180
27
+ */
28
+ try_interval? :number;
29
+ /**尝试延迟 重新尝试时会先等待此秒数 秒 默认0*/
30
+ try_delay? :number;
31
+ }>
32
+
21
33
  type SuccessOut<T> = Outcome<Success,T>;
22
34
  type TimeoutOut<T> = Outcome<Timeout,Promise<T>>;
23
35
  /**永不完成的Promise单例 */
@@ -178,18 +190,21 @@ static getNeverResolvedPromise<T>():Promise<T>{
178
190
  * @async
179
191
  * @param procFn - 发起函数
180
192
  * @param verifyFn - 验证函数
181
- * @param repeatCount - 重试次数
182
- * @param repeatTime - 超时时间/秒 最小为5秒
193
+ * @param opt - 可选参数
194
+ * @param opt.count - 重试次数 默认3
195
+ * @param opt.timeout - 超时时间/秒 默认180 最小为5秒
196
+ * @param opt.interval - 重试间隔时间 默认0
183
197
  * @returns 结果 null 为全部失败/超时
184
198
  */
185
199
  @LogTimeAsync("repeatPromise ",true)
186
- static async repeatPromise<T>(procFn:()=>Promise<T>,verifyFn?:ReqVerifyFn<T>,
187
- repeatCount:number=3,repeatTime:number=180):Promise<T|null>{
188
-
200
+ static async repeatPromise<T>(procFn:()=>Promise<T>,verifyFn?:ReqVerifyFn<T>,opt:RepeatPromiseOpt = {}):Promise<T|null>{
201
+ opt.count = opt.count??3;
202
+ opt.try_interval = opt.try_interval??180;
203
+ let {count,try_interval} = opt;
189
204
  /**是否含有超时时间 */
190
- const hasRepeatTime = (repeatTime>=5);
205
+ const hasRepeatTime = (try_interval>=5);
191
206
  //转换为毫秒
192
- if(hasRepeatTime) repeatTime*=1000;
207
+ if(hasRepeatTime) try_interval*=1000;
193
208
 
194
209
  //验证处理函数
195
210
  if(verifyFn===undefined)
@@ -204,9 +219,9 @@ static async repeatPromise<T>(procFn:()=>Promise<T>,verifyFn?:ReqVerifyFn<T>,
204
219
  //开始处理
205
220
  try{
206
221
  //根据最大重试次数限制进行循环
207
- for(let i=0;i<repeatCount;){
222
+ for(let i=0;i<count;){
223
+ if(i>0 && opt.try_delay) await this.sleep(opt.try_delay*1000);
208
224
  SLogger.info(`开始第 ${i+1} 次 repeatPromise`);
209
-
210
225
  //如果 plist 中当前下标的任务还未创建 则 创建当前任务
211
226
  if(plist.length<i+1){
212
227
  plist.push(UtilFunc.timelimitPromise<PromiseResult<T>>(async ()=>{
@@ -214,7 +229,7 @@ static async repeatPromise<T>(procFn:()=>Promise<T>,verifyFn?:ReqVerifyFn<T>,
214
229
  const result = await procFn();
215
230
  const stat = await verifyFn!(result);
216
231
  return {result, stat, index}
217
- },hasRepeatTime?repeatTime:undefined));
232
+ },hasRepeatTime?try_interval:undefined));
218
233
  }
219
234
 
220
235
  //等待任意任务 或当前计时器完成
@@ -227,7 +242,7 @@ static async repeatPromise<T>(procFn:()=>Promise<T>,verifyFn?:ReqVerifyFn<T>,
227
242
  const res = await currObj.result;
228
243
  rslove(UtilFunc.outcome(Success,res));
229
244
  })
230
- SLogger.warn(`第 ${i+1} 次 repeatPromise 超时 ${repeatTime} ms 开始重试`);
245
+ SLogger.warn(`第 ${i+1} 次 repeatPromise 超时 ${try_interval} ms 开始重试`);
231
246
  i++;
232
247
  continue;
233
248
  }
@@ -239,7 +254,7 @@ static async repeatPromise<T>(procFn:()=>Promise<T>,verifyFn?:ReqVerifyFn<T>,
239
254
  SLogger.info(`第 ${postresult.index+1} 次 repeatPromise 成功`);
240
255
  //非当前
241
256
  if(postresult.index!=i)
242
- SLogger.info(`成功的 promise 非当前 promise 考虑增大重试间隔\n当前index: ${i}\n当前间隔: ${repeatTime}`);
257
+ SLogger.info(`成功的 promise 非当前 promise 考虑增大重试时间\n当前index: ${i}\n当前重试时间: ${try_interval}`);
243
258
  return postresult.result;
244
259
  },
245
260
  [Terminated](){
@@ -263,7 +278,7 @@ static async repeatPromise<T>(procFn:()=>Promise<T>,verifyFn?:ReqVerifyFn<T>,
263
278
  if(result !== None) return result;
264
279
  }
265
280
  //全部失败或超时则返回null
266
- SLogger.warn(`${repeatCount} 次 repeatPromise 尝试均失败`);
281
+ SLogger.warn(`${count} 次 repeatPromise 尝试均失败`);
267
282
  return null;
268
283
  }catch(err){
269
284
  SLogger.warn(`repeatPromise 发生错误`,err);
@@ -97,12 +97,15 @@ export type FixedLengthTuple<T, N extends number, R extends unknown[] = []> =
97
97
  export type AnyString = (string&{});
98
98
 
99
99
  /**联合类型转为交叉类型
100
- * 将联合类型映射为联合函数
101
- * 再取得可以传递给任意联合函数的类型
100
+ * 利用分布式条件将U展开为联合函数(k:U1)=>void | (k:U2)=>void
101
+ * 再从中提取可以传递给任意联合函数的类型
102
102
  */
103
103
  export type UnionToIntersection<U> =
104
- ((k: U)=>void) extends ((k: infer I)=>void)
105
- ? I : never
104
+ (U extends any
105
+ ? (k: U)=>void
106
+ : never) extends ((k: infer I)=>void)
107
+ ? I
108
+ : never
106
109
 
107
110
  /**创建一个新的类型,这个新的类型包含了基础类型 B 的所有属性,
108
111
  * 以及一个名为 K[N] 类型为 T 的新属性。