@zwa73/utils 1.0.188 → 1.0.190
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 +21 -14
- package/package.json +1 -1
- package/src/UtilFunctions.ts +20 -13
package/dist/UtilFunctions.js
CHANGED
|
@@ -188,7 +188,7 @@ class UtilFunc {
|
|
|
188
188
|
//根据最大重试次数限制进行循环
|
|
189
189
|
for (let i = 0; i < count;) {
|
|
190
190
|
if (i > 0 && opt.tryDelay)
|
|
191
|
-
await
|
|
191
|
+
await UtilFunc.sleep(opt.tryDelay);
|
|
192
192
|
UtilLogger_1.SLogger.info(`开始第 ${i + 1} 次 repeatPromise`);
|
|
193
193
|
//如果 plist 中当前下标的任务还未创建 则 创建当前任务
|
|
194
194
|
if (plist.length < i + 1) {
|
|
@@ -396,12 +396,14 @@ class UtilFunc {
|
|
|
396
396
|
*/
|
|
397
397
|
static queueProc(flag, task) {
|
|
398
398
|
// 如果当前标签的队列不存在,则创建一个新的队列
|
|
399
|
-
if (!
|
|
400
|
-
|
|
399
|
+
if (!UtilFunc.pendingMap[flag])
|
|
400
|
+
UtilFunc.pendingMap[flag] = [];
|
|
401
401
|
// 创建一个新的Promise,并保存resolve函数以便后续调用
|
|
402
402
|
let resolveFunc;
|
|
403
|
-
|
|
403
|
+
let rejectFunc;
|
|
404
|
+
const promise = new Promise((resolve, reject) => {
|
|
404
405
|
resolveFunc = resolve;
|
|
406
|
+
rejectFunc = reject;
|
|
405
407
|
});
|
|
406
408
|
// 定义处理任务的函数
|
|
407
409
|
const processTask = async () => {
|
|
@@ -414,25 +416,26 @@ class UtilFunc {
|
|
|
414
416
|
}
|
|
415
417
|
catch (error) {
|
|
416
418
|
// 如果任务执行出错,记录错误日志
|
|
417
|
-
UtilLogger_1.SLogger.warn(`queueProc
|
|
419
|
+
UtilLogger_1.SLogger.warn(`queueProc 错误 flag: ${String(flag)} 已抛出`);
|
|
420
|
+
rejectFunc(error);
|
|
418
421
|
}
|
|
419
422
|
finally {
|
|
420
423
|
// 无论任务是否成功,都从队列中移除当前任务
|
|
421
|
-
|
|
424
|
+
UtilFunc.pendingMap[flag].shift();
|
|
422
425
|
// 如果队列中还有任务,执行下一个任务
|
|
423
|
-
if (
|
|
424
|
-
|
|
426
|
+
if (UtilFunc.pendingMap[flag].length > 0) {
|
|
427
|
+
UtilFunc.pendingMap[flag][0]();
|
|
425
428
|
}
|
|
426
429
|
else {
|
|
427
430
|
// 如果队列中没有任务,删除队列
|
|
428
|
-
delete
|
|
431
|
+
delete UtilFunc.pendingMap[flag];
|
|
429
432
|
}
|
|
430
433
|
}
|
|
431
434
|
};
|
|
432
435
|
// 将处理任务的函数添加到队列中
|
|
433
|
-
|
|
436
|
+
UtilFunc.pendingMap[flag].push(processTask);
|
|
434
437
|
// 如果队列中只有当前任务,立即执行
|
|
435
|
-
if (
|
|
438
|
+
if (UtilFunc.pendingMap[flag].length === 1)
|
|
436
439
|
processTask();
|
|
437
440
|
// 返回Promise,以便调用者可以等待任务完成
|
|
438
441
|
return promise;
|
|
@@ -441,7 +444,7 @@ class UtilFunc {
|
|
|
441
444
|
* @param flag - 队列标签
|
|
442
445
|
*/
|
|
443
446
|
static queueLength(flag) {
|
|
444
|
-
const pd =
|
|
447
|
+
const pd = UtilFunc.pendingMap[flag];
|
|
445
448
|
return pd != null ? pd.length : 0;
|
|
446
449
|
}
|
|
447
450
|
/**创建一个Outcome */
|
|
@@ -728,7 +731,10 @@ class UtilFunc {
|
|
|
728
731
|
static parseSrtTime(time) {
|
|
729
732
|
const [hours, minutes, seconds] = time.split(':');
|
|
730
733
|
const [secs, milliseconds] = seconds.split(',');
|
|
731
|
-
return parseInt(hours) * 3600000 +
|
|
734
|
+
return parseInt(hours) * 3600000 +
|
|
735
|
+
parseInt(minutes) * 60000 +
|
|
736
|
+
parseInt(secs) * 1000 +
|
|
737
|
+
parseInt(milliseconds);
|
|
732
738
|
}
|
|
733
739
|
/**将毫秒转换为hh:mm:ss,ms格式 */
|
|
734
740
|
static formatSrtTime(milliseconds) {
|
|
@@ -748,7 +754,8 @@ class UtilFunc {
|
|
|
748
754
|
if (!id)
|
|
749
755
|
continue;
|
|
750
756
|
const timeRange = srtLines[index++].trim();
|
|
751
|
-
const [start, end] = timeRange.split(' --> ')
|
|
757
|
+
const [start, end] = timeRange.split(' --> ')
|
|
758
|
+
.map(time => UtilFunc.parseSrtTime(time));
|
|
752
759
|
let text = '';
|
|
753
760
|
while (index < srtLines.length && srtLines[index].trim())
|
|
754
761
|
text += `${srtLines[index++]}\n`;
|
package/package.json
CHANGED
package/src/UtilFunctions.ts
CHANGED
|
@@ -249,7 +249,7 @@ Promise<RepeatPromiseResult<T>>{
|
|
|
249
249
|
try{
|
|
250
250
|
//根据最大重试次数限制进行循环
|
|
251
251
|
for(let i=0;i<count;){
|
|
252
|
-
if(i>0 && opt.tryDelay) await
|
|
252
|
+
if(i>0 && opt.tryDelay) await UtilFunc.sleep(opt.tryDelay);
|
|
253
253
|
SLogger.info(`开始第 ${i+1} 次 repeatPromise`);
|
|
254
254
|
//如果 plist 中当前下标的任务还未创建 则 创建当前任务
|
|
255
255
|
if(plist.length<i+1){
|
|
@@ -468,12 +468,14 @@ static pendingMap:Record<Keyable,AnyFunc[]> = {};
|
|
|
468
468
|
*/
|
|
469
469
|
static queueProc<T>(flag: Keyable, task: () => Promise<T>): Promise<T> {
|
|
470
470
|
// 如果当前标签的队列不存在,则创建一个新的队列
|
|
471
|
-
if (!
|
|
471
|
+
if (!UtilFunc.pendingMap[flag]) UtilFunc.pendingMap[flag] = [];
|
|
472
472
|
|
|
473
473
|
// 创建一个新的Promise,并保存resolve函数以便后续调用
|
|
474
474
|
let resolveFunc: (value: T | PromiseLike<T>) => void;
|
|
475
|
-
|
|
475
|
+
let rejectFunc: (value: any | PromiseLike<any>) => void;
|
|
476
|
+
const promise = new Promise<T>((resolve,reject) => {
|
|
476
477
|
resolveFunc = resolve;
|
|
478
|
+
rejectFunc = reject;
|
|
477
479
|
});
|
|
478
480
|
|
|
479
481
|
// 定义处理任务的函数
|
|
@@ -486,24 +488,25 @@ static queueProc<T>(flag: Keyable, task: () => Promise<T>): Promise<T> {
|
|
|
486
488
|
resolveFunc(result);
|
|
487
489
|
} catch (error) {
|
|
488
490
|
// 如果任务执行出错,记录错误日志
|
|
489
|
-
SLogger.warn(`queueProc
|
|
491
|
+
SLogger.warn(`queueProc 错误 flag: ${String(flag)} 已抛出`);
|
|
492
|
+
rejectFunc(error);
|
|
490
493
|
} finally {
|
|
491
494
|
// 无论任务是否成功,都从队列中移除当前任务
|
|
492
|
-
|
|
495
|
+
UtilFunc.pendingMap[flag].shift();
|
|
493
496
|
// 如果队列中还有任务,执行下一个任务
|
|
494
|
-
if (
|
|
495
|
-
|
|
497
|
+
if (UtilFunc.pendingMap[flag].length > 0) {
|
|
498
|
+
UtilFunc.pendingMap[flag][0]();
|
|
496
499
|
} else {
|
|
497
500
|
// 如果队列中没有任务,删除队列
|
|
498
|
-
delete
|
|
501
|
+
delete UtilFunc.pendingMap[flag];
|
|
499
502
|
}
|
|
500
503
|
}
|
|
501
504
|
};
|
|
502
505
|
|
|
503
506
|
// 将处理任务的函数添加到队列中
|
|
504
|
-
|
|
507
|
+
UtilFunc.pendingMap[flag].push(processTask);
|
|
505
508
|
// 如果队列中只有当前任务,立即执行
|
|
506
|
-
if (
|
|
509
|
+
if (UtilFunc.pendingMap[flag].length === 1) processTask();
|
|
507
510
|
|
|
508
511
|
// 返回Promise,以便调用者可以等待任务完成
|
|
509
512
|
return promise;
|
|
@@ -512,7 +515,7 @@ static queueProc<T>(flag: Keyable, task: () => Promise<T>): Promise<T> {
|
|
|
512
515
|
* @param flag - 队列标签
|
|
513
516
|
*/
|
|
514
517
|
static queueLength(flag:Keyable){
|
|
515
|
-
const pd =
|
|
518
|
+
const pd = UtilFunc.pendingMap[flag]
|
|
516
519
|
return pd!=null ? pd.length : 0;
|
|
517
520
|
}
|
|
518
521
|
|
|
@@ -861,7 +864,10 @@ static parseSrtTime(time: string): number {
|
|
|
861
864
|
const [hours, minutes, seconds] = time.split(':');
|
|
862
865
|
const [secs, milliseconds] = seconds.split(',');
|
|
863
866
|
|
|
864
|
-
return parseInt(hours) * 3600000 +
|
|
867
|
+
return parseInt(hours) * 3600000 +
|
|
868
|
+
parseInt(minutes) * 60000 +
|
|
869
|
+
parseInt(secs) * 1000 +
|
|
870
|
+
parseInt(milliseconds);
|
|
865
871
|
}
|
|
866
872
|
/**将毫秒转换为hh:mm:ss,ms格式 */
|
|
867
873
|
static formatSrtTime(milliseconds: number): string {
|
|
@@ -883,7 +889,8 @@ static parseSrt(srtText: string): SrtSegment[] {
|
|
|
883
889
|
if (!id) continue;
|
|
884
890
|
|
|
885
891
|
const timeRange = srtLines[index++].trim();
|
|
886
|
-
const [start, end] = timeRange.split(' --> ')
|
|
892
|
+
const [start, end] = timeRange.split(' --> ')
|
|
893
|
+
.map(time => UtilFunc.parseSrtTime(time));
|
|
887
894
|
|
|
888
895
|
let text = '';
|
|
889
896
|
while (index < srtLines.length && srtLines[index].trim())
|