@zwa73/utils 1.0.130 → 1.0.131
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 +1 -1
- package/dist/UtilCom.js +3 -3
- package/dist/UtilFunctions.d.ts +2 -2
- package/dist/UtilFunctions.js +9 -9
- package/package.json +1 -1
- package/scripts/postinstall.js +5 -5
- package/src/UtilCom.ts +4 -4
- package/src/UtilFunctions.ts +10 -10
package/dist/UtilCom.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { JObject, ReqVerifyFn } from "./UtilInterfaces";
|
|
|
2
2
|
import { RepeatPromiseOpt } from "./UtilFunctions";
|
|
3
3
|
type RepeatPostOpt = Partial<{
|
|
4
4
|
/**httppost 超时中断时间限制 */
|
|
5
|
-
|
|
5
|
+
postTimeLimit?: number;
|
|
6
6
|
}> & RepeatPromiseOpt;
|
|
7
7
|
/**网络工具 */
|
|
8
8
|
export declare namespace UtilCom {
|
package/dist/UtilCom.js
CHANGED
|
@@ -149,9 +149,9 @@ var UtilCom;
|
|
|
149
149
|
*/
|
|
150
150
|
async function sRepeatPost(posttype, json, options, verifyFn, opt = {}) {
|
|
151
151
|
opt.count = opt.count ?? 3;
|
|
152
|
-
opt.
|
|
153
|
-
opt.
|
|
154
|
-
const procFn = () => sPost(posttype, json, options, opt.
|
|
152
|
+
opt.tryInterval = opt.tryInterval ?? 180;
|
|
153
|
+
opt.tryDelay = opt.tryDelay ?? 1;
|
|
154
|
+
const procFn = () => sPost(posttype, json, options, opt.postTimeLimit);
|
|
155
155
|
return UtilFunctions_1.UtilFunc.repeatPromise(procFn, verifyFn, opt);
|
|
156
156
|
}
|
|
157
157
|
/**重复一个 https POST请求并接受数据
|
package/dist/UtilFunctions.d.ts
CHANGED
|
@@ -15,9 +15,9 @@ export type RepeatPromiseOpt = Partial<{
|
|
|
15
15
|
/**尝试间隔时间 超过此事件会重新创建新的Promise
|
|
16
16
|
* 同时等待新的与旧的Promise 秒 默认180
|
|
17
17
|
*/
|
|
18
|
-
|
|
18
|
+
tryInterval?: number;
|
|
19
19
|
/**尝试延迟 重新尝试时会先等待此秒数 秒 默认0*/
|
|
20
|
-
|
|
20
|
+
tryDelay?: number;
|
|
21
21
|
}>;
|
|
22
22
|
type SuccessOut<T> = Outcome<Success, T>;
|
|
23
23
|
type TimeoutOut<T> = Outcome<Timeout, Promise<T>>;
|
package/dist/UtilFunctions.js
CHANGED
|
@@ -174,13 +174,13 @@ class UtilFunc {
|
|
|
174
174
|
*/
|
|
175
175
|
static async repeatPromise(procFn, verifyFn, opt = {}) {
|
|
176
176
|
opt.count = opt.count ?? 3;
|
|
177
|
-
opt.
|
|
178
|
-
let { count,
|
|
177
|
+
opt.tryInterval = opt.tryInterval ?? 180;
|
|
178
|
+
let { count, tryInterval } = opt;
|
|
179
179
|
/**是否含有超时时间 */
|
|
180
|
-
const hasRepeatTime = (
|
|
180
|
+
const hasRepeatTime = (tryInterval >= 5);
|
|
181
181
|
//转换为毫秒
|
|
182
182
|
if (hasRepeatTime)
|
|
183
|
-
|
|
183
|
+
tryInterval *= 1000;
|
|
184
184
|
//验证处理函数
|
|
185
185
|
if (verifyFn === undefined)
|
|
186
186
|
verifyFn = () => UtilSymbol_1.Success;
|
|
@@ -190,8 +190,8 @@ class UtilFunc {
|
|
|
190
190
|
try {
|
|
191
191
|
//根据最大重试次数限制进行循环
|
|
192
192
|
for (let i = 0; i < count;) {
|
|
193
|
-
if (i > 0 && opt.
|
|
194
|
-
await this.sleep(opt.
|
|
193
|
+
if (i > 0 && opt.tryDelay)
|
|
194
|
+
await this.sleep(opt.tryDelay * 1000);
|
|
195
195
|
UtilLogger_1.SLogger.info(`开始第 ${i + 1} 次 repeatPromise`);
|
|
196
196
|
//如果 plist 中当前下标的任务还未创建 则 创建当前任务
|
|
197
197
|
if (plist.length < i + 1) {
|
|
@@ -200,7 +200,7 @@ class UtilFunc {
|
|
|
200
200
|
const result = await procFn();
|
|
201
201
|
const stat = await verifyFn(result);
|
|
202
202
|
return { result, stat, index };
|
|
203
|
-
}, hasRepeatTime ?
|
|
203
|
+
}, hasRepeatTime ? tryInterval : undefined));
|
|
204
204
|
}
|
|
205
205
|
//等待任意任务 或当前计时器完成
|
|
206
206
|
const currObj = await Promise.race([...plist]);
|
|
@@ -211,7 +211,7 @@ class UtilFunc {
|
|
|
211
211
|
const res = await currObj.result;
|
|
212
212
|
rslove(UtilFunc.outcome(UtilSymbol_1.Success, res));
|
|
213
213
|
});
|
|
214
|
-
UtilLogger_1.SLogger.warn(`第 ${i + 1} 次 repeatPromise 超时 ${
|
|
214
|
+
UtilLogger_1.SLogger.warn(`第 ${i + 1} 次 repeatPromise 超时 ${tryInterval} ms 开始重试`);
|
|
215
215
|
i++;
|
|
216
216
|
continue;
|
|
217
217
|
}
|
|
@@ -222,7 +222,7 @@ class UtilFunc {
|
|
|
222
222
|
UtilLogger_1.SLogger.info(`第 ${postresult.index + 1} 次 repeatPromise 成功`);
|
|
223
223
|
//非当前
|
|
224
224
|
if (postresult.index != i)
|
|
225
|
-
UtilLogger_1.SLogger.info(`成功的 promise 非当前 promise 考虑增大重试时间\n当前index: ${i}\n当前重试时间: ${
|
|
225
|
+
UtilLogger_1.SLogger.info(`成功的 promise 非当前 promise 考虑增大重试时间\n当前index: ${i}\n当前重试时间: ${tryInterval}`);
|
|
226
226
|
return postresult.result;
|
|
227
227
|
},
|
|
228
228
|
[UtilSymbol_1.Terminated]() {
|
package/package.json
CHANGED
package/scripts/postinstall.js
CHANGED
|
@@ -15,6 +15,7 @@ const INFO_TABLE = {
|
|
|
15
15
|
"UtilFT.ensurePathExists 更改参数 dir 移入 可选参数对象 \n" +
|
|
16
16
|
"UtilFT.ensurePathExistsSync 更改参数 dir 移入 可选参数对象 \n",
|
|
17
17
|
"1.0.128":"fileSearch 添加Sync版本, 默认改为异步函数",
|
|
18
|
+
"1.0.131":"repeatPromise 与 repeatPost 改为传入可选参数表模式",
|
|
18
19
|
}
|
|
19
20
|
|
|
20
21
|
// 将版本号转换为可以比较的数字
|
|
@@ -31,6 +32,7 @@ function showUpgradeMessages(prevVersion, currentVersion) {
|
|
|
31
32
|
// 遍历infoTable中的所有版本
|
|
32
33
|
for (const version in INFO_TABLE) {
|
|
33
34
|
const versionNumber = versionToNumber(version);
|
|
35
|
+
console.log(versionNumber,prevVersionNumber,currentVersionNumber)
|
|
34
36
|
// 如果用户的上一个版本低于这个版本,而当前版本高于或等于这个版本
|
|
35
37
|
if (versionNumber > prevVersionNumber && versionNumber <= currentVersionNumber) {
|
|
36
38
|
// 显示这个版本的提示信息
|
|
@@ -47,17 +49,15 @@ async function main(){
|
|
|
47
49
|
const packageTable = await UtilFT.loadJSONFile(PACKAGE_PATH);
|
|
48
50
|
const currentVersion = packageTable.version;
|
|
49
51
|
await UtilFT.ensurePathExists(VERSION_PATH);
|
|
50
|
-
const versionTable =
|
|
51
|
-
await UtilFT.loadJSONFile(VERSION_PATH,{}),
|
|
52
|
-
{utils:{version:currentVersion}});
|
|
52
|
+
const versionTable = await UtilFT.loadJSONFile(VERSION_PATH,{})
|
|
53
53
|
const prevVersion = versionTable.utils.version;
|
|
54
54
|
|
|
55
55
|
console.log(`${currentVersion} 版本已安装`);
|
|
56
56
|
// 使用这个函数来显示升级信息
|
|
57
57
|
const hasOut = showUpgradeMessages(prevVersion, currentVersion);
|
|
58
58
|
|
|
59
|
-
versionTable
|
|
60
|
-
await UtilFT.writeJSONFile(VERSION_PATH,
|
|
59
|
+
const ntable = Object.assign({},versionTable,{utils:{version:currentVersion}});
|
|
60
|
+
await UtilFT.writeJSONFile(VERSION_PATH, ntable);
|
|
61
61
|
|
|
62
62
|
if(!hasOut) return;
|
|
63
63
|
const rl = readline.createInterface({
|
package/src/UtilCom.ts
CHANGED
|
@@ -8,7 +8,7 @@ import { RepeatPromiseOpt, UtilFunc } from "@src/UtilFunctions";
|
|
|
8
8
|
|
|
9
9
|
type RepeatPostOpt = Partial<{
|
|
10
10
|
/**httppost 超时中断时间限制 */
|
|
11
|
-
|
|
11
|
+
postTimeLimit? : number;
|
|
12
12
|
}> & RepeatPromiseOpt;
|
|
13
13
|
|
|
14
14
|
/**网络工具 */
|
|
@@ -141,10 +141,10 @@ export function shttpPost(json:JObject,options:Object,timeLimit?:number):Promise
|
|
|
141
141
|
*/
|
|
142
142
|
async function sRepeatPost(posttype:"http"|"https",json:JObject,options:Object,verifyFn?:ReqVerifyFn<JObject|null>,opt:RepeatPostOpt={}):Promise<JObject|null>{
|
|
143
143
|
opt.count = opt.count??3;
|
|
144
|
-
opt.
|
|
145
|
-
opt.
|
|
144
|
+
opt.tryInterval = opt.tryInterval??180;
|
|
145
|
+
opt.tryDelay = opt.tryDelay??1;
|
|
146
146
|
|
|
147
|
-
const procFn = ()=>sPost(posttype,json,options,opt.
|
|
147
|
+
const procFn = ()=>sPost(posttype,json,options,opt.postTimeLimit);
|
|
148
148
|
return UtilFunc.repeatPromise(procFn,verifyFn,opt);
|
|
149
149
|
}
|
|
150
150
|
|
package/src/UtilFunctions.ts
CHANGED
|
@@ -25,9 +25,9 @@ export type RepeatPromiseOpt = Partial<{
|
|
|
25
25
|
/**尝试间隔时间 超过此事件会重新创建新的Promise
|
|
26
26
|
* 同时等待新的与旧的Promise 秒 默认180
|
|
27
27
|
*/
|
|
28
|
-
|
|
28
|
+
tryInterval? :number;
|
|
29
29
|
/**尝试延迟 重新尝试时会先等待此秒数 秒 默认0*/
|
|
30
|
-
|
|
30
|
+
tryDelay? :number;
|
|
31
31
|
}>
|
|
32
32
|
|
|
33
33
|
type SuccessOut<T> = Outcome<Success,T>;
|
|
@@ -199,12 +199,12 @@ static getNeverResolvedPromise<T>():Promise<T>{
|
|
|
199
199
|
@LogTimeAsync("repeatPromise ",true)
|
|
200
200
|
static async repeatPromise<T>(procFn:()=>Promise<T>,verifyFn?:ReqVerifyFn<T>,opt:RepeatPromiseOpt = {}):Promise<T|null>{
|
|
201
201
|
opt.count = opt.count??3;
|
|
202
|
-
opt.
|
|
203
|
-
let {count,
|
|
202
|
+
opt.tryInterval = opt.tryInterval??180;
|
|
203
|
+
let {count,tryInterval} = opt;
|
|
204
204
|
/**是否含有超时时间 */
|
|
205
|
-
const hasRepeatTime = (
|
|
205
|
+
const hasRepeatTime = (tryInterval>=5);
|
|
206
206
|
//转换为毫秒
|
|
207
|
-
if(hasRepeatTime)
|
|
207
|
+
if(hasRepeatTime) tryInterval*=1000;
|
|
208
208
|
|
|
209
209
|
//验证处理函数
|
|
210
210
|
if(verifyFn===undefined)
|
|
@@ -220,7 +220,7 @@ static async repeatPromise<T>(procFn:()=>Promise<T>,verifyFn?:ReqVerifyFn<T>,opt
|
|
|
220
220
|
try{
|
|
221
221
|
//根据最大重试次数限制进行循环
|
|
222
222
|
for(let i=0;i<count;){
|
|
223
|
-
if(i>0 && opt.
|
|
223
|
+
if(i>0 && opt.tryDelay) await this.sleep(opt.tryDelay*1000);
|
|
224
224
|
SLogger.info(`开始第 ${i+1} 次 repeatPromise`);
|
|
225
225
|
//如果 plist 中当前下标的任务还未创建 则 创建当前任务
|
|
226
226
|
if(plist.length<i+1){
|
|
@@ -229,7 +229,7 @@ static async repeatPromise<T>(procFn:()=>Promise<T>,verifyFn?:ReqVerifyFn<T>,opt
|
|
|
229
229
|
const result = await procFn();
|
|
230
230
|
const stat = await verifyFn!(result);
|
|
231
231
|
return {result, stat, index}
|
|
232
|
-
},hasRepeatTime?
|
|
232
|
+
},hasRepeatTime?tryInterval:undefined));
|
|
233
233
|
}
|
|
234
234
|
|
|
235
235
|
//等待任意任务 或当前计时器完成
|
|
@@ -242,7 +242,7 @@ static async repeatPromise<T>(procFn:()=>Promise<T>,verifyFn?:ReqVerifyFn<T>,opt
|
|
|
242
242
|
const res = await currObj.result;
|
|
243
243
|
rslove(UtilFunc.outcome(Success,res));
|
|
244
244
|
})
|
|
245
|
-
SLogger.warn(`第 ${i+1} 次 repeatPromise 超时 ${
|
|
245
|
+
SLogger.warn(`第 ${i+1} 次 repeatPromise 超时 ${tryInterval} ms 开始重试`);
|
|
246
246
|
i++;
|
|
247
247
|
continue;
|
|
248
248
|
}
|
|
@@ -254,7 +254,7 @@ static async repeatPromise<T>(procFn:()=>Promise<T>,verifyFn?:ReqVerifyFn<T>,opt
|
|
|
254
254
|
SLogger.info(`第 ${postresult.index+1} 次 repeatPromise 成功`);
|
|
255
255
|
//非当前
|
|
256
256
|
if(postresult.index!=i)
|
|
257
|
-
SLogger.info(`成功的 promise 非当前 promise 考虑增大重试时间\n当前index: ${i}\n当前重试时间: ${
|
|
257
|
+
SLogger.info(`成功的 promise 非当前 promise 考虑增大重试时间\n当前index: ${i}\n当前重试时间: ${tryInterval}`);
|
|
258
258
|
return postresult.result;
|
|
259
259
|
},
|
|
260
260
|
[Terminated](){
|