@zwa73/utils 1.0.78 → 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/UtilFunctions.js +8 -7
- package/dist/test/repeatTest.d.ts +1 -0
- package/dist/test/repeatTest.js +29 -0
- package/package.json +1 -1
- package/src/UtilFunctions.ts +8 -7
- package/src/test/repeatTest.ts +29 -0
- package/test/test.bat +2 -0
- package/test/test.js +37 -0
package/dist/UtilFunctions.js
CHANGED
|
@@ -142,10 +142,11 @@ class UtilFunc {
|
|
|
142
142
|
//如果 plist 中当前下标的任务还未创建 则 创建当前任务
|
|
143
143
|
if (plist.length < i + 1) {
|
|
144
144
|
plist.push(UtilFunc.timelimitPromise(async () => {
|
|
145
|
+
const index = i;
|
|
145
146
|
const result = await procFn();
|
|
146
147
|
const stat = await verifyFn(result);
|
|
147
|
-
return { result, stat, index
|
|
148
|
-
}));
|
|
148
|
+
return { result, stat, index };
|
|
149
|
+
}, hasRepeatTime ? repeatTime : undefined));
|
|
149
150
|
}
|
|
150
151
|
//等待任意任务 或当前计时器完成
|
|
151
152
|
const currObj = await Promise.race([...plist]);
|
|
@@ -208,18 +209,18 @@ class UtilFunc {
|
|
|
208
209
|
static timelimitPromise(func, timeLimit) {
|
|
209
210
|
return new Promise((reslove) => {
|
|
210
211
|
let clearTimer = null;
|
|
211
|
-
|
|
212
|
+
const procer = (async () => await func())();
|
|
212
213
|
const procerP = new Promise(async (resolve) => {
|
|
213
|
-
const res = await
|
|
214
|
+
const res = await procer;
|
|
215
|
+
resolve((0, QuickFunction_1.outcome)(UtilSymbol_1.Completed, res));
|
|
214
216
|
if (clearTimer)
|
|
215
217
|
clearTimer();
|
|
216
|
-
resolve((0, QuickFunction_1.outcome)(UtilSymbol_1.Completed, res));
|
|
217
218
|
});
|
|
218
219
|
const timerP = timeLimit
|
|
219
220
|
? new Promise((resolve) => {
|
|
220
|
-
const timer = setTimeout(() => resolve((0, QuickFunction_1.outcome)(UtilSymbol_1.Timeout,
|
|
221
|
+
const timer = setTimeout(() => resolve((0, QuickFunction_1.outcome)(UtilSymbol_1.Timeout, procer)), timeLimit); //无限制则无限时间
|
|
221
222
|
clearTimer = () => {
|
|
222
|
-
resolve((0, QuickFunction_1.outcome)(UtilSymbol_1.Timeout,
|
|
223
|
+
resolve((0, QuickFunction_1.outcome)(UtilSymbol_1.Timeout, procer));
|
|
223
224
|
clearInterval(timer);
|
|
224
225
|
};
|
|
225
226
|
})
|
|
@@ -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
package/src/UtilFunctions.ts
CHANGED
|
@@ -177,10 +177,11 @@ static async repeatPromise<T>(procFn:()=>Promise<T>,verifyFn?:PromiseVerifyFn<T>
|
|
|
177
177
|
//如果 plist 中当前下标的任务还未创建 则 创建当前任务
|
|
178
178
|
if(plist.length<i+1){
|
|
179
179
|
plist.push(UtilFunc.timelimitPromise<ProcessingPromise<T>>(async ()=>{
|
|
180
|
+
const index = i;
|
|
180
181
|
const result = await procFn();
|
|
181
182
|
const stat = await verifyFn!(result);
|
|
182
|
-
return {result, stat, index
|
|
183
|
-
}));
|
|
183
|
+
return {result, stat, index}
|
|
184
|
+
},hasRepeatTime?repeatTime:undefined));
|
|
184
185
|
}
|
|
185
186
|
|
|
186
187
|
//等待任意任务 或当前计时器完成
|
|
@@ -247,22 +248,22 @@ static timelimitPromise<T>
|
|
|
247
248
|
(func:()=>Promise<T>|T,timeLimit?:number):Promise<CompleteCome<T>|TimeoutCome<T>>{
|
|
248
249
|
return new Promise<CompleteCome<T>|TimeoutCome<T>>((reslove)=>{
|
|
249
250
|
let clearTimer:(()=>void)| null = null;
|
|
250
|
-
|
|
251
|
+
const procer = (async ()=>await func())();
|
|
251
252
|
|
|
252
253
|
const procerP = new Promise<CompleteCome<T>>(async (resolve)=>{
|
|
253
|
-
const res = await
|
|
254
|
+
const res = await procer;
|
|
255
|
+
resolve(outcome(Completed,res));
|
|
254
256
|
if(clearTimer) clearTimer();
|
|
255
|
-
resolve(outcome(Completed,res))
|
|
256
257
|
});
|
|
257
258
|
|
|
258
259
|
const timerP = timeLimit
|
|
259
260
|
? new Promise<TimeoutCome<T>>((resolve)=>{
|
|
260
261
|
const timer = setTimeout(()=>
|
|
261
|
-
resolve(outcome(Timeout,
|
|
262
|
+
resolve(outcome(Timeout,procer))
|
|
262
263
|
,timeLimit);//无限制则无限时间
|
|
263
264
|
|
|
264
265
|
clearTimer = ()=>{
|
|
265
|
-
resolve(outcome(Timeout,
|
|
266
|
+
resolve(outcome(Timeout,procer))
|
|
266
267
|
clearInterval(timer)
|
|
267
268
|
}
|
|
268
269
|
})
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Completed, Failed, PromiseStat, PromiseVerifyFn, UtilFunc } from "..";
|
|
2
|
+
|
|
3
|
+
// 模拟的异步函数,有一定几率失败
|
|
4
|
+
async function mockAsyncFunction(): Promise<string> {
|
|
5
|
+
return new Promise((resolve, reject) => {
|
|
6
|
+
setTimeout(() => {
|
|
7
|
+
Math.random() > 0.5 ? resolve("Success") : reject("Failure");
|
|
8
|
+
}, Math.random()%2000);
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// 验证函数,只有当结果为 "Success" 时才认为成功
|
|
13
|
+
function mockVerifyFunction(result: string): PromiseStat {
|
|
14
|
+
return result === "Success" ? Completed : Failed;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// 测试 repeatPromise 函数
|
|
18
|
+
UtilFunc.repeatPromise(mockAsyncFunction, mockVerifyFunction, 3, 10)
|
|
19
|
+
.then(result => {
|
|
20
|
+
console.log("then")
|
|
21
|
+
if (result === null) {
|
|
22
|
+
console.log("全部超时");
|
|
23
|
+
} else {
|
|
24
|
+
console.log("成功:", result);
|
|
25
|
+
}
|
|
26
|
+
})
|
|
27
|
+
.catch(err => {
|
|
28
|
+
console.error("出现错误", err);
|
|
29
|
+
});
|
package/test/test.bat
ADDED
package/test/test.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
const { Completed, Failed, PromiseStat, PromiseVerifyFn, UtilFunc } = require("../dist");
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
let timeList = [21000,5000,1000000]
|
|
5
|
+
let resList = ["Success","Failure","Failure"]
|
|
6
|
+
let index = 0;
|
|
7
|
+
// 模拟的异步函数,有一定几率失败
|
|
8
|
+
async function mockAsyncFunction() {
|
|
9
|
+
return new Promise((resolve, reject) => {
|
|
10
|
+
|
|
11
|
+
let delay =timeList[index];
|
|
12
|
+
let result = resList[index];
|
|
13
|
+
console.log("延迟: "+delay,"结果: "+result)
|
|
14
|
+
setTimeout(() => {
|
|
15
|
+
resolve(result);
|
|
16
|
+
}, delay);
|
|
17
|
+
index++;
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// 验证函数,只有当结果为 "Success" 时才认为成功
|
|
22
|
+
function mockVerifyFunction(result) {
|
|
23
|
+
return result === "Success" ? Completed : Failed;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// 测试 repeatPromise 函数
|
|
27
|
+
UtilFunc.repeatPromise(mockAsyncFunction, mockVerifyFunction, 3, 10)
|
|
28
|
+
.then(result => {
|
|
29
|
+
if (result === null) {
|
|
30
|
+
console.log("全部超时");
|
|
31
|
+
} else {
|
|
32
|
+
console.log("成功:", result);
|
|
33
|
+
}
|
|
34
|
+
})
|
|
35
|
+
.catch(err => {
|
|
36
|
+
console.error("出现错误", err);
|
|
37
|
+
});
|