@zwa73/utils 1.0.184 → 1.0.186

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
@@ -110,7 +110,7 @@ export declare namespace UtilCom {
110
110
  headers: http.IncomingHttpHeaders;
111
111
  /**响应状态码 */
112
112
  statusCode?: number;
113
- } | undefined> | undefined>;
113
+ } | undefined>>;
114
114
  /**重复一个 http POST请求并接受数据
115
115
  * @async
116
116
  * @param comReqOpt - 网络请求选项
@@ -125,7 +125,7 @@ export declare namespace UtilCom {
125
125
  headers: http.IncomingHttpHeaders;
126
126
  /**响应状态码 */
127
127
  statusCode?: number;
128
- } | undefined> | undefined>;
128
+ } | undefined>>;
129
129
  /**重复一个 https GET 请求并接受数据
130
130
  * @async
131
131
  * @param comReqOpt - 网络请求选项
@@ -140,7 +140,7 @@ export declare namespace UtilCom {
140
140
  headers: http.IncomingHttpHeaders;
141
141
  /**响应状态码 */
142
142
  statusCode?: number;
143
- } | undefined> | undefined>;
143
+ } | undefined>>;
144
144
  /**重复一个 http GET 请求并接受数据
145
145
  * @async
146
146
  * @param comReqOpt - 网络请求选项
@@ -155,5 +155,5 @@ export declare namespace UtilCom {
155
155
  headers: http.IncomingHttpHeaders;
156
156
  /**响应状态码 */
157
157
  statusCode?: number;
158
- } | undefined> | undefined>;
158
+ } | undefined>>;
159
159
  }
package/dist/UtilCom.js CHANGED
@@ -102,12 +102,12 @@ var UtilCom;
102
102
  if (result == undefined)
103
103
  return undefined;
104
104
  const { data, ...rest } = result;
105
- if (data == "") {
105
+ if (data.trim() == "") {
106
106
  UtilLogger_1.SLogger.warn(`${jsonReq.name} 接收反馈错误: 原始字符串为空`);
107
107
  return undefined;
108
108
  }
109
109
  try {
110
- const obj = JSON.parse(data);
110
+ const obj = JSON.parse(data.trim());
111
111
  UtilLogger_1.SLogger.http(`${jsonReq.name} 接受信息:`, UtilFunctions_1.UtilFunc.stringifyJToken(obj, { compress: true, space: 2 }));
112
112
  return { ...rest, data: obj };
113
113
  }
@@ -34,7 +34,7 @@ type SuccessOut<T> = Outcome<Success, T>;
34
34
  type TimeoutOut<T> = Outcome<Timeout, Promise<T>>;
35
35
  /**完成的重试请求 */
36
36
  export type RepeatPromiseResult<T> = {
37
- completed: T;
37
+ completed: T | undefined;
38
38
  /**还未完成的其他Promise 若是验证失败则会返回undefined */
39
39
  pending: Promise<T | undefined>[];
40
40
  };
@@ -111,9 +111,9 @@ export declare class UtilFunc {
111
111
  * @param procFn - 发起函数
112
112
  * @param verifyFn - 验证函数
113
113
  * @param opt - 重试参数 默认 延迟:0ms 间隔:180_000ms 重试:3次
114
- * @returns 结果 undefined 为全部失败/超时
114
+ * @returns 重复结果
115
115
  */
116
- static repeatPromise<T>(procFn: () => Promise<T>, verifyFn?: ReqVerifyFn<T>, opt?: RepeatPromiseOpt): Promise<RepeatPromiseResult<T> | undefined>;
116
+ static repeatPromise<T>(procFn: () => Promise<T>, verifyFn?: ReqVerifyFn<T>, opt?: RepeatPromiseOpt): Promise<RepeatPromiseResult<T>>;
117
117
  /**创建一个限时的Promise
118
118
  * @param func - 处理函数
119
119
  * @param timeLimit - 毫秒限时
@@ -170,7 +170,7 @@ class UtilFunc {
170
170
  * @param procFn - 发起函数
171
171
  * @param verifyFn - 验证函数
172
172
  * @param opt - 重试参数 默认 延迟:0ms 间隔:180_000ms 重试:3次
173
- * @returns 结果 undefined 为全部失败/超时
173
+ * @returns 重复结果
174
174
  */
175
175
  static async repeatPromise(procFn, verifyFn, opt = {}) {
176
176
  opt.count = opt.count ?? 3;
@@ -262,13 +262,31 @@ class UtilFunc {
262
262
  }),
263
263
  };
264
264
  }
265
- //全部失败或超时则返回 undefined
265
+ //全部失败或超时则返回 全pending
266
266
  UtilLogger_1.SLogger.warn(`${count} 次 repeatPromise 尝试均失败`);
267
- return undefined;
267
+ return {
268
+ completed: undefined,
269
+ pending: plist
270
+ .filter(p => p != UtilFunc.getNeverResolvedPromise())
271
+ .map(async (p) => {
272
+ const curObj = await p;
273
+ if (curObj.status == UtilSymbol_1.Success) {
274
+ const outres = curObj.result;
275
+ if (outres.stat == UtilSymbol_1.Success)
276
+ return outres.result;
277
+ }
278
+ if (curObj.status == UtilSymbol_1.Timeout) {
279
+ const outres = await curObj.result;
280
+ if (outres.stat == UtilSymbol_1.Success)
281
+ return outres.result;
282
+ }
283
+ return undefined;
284
+ }),
285
+ };
268
286
  }
269
287
  catch (err) {
270
- UtilLogger_1.SLogger.warn(`repeatPromise 发生错误`, err);
271
- return undefined;
288
+ UtilLogger_1.SLogger.warn(`repeatPromise 发生意外错误`, err);
289
+ return { completed: undefined, pending: [] };
272
290
  }
273
291
  }
274
292
  /**创建一个限时的Promise
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.184",
3
+ "version": "1.0.186",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/UtilCom.ts CHANGED
@@ -162,13 +162,13 @@ async function jsonReq<T extends ComReqOpt>(
162
162
  if(result==undefined) return undefined;
163
163
  const {data,...rest} = result;
164
164
 
165
- if(data==""){
165
+ if(data.trim()==""){
166
166
  SLogger.warn(`${jsonReq.name} 接收反馈错误: 原始字符串为空`);
167
167
  return undefined;
168
168
  }
169
169
 
170
170
  try{
171
- const obj = JSON.parse(data) as JObject;
171
+ const obj = JSON.parse(data.trim()) as JObject;
172
172
  SLogger.http(`${jsonReq.name} 接受信息:`,UtilFunc.stringifyJToken(obj,{compress:true,space:2}));
173
173
  return{...rest,data:obj};
174
174
  }
@@ -58,7 +58,7 @@ type PromiseResult<T> = {
58
58
 
59
59
  /**完成的重试请求 */
60
60
  export type RepeatPromiseResult<T> = {
61
- completed:T;
61
+ completed:T|undefined;
62
62
  /**还未完成的其他Promise 若是验证失败则会返回undefined */
63
63
  pending:Promise<T|undefined>[];
64
64
  }
@@ -224,11 +224,11 @@ static getNeverResolvedPromise<T>():Promise<T>{
224
224
  * @param procFn - 发起函数
225
225
  * @param verifyFn - 验证函数
226
226
  * @param opt - 重试参数 默认 延迟:0ms 间隔:180_000ms 重试:3次
227
- * @returns 结果 undefined 为全部失败/超时
227
+ * @returns 重复结果
228
228
  */
229
229
  @LogTimeAsync("repeatPromise ",true)
230
230
  static async repeatPromise<T>(procFn:()=>Promise<T>,verifyFn?:ReqVerifyFn<T>,opt:RepeatPromiseOpt = {}):
231
- Promise<RepeatPromiseResult<T>|undefined>{
231
+ Promise<RepeatPromiseResult<T>>{
232
232
  opt.count = opt.count??3;
233
233
  opt.tryInterval = opt.tryInterval??180_000;
234
234
  const {count,tryInterval} = opt;
@@ -323,12 +323,28 @@ Promise<RepeatPromiseResult<T>|undefined>{
323
323
  }),
324
324
  };
325
325
  }
326
- //全部失败或超时则返回 undefined
326
+ //全部失败或超时则返回 全pending
327
327
  SLogger.warn(`${count} 次 repeatPromise 尝试均失败`);
328
- return undefined;
328
+ return {
329
+ completed:undefined,
330
+ pending:plist
331
+ .filter(p=>p!=UtilFunc.getNeverResolvedPromise())
332
+ .map(async p=>{
333
+ const curObj = await p;
334
+ if(curObj.status==Success){
335
+ const outres = curObj.result;
336
+ if(outres.stat==Success) return outres.result;
337
+ }
338
+ if(curObj.status==Timeout){
339
+ const outres = await curObj.result;
340
+ if(outres.stat==Success) return outres.result;
341
+ }
342
+ return undefined;
343
+ }),
344
+ };
329
345
  }catch(err){
330
- SLogger.warn(`repeatPromise 发生错误`,err);
331
- return undefined;
346
+ SLogger.warn(`repeatPromise 发生意外错误`,err);
347
+ return {completed:undefined,pending:[]};
332
348
  }
333
349
  }
334
350