utiller 1.0.379 → 1.0.380

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.
@@ -6,7 +6,6 @@ Object.defineProperty(exports, "__esModule", {
6
6
  });
7
7
  exports["default"] = void 0;
8
8
  var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
9
- var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
10
9
  var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
11
10
  var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
12
11
  var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
@@ -28,7 +27,7 @@ function _assertClassBrand(e, t, n) { if ("function" == typeof e ? e === t : e.h
28
27
  Pooller 有以下特點:
29
28
  1.task可以設定timeout
30
29
  2.queue滿了的可以設定task interval
31
- 3.如果是runByEachTask length, queue裡面沒有task時,可以設定sleeptime, 以及Sleepcounts
30
+ 3.如果是runByEachTask length, queue裡面沒有task時,可以設定sleeptime, 以及Sleepcounts
32
31
  4.task 可以cancelled by hash
33
32
  5.runByParams,runByTimes,runInInfinite,runByEachTask
34
33
  6.可以設定taskFailHandler, 這樣遇到錯誤就不會停掉poollers
@@ -84,27 +83,31 @@ var InfinitePool = /*#__PURE__*/function () {
84
83
  return _this.nameOfCurrentPool;
85
84
  });
86
85
  /** return true if task completed, after 15 secs, force leave
87
- * TODO:應該要設計成當terminate後, 監聽executingTaskInQueue為零時,回傳結束 */
86
+ * TODO:應該要設計成當terminate後, 監聽executingTaskInQueue為零時,回傳結束 */
88
87
  (0, _defineProperty2["default"])(this, "stopInBackground", /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
88
+ var attempts, maxAttempts;
89
89
  return _regenerator["default"].wrap(function _callee$(_context) {
90
90
  while (1) switch (_context.prev = _context.next) {
91
91
  case 0:
92
92
  _this.terminate();
93
- case 1:
94
- if (!(_lodash["default"].size(_this.queueOfExecutingTask) > 0)) {
95
- _context.next = 8;
93
+ attempts = 0;
94
+ maxAttempts = 30; // 30 * 500ms = 15 seconds wait
95
+ case 3:
96
+ if (!(_lodash["default"].size(_this.queueOfExecutingTask) > 0 && attempts < maxAttempts)) {
97
+ _context.next = 11;
96
98
  break;
97
99
  }
98
- _context.next = 4;
100
+ _context.next = 6;
99
101
  return _index2.utiller.syncDelay(500);
100
- case 4:
102
+ case 6:
101
103
  _this.printLogMessage("784512, \u54AC\u5728 stopInBackground \u51FA\u4E0D\u4F86,".concat(_this.getLogMessageOfExecutingTaskQueueCount()));
102
104
  _this.showState();
103
- _context.next = 1;
105
+ attempts++;
106
+ _context.next = 3;
104
107
  break;
105
- case 8:
108
+ case 11:
106
109
  return _context.abrupt("return", true);
107
- case 9:
110
+ case 12:
108
111
  case "end":
109
112
  return _context.stop();
110
113
  }
@@ -461,26 +464,34 @@ var InfinitePool = /*#__PURE__*/function () {
461
464
  case 0:
462
465
  _context6.prev = 0;
463
466
  _context6.next = 3;
464
- return asyncfunc.apply(void 0, params);
467
+ return asyncfunc.apply(_this, params);
465
468
  case 3:
466
- _context6.next = 8;
469
+ _context6.next = 12;
467
470
  break;
468
471
  case 5:
469
472
  _context6.prev = 5;
470
473
  _context6.t0 = _context6["catch"](0);
474
+ if (!(_context6.t0 instanceof _exceptioner["default"])) {
475
+ _context6.next = 11;
476
+ break;
477
+ }
478
+ _this.printLogMessage("7812123, runInBackGround() \u57F7\u884C\u932F\u8AA4: ".concat(_context6.t0.message), true, _context6.t0);
479
+ _context6.next = 12;
480
+ break;
481
+ case 11:
471
482
  throw new _exceptioner["default"](4009, {
472
483
  message: "".concat(_this.getPoollerLogFormat(""))
473
484
  }, _context6.t0);
474
- case 8:
475
- _context6.prev = 8;
485
+ case 12:
486
+ _context6.prev = 12;
476
487
  _this.terminate();
477
488
  _this.printLogMessage("7812123, runInBackGround() \u8D70\u5230finally");
478
- return _context6.finish(8);
479
- case 12:
489
+ return _context6.finish(12);
490
+ case 16:
480
491
  case "end":
481
492
  return _context6.stop();
482
493
  }
483
- }, _callee6, null, [[0, 5, 8, 12]]);
494
+ }, _callee6, null, [[0, 5, 12, 16]]);
484
495
  })), 1);
485
496
  });
486
497
  (0, _defineProperty2["default"])(this, "getPoollerLogFormat", function (msg) {
@@ -500,9 +511,9 @@ var InfinitePool = /*#__PURE__*/function () {
500
511
  case _configerer.configerer.POOLLER_STATE.RUN_BY_PARAMS:
501
512
  return _this.isRunning() && !_this.isExecutingQueueFull() && _lodash["default"].size(_this.queueOfWaitingParam) > 0;
502
513
  case _configerer.configerer.POOLLER_STATE.RUN_BY_TIMES:
503
- return _this.isRunning() && !_this.isExecutingQueueFull() && _this.countsOfRunByTimes > 0;
504
514
  case _configerer.configerer.POOLLER_STATE.RUN_INFINITE:
505
- return _this.isRunning() && !_this.isExecutingQueueFull();
515
+ // 這三種模式的任務數在 AssignTaskQueue 中只有一個或一組
516
+ return _this.isRunning() && !_this.isExecutingQueueFull() && _this.getCountOfAssignTaskInQueue() > 0;
506
517
  default:
507
518
  throw new _exceptioner["default"](4005, "this.state ==> ".concat(_this.state));
508
519
  }
@@ -637,6 +648,16 @@ var InfinitePool = /*#__PURE__*/function () {
637
648
  }
638
649
  }, _callee9);
639
650
  })));
651
+ /** [新增] 輔助方法:獲取用於重複執行模式的任務資訊(複製任務並賦予新 hash) */
652
+ (0, _defineProperty2["default"])(this, "getTaskInfoForRepetitiveRun", function (originalTaskInfo) {
653
+ // 必須複製任務函數並給予一個新的 hash,用於在 executingTaskQueue 中追蹤
654
+ var newTaskInfo = {
655
+ task: originalTaskInfo.task,
656
+ hash: _index2.utiller.getRandomHash()
657
+ };
658
+ _this.appendHashTaskMap(newTaskInfo); // 追蹤這個新的 hash 直到任務開始執行
659
+ return newTaskInfo;
660
+ });
640
661
  /** taskInfo = { task, hash }*/
641
662
  (0, _defineProperty2["default"])(this, "getTaskInfoDependOnPriority", function () {
642
663
  var _iterator3 = _createForOfIteratorHelper(_configerer.configerer.POOLLER_PRIORITY),
@@ -647,13 +668,17 @@ var InfinitePool = /*#__PURE__*/function () {
647
668
  if (_this.queueOfAssignTask[prior].length > 0) {
648
669
  switch (_this.state) {
649
670
  case _configerer.configerer.POOLLER_STATE.RUN_BY_EACH_TASK:
671
+ // [修正] RUN_BY_EACH_TASK: 移除任務
650
672
  return _this.queueOfAssignTask[prior].shift();
651
673
  case _configerer.configerer.POOLLER_STATE.RUN_BY_PARAMS:
652
674
  case _configerer.configerer.POOLLER_STATE.RUN_BY_TIMES:
653
675
  case _configerer.configerer.POOLLER_STATE.RUN_INFINITE:
654
- var taskInfo = _this.queueOfAssignTask[prior].shift();
655
- _this.add(taskInfo.task);
656
- return taskInfo;
676
+ // [重大 Bug 修正] 針對重複執行模式:
677
+ // 1. 不使用 shift() 移除任務 (因為需要重複執行)
678
+ // 2. 獲取任務函數的引用 (Peek)
679
+ // 3. 複製任務函數並賦予新的 Hash 以便追蹤本次執行
680
+ var originalTaskInfo = _this.queueOfAssignTask[prior][0];
681
+ return _this.getTaskInfoForRepetitiveRun(originalTaskInfo);
657
682
  default:
658
683
  throw new _exceptioner["default"](4005, "this.state ==> ".concat(_this.state));
659
684
  }
@@ -894,9 +919,11 @@ var InfinitePool = /*#__PURE__*/function () {
894
919
  function setState(_state) {
895
920
  this.state = _state;
896
921
  }
922
+
923
+ /** [優化] 重新命名以明確說明它具有副作用 (Side Effect) */
897
924
  }, {
898
- key: "isFirstTaskCompleted",
899
- value: function isFirstTaskCompleted() {
925
+ key: "checkAndMarkInitialTaskStatus",
926
+ value: function checkAndMarkInitialTaskStatus() {
900
927
  if (!this.initialTaskCompleted) {
901
928
  this.initialTaskCompleted = true;
902
929
  return false;
@@ -950,11 +977,13 @@ var InfinitePool = /*#__PURE__*/function () {
950
977
  while (1) switch (_context11.prev = _context11.next) {
951
978
  case 0:
952
979
  this.printLogMessage("448984466, \u8D70\u9032\u4F86\u4E86 syncTaskDispatcher()");
953
- initialTaskShouldNotRun = this.disableFirstRun && !this.isFirstTaskCompleted();
980
+
981
+ // [修正/優化] 使用新的方法名稱,並將檢查和標記分開處理
982
+ initialTaskShouldNotRun = this.disableFirstRun && !this.checkAndMarkInitialTaskStatus();
954
983
  isExecutingTaskAlmostFull = this.queueOfExecutingTask.length >= this.maximumOfWorker - 1;
955
984
  /** 因為走能到syncTaskDispatcher表示其中一個工作完成了, 這個瞬間不可能 === maximumOfWorker,
956
- * 所以必須減1, 除非這個syncTaskDispatcher是單獨一個線程 */
957
- comparison = this.isFirstTaskCompleted() && isExecutingTaskAlmostFull && this.enableOfTaskSleepByInterval;
985
+ * 所以必須減1, 除非這個syncTaskDispatcher是單獨一個線程 */
986
+ comparison = this.checkAndMarkInitialTaskStatus() && isExecutingTaskAlmostFull && this.enableOfTaskSleepByInterval;
958
987
  if (!(initialTaskShouldNotRun || comparison)) {
959
988
  _context11.next = 9;
960
989
  break;
@@ -974,8 +1003,13 @@ var InfinitePool = /*#__PURE__*/function () {
974
1003
  _context11.next = 17;
975
1004
  break;
976
1005
  }
977
- promise = this.taskWrapper(taskInfo.task, taskInfo.hash, this.queueOfWaitingParam.shift());
978
- this.removeTaskMapByHash(taskInfo.hash);
1006
+ promise = this.taskWrapper(taskInfo.task, taskInfo.hash, this.queueOfWaitingParam.shift()); // 如果是重複執行的 Task,就不移除 TaskMap,因為它只追蹤任務函數的參考,並在下次執行時產生新的 Hash
1007
+ if (this.state === _configerer.configerer.POOLLER_STATE.RUN_BY_EACH_TASK) {
1008
+ this.removeTaskMapByHash(taskInfo.hash);
1009
+ } else {
1010
+ // 對於重複執行的任務,只需移除本次執行時生成的臨時 Hash
1011
+ // 由於 getTaskInfoDependOnPriority 現在會產生新的 hash,這裡不需要額外處理
1012
+ }
979
1013
  this.appendTaskToExecuteQueue(taskInfo.hash, promise);
980
1014
  _context11.next = 19;
981
1015
  break;
@@ -1005,805 +1039,6 @@ var InfinitePool = /*#__PURE__*/function () {
1005
1039
  function isWait4ResultTask(hash) {
1006
1040
  return this.mapOfHashNCallbackWrapper[hash] !== undefined;
1007
1041
  }
1008
- }, {
1009
- key: "exampleOfRunByTaskWait4ResultAndRunInBackground",
1010
- value: (
1011
- /** following function are examples **/
1012
- /** following function are examples **/
1013
- /** following function are examples **/
1014
- /** following function are examples **/
1015
- function () {
1016
- var _exampleOfRunByTaskWait4ResultAndRunInBackground = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee14() {
1017
- var pool, asyncTask, startTimoutTask;
1018
- return _regenerator["default"].wrap(function _callee14$(_context14) {
1019
- while (1) switch (_context14.prev = _context14.next) {
1020
- case 0:
1021
- startTimoutTask = function _startTimoutTask(delayTime, taskSpendTime, sign) {
1022
- var priority = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : "low";
1023
- setTimeout( /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee13() {
1024
- var printSign;
1025
- return _regenerator["default"].wrap(function _callee13$(_context13) {
1026
- while (1) switch (_context13.prev = _context13.next) {
1027
- case 0:
1028
- _context13.prev = 0;
1029
- _context13.next = 3;
1030
- return pool.addTaskAndWait4Result(asyncTask(sign, taskSpendTime), priority);
1031
- case 3:
1032
- printSign = _context13.sent;
1033
- _index2.utiller.appendInfo("answer => ", printSign);
1034
- _context13.next = 10;
1035
- break;
1036
- case 7:
1037
- _context13.prev = 7;
1038
- _context13.t0 = _context13["catch"](0);
1039
- _index2.utiller.appendError("sign ".concat(sign, " perform fail"), _context13.t0.message);
1040
- case 10:
1041
- case "end":
1042
- return _context13.stop();
1043
- }
1044
- }, _callee13, null, [[0, 7]]);
1045
- })), delayTime);
1046
- };
1047
- asyncTask = function _asyncTask(sign) {
1048
- var taskSpend = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2000;
1049
- return /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee12() {
1050
- return _regenerator["default"].wrap(function _callee12$(_context12) {
1051
- while (1) switch (_context12.prev = _context12.next) {
1052
- case 0:
1053
- _context12.next = 2;
1054
- return _index2.utiller.syncDelay(taskSpend);
1055
- case 2:
1056
- return _context12.abrupt("return", sign);
1057
- case 3:
1058
- case "end":
1059
- return _context12.stop();
1060
- }
1061
- }, _callee12);
1062
- }));
1063
- };
1064
- pool = new InfinitePool(1, "david").runByEachTaskInBackGround();
1065
- pool.enableTaskTimeout(true, 3000);
1066
- startTimoutTask(0, 1000, "A", "low");
1067
- startTimoutTask(1000, 1000, "C", "low");
1068
- startTimoutTask(1000, 1000, "G", "high");
1069
- startTimoutTask(500, 4000, "B", "low");
1070
- startTimoutTask(3000, 1000, "D", "medium");
1071
- startTimoutTask(10000, 2000, "E", "medium");
1072
- case 10:
1073
- if (!pool.isRunning()) {
1074
- _context14.next = 17;
1075
- break;
1076
- }
1077
- console.log("system is running");
1078
- pool.showState();
1079
- _context14.next = 15;
1080
- return _index2.utiller.syncDelay(1000);
1081
- case 15:
1082
- _context14.next = 10;
1083
- break;
1084
- case 17:
1085
- case "end":
1086
- return _context14.stop();
1087
- }
1088
- }, _callee14);
1089
- }));
1090
- function exampleOfRunByTaskWait4ResultAndRunInBackground() {
1091
- return _exampleOfRunByTaskWait4ResultAndRunInBackground.apply(this, arguments);
1092
- }
1093
- return exampleOfRunByTaskWait4ResultAndRunInBackground;
1094
- }())
1095
- }, {
1096
- key: "exampleOfRunInBackgroundInfinite",
1097
- value: function () {
1098
- var _exampleOfRunInBackgroundInfinite = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee17() {
1099
- var myAsyncTask, _myAsyncTask, pool;
1100
- return _regenerator["default"].wrap(function _callee17$(_context17) {
1101
- while (1) switch (_context17.prev = _context17.next) {
1102
- case 0:
1103
- _myAsyncTask = function _myAsyncTask3() {
1104
- _myAsyncTask = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee16() {
1105
- var taskSpend, sign;
1106
- return _regenerator["default"].wrap(function _callee16$(_context16) {
1107
- while (1) switch (_context16.prev = _context16.next) {
1108
- case 0:
1109
- taskSpend = _index2.utiller.getRandomValue(1000, 1000);
1110
- sign = _index2.utiller.getRandomHash(15);
1111
- if (!_index2.utiller.isOdd(taskSpend)) {
1112
- _context16.next = 4;
1113
- break;
1114
- }
1115
- throw new Error("45100 Oops, ".concat(taskSpend, " is odd\u5566"));
1116
- case 4:
1117
- _context16.next = 6;
1118
- return _index2.utiller.syncDelay(taskSpend);
1119
- case 6:
1120
- _index2.utiller.appendInfo(sign, taskSpend);
1121
- return _context16.abrupt("return", sign);
1122
- case 8:
1123
- case "end":
1124
- return _context16.stop();
1125
- }
1126
- }, _callee16);
1127
- }));
1128
- return _myAsyncTask.apply(this, arguments);
1129
- };
1130
- myAsyncTask = function _myAsyncTask2() {
1131
- return _myAsyncTask.apply(this, arguments);
1132
- };
1133
- pool = new InfinitePool(2);
1134
- pool.runInfiniteInBackground(myAsyncTask, 5000);
1135
- pool.setDisableFirstRun(true);
1136
- pool.setTaskFailHandler(function (error) {
1137
- console.error(error.message);
1138
- });
1139
- setTimeout( /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee15() {
1140
- return _regenerator["default"].wrap(function _callee15$(_context15) {
1141
- while (1) switch (_context15.prev = _context15.next) {
1142
- case 0:
1143
- _context15.next = 2;
1144
- return pool.stopInBackground();
1145
- case 2:
1146
- case "end":
1147
- return _context15.stop();
1148
- }
1149
- }, _callee15);
1150
- })), 100000);
1151
- case 7:
1152
- if (!pool.isRunning()) {
1153
- _context17.next = 14;
1154
- break;
1155
- }
1156
- _index2.utiller.appendInfo("system is running");
1157
- _context17.next = 11;
1158
- return _index2.utiller.syncDelay(3000);
1159
- case 11:
1160
- pool.showState();
1161
- _context17.next = 7;
1162
- break;
1163
- case 14:
1164
- case "end":
1165
- return _context17.stop();
1166
- }
1167
- }, _callee17);
1168
- }));
1169
- function exampleOfRunInBackgroundInfinite() {
1170
- return _exampleOfRunInBackgroundInfinite.apply(this, arguments);
1171
- }
1172
- return exampleOfRunInBackgroundInfinite;
1173
- }()
1174
- }, {
1175
- key: "exampleOfRunInfinite",
1176
- value: function () {
1177
- var _exampleOfRunInfinite = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee19() {
1178
- return _regenerator["default"].wrap(function _callee19$(_context19) {
1179
- while (1) switch (_context19.prev = _context19.next) {
1180
- case 0:
1181
- _context19.next = 2;
1182
- return new InfinitePool(3).runInInfinite( /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee18() {
1183
- return _regenerator["default"].wrap(function _callee18$(_context18) {
1184
- while (1) switch (_context18.prev = _context18.next) {
1185
- case 0:
1186
- console.log("1000");
1187
- case 1:
1188
- case "end":
1189
- return _context18.stop();
1190
- }
1191
- }, _callee18);
1192
- })), 1000);
1193
- case 2:
1194
- case "end":
1195
- return _context19.stop();
1196
- }
1197
- }, _callee19);
1198
- }));
1199
- function exampleOfRunInfinite() {
1200
- return _exampleOfRunInfinite.apply(this, arguments);
1201
- }
1202
- return exampleOfRunInfinite;
1203
- }()
1204
- }, {
1205
- key: "exampleOfRunByParamInBackground",
1206
- value: function () {
1207
- var _exampleOfRunByParamInBackground = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee21() {
1208
- var pool;
1209
- return _regenerator["default"].wrap(function _callee21$(_context21) {
1210
- while (1) switch (_context21.prev = _context21.next) {
1211
- case 0:
1212
- pool = new InfinitePool(0);
1213
- pool.runByParamInBackGround( /*#__PURE__*/function () {
1214
- var _ref12 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee20(param) {
1215
- var ms;
1216
- return _regenerator["default"].wrap(function _callee20$(_context20) {
1217
- while (1) switch (_context20.prev = _context20.next) {
1218
- case 0:
1219
- _context20.next = 2;
1220
- return _index2.utiller.syncDelayRandom();
1221
- case 2:
1222
- ms = _context20.sent;
1223
- console.log("wait ".concat(ms, " ms"), "param", param);
1224
- case 4:
1225
- case "end":
1226
- return _context20.stop();
1227
- }
1228
- }, _callee20);
1229
- }));
1230
- return function (_x4) {
1231
- return _ref12.apply(this, arguments);
1232
- };
1233
- }(), "david", "susan", "golden", "weber", "kevin");
1234
- setTimeout(function () {
1235
- pool.appendParamInToQueue("apple", "Rui", "Rui", "Qui", "Seiu", "Bikky", "apple", "Rui", "Rui", "Qui", "Seiu", "Bikky");
1236
- }, 12000);
1237
- setTimeout(function () {
1238
- pool.setWorker(5);
1239
- }, 6000);
1240
- case 4:
1241
- if (!true) {
1242
- _context21.next = 10;
1243
- break;
1244
- }
1245
- _index2.utiller.appendInfo("system is running after 2 seconds");
1246
- _context21.next = 8;
1247
- return _index2.utiller.syncDelay(2000);
1248
- case 8:
1249
- _context21.next = 4;
1250
- break;
1251
- case 10:
1252
- case "end":
1253
- return _context21.stop();
1254
- }
1255
- }, _callee21);
1256
- }));
1257
- function exampleOfRunByParamInBackground() {
1258
- return _exampleOfRunByParamInBackground.apply(this, arguments);
1259
- }
1260
- return exampleOfRunByParamInBackground;
1261
- }()
1262
- }, {
1263
- key: "exampleOfRunByTimesInBackground",
1264
- value: function () {
1265
- var _exampleOfRunByTimesInBackground = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee23() {
1266
- var pool, count;
1267
- return _regenerator["default"].wrap(function _callee23$(_context23) {
1268
- while (1) switch (_context23.prev = _context23.next) {
1269
- case 0:
1270
- pool = new InfinitePool(0);
1271
- count = 0;
1272
- pool.runByTimesInBackGround( /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee22() {
1273
- var ms;
1274
- return _regenerator["default"].wrap(function _callee22$(_context22) {
1275
- while (1) switch (_context22.prev = _context22.next) {
1276
- case 0:
1277
- _context22.next = 2;
1278
- return _index2.utiller.syncDelayRandom();
1279
- case 2:
1280
- ms = _context22.sent;
1281
- count++;
1282
- console.log("wait for time: ".concat(ms, " ms, count:").concat(count));
1283
- case 5:
1284
- case "end":
1285
- return _context22.stop();
1286
- }
1287
- }, _callee22);
1288
- })), 50);
1289
- setTimeout(function () {
1290
- pool.setWorker(3);
1291
- }, 10000);
1292
- case 4:
1293
- if (!true) {
1294
- _context23.next = 10;
1295
- break;
1296
- }
1297
- _index2.utiller.appendInfo("system is running after 2.5 seconds");
1298
- _context23.next = 8;
1299
- return _index2.utiller.syncDelay(2500);
1300
- case 8:
1301
- _context23.next = 4;
1302
- break;
1303
- case 10:
1304
- case "end":
1305
- return _context23.stop();
1306
- }
1307
- }, _callee23);
1308
- }));
1309
- function exampleOfRunByTimesInBackground() {
1310
- return _exampleOfRunByTimesInBackground.apply(this, arguments);
1311
- }
1312
- return exampleOfRunByTimesInBackground;
1313
- }()
1314
- }, {
1315
- key: "exampleOfRunByParam",
1316
- value: function () {
1317
- var _exampleOfRunByParam = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee25() {
1318
- var oneToTen, pool;
1319
- return _regenerator["default"].wrap(function _callee25$(_context25) {
1320
- while (1) switch (_context25.prev = _context25.next) {
1321
- case 0:
1322
- oneToTen = _lodash["default"].range(1, 10);
1323
- pool = new InfinitePool(5);
1324
- _index2.utiller.appendInfo("....start method of runByParams");
1325
- _context25.next = 5;
1326
- return pool.runByParams.apply(pool, [( /*#__PURE__*/function () {
1327
- var _ref14 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee24(param) {
1328
- var ms;
1329
- return _regenerator["default"].wrap(function _callee24$(_context24) {
1330
- while (1) switch (_context24.prev = _context24.next) {
1331
- case 0:
1332
- _context24.next = 2;
1333
- return _index2.utiller.syncDelayRandom();
1334
- case 2:
1335
- ms = _context24.sent;
1336
- console.log(param, "".concat(ms, " ms"));
1337
- case 4:
1338
- case "end":
1339
- return _context24.stop();
1340
- }
1341
- }, _callee24);
1342
- }));
1343
- return function (_x5) {
1344
- return _ref14.apply(this, arguments);
1345
- };
1346
- }())].concat((0, _toConsumableArray2["default"])(oneToTen)));
1347
- case 5:
1348
- _index2.utiller.appendInfo("....finish method of runByParams");
1349
- case 6:
1350
- case "end":
1351
- return _context25.stop();
1352
- }
1353
- }, _callee25);
1354
- }));
1355
- function exampleOfRunByParam() {
1356
- return _exampleOfRunByParam.apply(this, arguments);
1357
- }
1358
- return exampleOfRunByParam;
1359
- }()
1360
- }, {
1361
- key: "exampleOfRunByTask",
1362
- value: function () {
1363
- var _exampleOfRunByTask = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee26() {
1364
- var pool, tasks, all;
1365
- return _regenerator["default"].wrap(function _callee26$(_context26) {
1366
- while (1) switch (_context26.prev = _context26.next) {
1367
- case 0:
1368
- pool = new InfinitePool(1);
1369
- tasks = _lodash["default"].range(1, 5).map(function (each) {
1370
- return _index2.utiller.asyncUnitTaskFunction(each * 1000);
1371
- });
1372
- _index2.utiller.appendInfo("....start method of exampleOfRunByTask");
1373
- _context26.next = 5;
1374
- return pool.runByEachTask(tasks);
1375
- case 5:
1376
- all = _context26.sent;
1377
- _index2.utiller.appendInfo(all);
1378
- _index2.utiller.appendInfo("....finish method of exampleOfRunByTask");
1379
- case 8:
1380
- case "end":
1381
- return _context26.stop();
1382
- }
1383
- }, _callee26);
1384
- }));
1385
- function exampleOfRunByTask() {
1386
- return _exampleOfRunByTask.apply(this, arguments);
1387
- }
1388
- return exampleOfRunByTask;
1389
- }()
1390
- }, {
1391
- key: "exampleOfRunByTimes",
1392
- value: function () {
1393
- var _exampleOfRunByTimes = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee28() {
1394
- var pool, time;
1395
- return _regenerator["default"].wrap(function _callee28$(_context28) {
1396
- while (1) switch (_context28.prev = _context28.next) {
1397
- case 0:
1398
- pool = new InfinitePool(5);
1399
- time = 0;
1400
- _index2.utiller.appendInfo("....start method of runByTimes");
1401
- _context28.next = 5;
1402
- return pool.runByTimes( /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee27() {
1403
- return _regenerator["default"].wrap(function _callee27$(_context27) {
1404
- while (1) switch (_context27.prev = _context27.next) {
1405
- case 0:
1406
- _context27.next = 2;
1407
- return _index2.utiller.syncDelay(1000);
1408
- case 2:
1409
- time++;
1410
- console.log("execute the ".concat(time, " time"));
1411
- case 4:
1412
- case "end":
1413
- return _context27.stop();
1414
- }
1415
- }, _callee27);
1416
- })), 10);
1417
- case 5:
1418
- _index2.utiller.appendInfo("....finish method of runByTimes");
1419
- case 6:
1420
- case "end":
1421
- return _context28.stop();
1422
- }
1423
- }, _callee28);
1424
- }));
1425
- function exampleOfRunByTimes() {
1426
- return _exampleOfRunByTimes.apply(this, arguments);
1427
- }
1428
- return exampleOfRunByTimes;
1429
- }()
1430
- }, {
1431
- key: "exampleOfAsyncTaskOfFunctionInside",
1432
- value: function () {
1433
- var _exampleOfAsyncTaskOfFunctionInside = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee35() {
1434
- var result;
1435
- return _regenerator["default"].wrap(function _callee35$(_context35) {
1436
- while (1) switch (_context35.prev = _context35.next) {
1437
- case 0:
1438
- result = [];
1439
- _context35.next = 3;
1440
- return new InfinitePool(2).runByEachTask([/*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee29() {
1441
- var item;
1442
- return _regenerator["default"].wrap(function _callee29$(_context29) {
1443
- while (1) switch (_context29.prev = _context29.next) {
1444
- case 0:
1445
- item = _index2.utiller.asyncUnitTaskFunction(1000);
1446
- _context29.t0 = result;
1447
- _context29.next = 4;
1448
- return item();
1449
- case 4:
1450
- _context29.t1 = _context29.sent;
1451
- _context29.t0.push.call(_context29.t0, _context29.t1);
1452
- case 6:
1453
- case "end":
1454
- return _context29.stop();
1455
- }
1456
- }, _callee29);
1457
- })), /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee30() {
1458
- var item;
1459
- return _regenerator["default"].wrap(function _callee30$(_context30) {
1460
- while (1) switch (_context30.prev = _context30.next) {
1461
- case 0:
1462
- _context30.next = 2;
1463
- return _index2.utiller.asyncUnitTaskFunction(3000);
1464
- case 2:
1465
- item = _context30.sent;
1466
- _context30.t0 = result;
1467
- _context30.next = 6;
1468
- return item();
1469
- case 6:
1470
- _context30.t1 = _context30.sent;
1471
- _context30.t0.push.call(_context30.t0, _context30.t1);
1472
- case 8:
1473
- case "end":
1474
- return _context30.stop();
1475
- }
1476
- }, _callee30);
1477
- })), /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee31() {
1478
- var item;
1479
- return _regenerator["default"].wrap(function _callee31$(_context31) {
1480
- while (1) switch (_context31.prev = _context31.next) {
1481
- case 0:
1482
- _context31.next = 2;
1483
- return _index2.utiller.asyncUnitTaskFunction(4000);
1484
- case 2:
1485
- item = _context31.sent;
1486
- _context31.t0 = result;
1487
- _context31.next = 6;
1488
- return item();
1489
- case 6:
1490
- _context31.t1 = _context31.sent;
1491
- _context31.t0.push.call(_context31.t0, _context31.t1);
1492
- case 8:
1493
- case "end":
1494
- return _context31.stop();
1495
- }
1496
- }, _callee31);
1497
- })), /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee32() {
1498
- var item;
1499
- return _regenerator["default"].wrap(function _callee32$(_context32) {
1500
- while (1) switch (_context32.prev = _context32.next) {
1501
- case 0:
1502
- _context32.next = 2;
1503
- return _index2.utiller.asyncUnitTaskFunction(9000);
1504
- case 2:
1505
- item = _context32.sent;
1506
- _context32.t0 = result;
1507
- _context32.next = 6;
1508
- return item();
1509
- case 6:
1510
- _context32.t1 = _context32.sent;
1511
- _context32.t0.push.call(_context32.t0, _context32.t1);
1512
- case 8:
1513
- case "end":
1514
- return _context32.stop();
1515
- }
1516
- }, _callee32);
1517
- })), /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee33() {
1518
- var item;
1519
- return _regenerator["default"].wrap(function _callee33$(_context33) {
1520
- while (1) switch (_context33.prev = _context33.next) {
1521
- case 0:
1522
- _context33.next = 2;
1523
- return _index2.utiller.asyncUnitTaskFunction(6000);
1524
- case 2:
1525
- item = _context33.sent;
1526
- _context33.t0 = result;
1527
- _context33.next = 6;
1528
- return item();
1529
- case 6:
1530
- _context33.t1 = _context33.sent;
1531
- _context33.t0.push.call(_context33.t0, _context33.t1);
1532
- case 8:
1533
- case "end":
1534
- return _context33.stop();
1535
- }
1536
- }, _callee33);
1537
- })), /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee34() {
1538
- var item;
1539
- return _regenerator["default"].wrap(function _callee34$(_context34) {
1540
- while (1) switch (_context34.prev = _context34.next) {
1541
- case 0:
1542
- _context34.next = 2;
1543
- return _index2.utiller.asyncUnitTaskFunction(6000);
1544
- case 2:
1545
- item = _context34.sent;
1546
- _context34.t0 = result;
1547
- _context34.next = 6;
1548
- return item();
1549
- case 6:
1550
- _context34.t1 = _context34.sent;
1551
- _context34.t0.push.call(_context34.t0, _context34.t1);
1552
- case 8:
1553
- case "end":
1554
- return _context34.stop();
1555
- }
1556
- }, _callee34);
1557
- }))]);
1558
- case 3:
1559
- _index2.utiller.appendInfo(result);
1560
- case 4:
1561
- case "end":
1562
- return _context35.stop();
1563
- }
1564
- }, _callee35);
1565
- }));
1566
- function exampleOfAsyncTaskOfFunctionInside() {
1567
- return _exampleOfAsyncTaskOfFunctionInside.apply(this, arguments);
1568
- }
1569
- return exampleOfAsyncTaskOfFunctionInside;
1570
- }()
1571
- }, {
1572
- key: "exampleOfInfiniteUnStopLoopingIssue",
1573
- value: function () {
1574
- var _exampleOfInfiniteUnStopLoopingIssue = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee37() {
1575
- var persistTone, _persistTone, poollers, pool, isRequiredTerminate, random, _iterator6, _step6, pooller;
1576
- return _regenerator["default"].wrap(function _callee37$(_context37) {
1577
- while (1) switch (_context37.prev = _context37.next) {
1578
- case 0:
1579
- _persistTone = function _persistTone3() {
1580
- _persistTone = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee36() {
1581
- var sign, time;
1582
- return _regenerator["default"].wrap(function _callee36$(_context36) {
1583
- while (1) switch (_context36.prev = _context36.next) {
1584
- case 0:
1585
- _context36.prev = 0;
1586
- sign = _index2.utiller.getRandomHash(20);
1587
- _context36.next = 4;
1588
- return _index2.utiller.syncDelayRandom(1, 10);
1589
- case 4:
1590
- time = _context36.sent;
1591
- _index2.utiller.appendInfo("".concat(time, " ms, yoyoyo ").concat(sign));
1592
- // Util.appendInfo(`沒有TONE可以下載了....隨機睡個${await Util.syncDelayRandom(1500, 3500)}`)
1593
- return _context36.abrupt("return", "\u4EFB\u52D9ID:".concat(sign));
1594
- case 9:
1595
- _context36.prev = 9;
1596
- _context36.t0 = _context36["catch"](0);
1597
- _index2.utiller.appendError(_context36.t0.message);
1598
- case 12:
1599
- case "end":
1600
- return _context36.stop();
1601
- }
1602
- }, _callee36, null, [[0, 9]]);
1603
- }));
1604
- return _persistTone.apply(this, arguments);
1605
- };
1606
- persistTone = function _persistTone2() {
1607
- return _persistTone.apply(this, arguments);
1608
- };
1609
- poollers = [];
1610
- pool = new InfinitePool(3);
1611
- pool.setPoolId("tone fetch");
1612
- pool.setDisableFirstRun(true);
1613
- pool.runInfiniteInBackground(persistTone, 5000);
1614
- pool.setTaskFailHandler(function (error) {
1615
- return _index2.utiller.appendError("5165 error ".concat(error.message));
1616
- });
1617
- poollers.push(pool);
1618
- isRequiredTerminate = false;
1619
- setTimeout(function () {
1620
- isRequiredTerminate = true;
1621
- }, 20000);
1622
- case 11:
1623
- if (!true) {
1624
- _context37.next = 43;
1625
- break;
1626
- }
1627
- _index2.utiller.exeAll(poollers, function (each) {
1628
- return each.showState();
1629
- });
1630
- _context37.next = 15;
1631
- return _index2.utiller.syncDelayRandom();
1632
- case 15:
1633
- random = _context37.sent;
1634
- _context37.next = 18;
1635
- return _index2.utiller.syncDelay(random);
1636
- case 18:
1637
- if (!isRequiredTerminate) {
1638
- _context37.next = 41;
1639
- break;
1640
- }
1641
- _index2.utiller.appendInfo("\u4E3B\u7DDA\u7A0B\u6536\u5230\u95DC\u9589\u6307\u4EE4...");
1642
- _iterator6 = _createForOfIteratorHelper(poollers);
1643
- _context37.prev = 21;
1644
- _iterator6.s();
1645
- case 23:
1646
- if ((_step6 = _iterator6.n()).done) {
1647
- _context37.next = 32;
1648
- break;
1649
- }
1650
- pooller = _step6.value;
1651
- _index2.utiller.appendInfo("POOLER ".concat(pooller.getPoolId(), " \u6B63\u5728\u95DC\u9589\u4E2D"));
1652
- _context37.next = 28;
1653
- return pooller.stopInBackground();
1654
- case 28:
1655
- pooller.showState();
1656
- _index2.utiller.appendInfo("POOLER ".concat(pooller.getPoolId(), " \u95DC\u9589\u6210\u529F!"));
1657
- case 30:
1658
- _context37.next = 23;
1659
- break;
1660
- case 32:
1661
- _context37.next = 37;
1662
- break;
1663
- case 34:
1664
- _context37.prev = 34;
1665
- _context37.t0 = _context37["catch"](21);
1666
- _iterator6.e(_context37.t0);
1667
- case 37:
1668
- _context37.prev = 37;
1669
- _iterator6.f();
1670
- return _context37.finish(37);
1671
- case 40:
1672
- return _context37.abrupt("break", 43);
1673
- case 41:
1674
- _context37.next = 11;
1675
- break;
1676
- case 43:
1677
- case "end":
1678
- return _context37.stop();
1679
- }
1680
- }, _callee37, null, [[21, 34, 37, 40]]);
1681
- }));
1682
- function exampleOfInfiniteUnStopLoopingIssue() {
1683
- return _exampleOfInfiniteUnStopLoopingIssue.apply(this, arguments);
1684
- }
1685
- return exampleOfInfiniteUnStopLoopingIssue;
1686
- }()
1687
- }, {
1688
- key: "sampleOfEachTaskInFreeMarker",
1689
- value: function () {
1690
- var _sampleOfEachTaskInFreeMarker = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee44() {
1691
- var test;
1692
- return _regenerator["default"].wrap(function _callee44$(_context44) {
1693
- while (1) switch (_context44.prev = _context44.next) {
1694
- case 0:
1695
- test = [];
1696
- _context44.next = 3;
1697
- return new InfinitePool(6).runByEachTask([/*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee38() {
1698
- var answer;
1699
- return _regenerator["default"].wrap(function _callee38$(_context38) {
1700
- while (1) switch (_context38.prev = _context38.next) {
1701
- case 0:
1702
- _context38.next = 2;
1703
- return _index2.utiller.syncDelayRandom(2000, 5000);
1704
- case 2:
1705
- answer = _context38.sent;
1706
- console.log(answer);
1707
- test.push(answer);
1708
- case 5:
1709
- case "end":
1710
- return _context38.stop();
1711
- }
1712
- }, _callee38);
1713
- })), /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee39() {
1714
- var answer;
1715
- return _regenerator["default"].wrap(function _callee39$(_context39) {
1716
- while (1) switch (_context39.prev = _context39.next) {
1717
- case 0:
1718
- _context39.next = 2;
1719
- return _index2.utiller.syncDelayRandom(2000, 5000);
1720
- case 2:
1721
- answer = _context39.sent;
1722
- console.log(answer);
1723
- test.push(answer);
1724
- case 5:
1725
- case "end":
1726
- return _context39.stop();
1727
- }
1728
- }, _callee39);
1729
- })), /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee40() {
1730
- var answer;
1731
- return _regenerator["default"].wrap(function _callee40$(_context40) {
1732
- while (1) switch (_context40.prev = _context40.next) {
1733
- case 0:
1734
- _context40.next = 2;
1735
- return _index2.utiller.syncDelayRandom(2000, 5000);
1736
- case 2:
1737
- answer = _context40.sent;
1738
- console.log(answer);
1739
- test.push(answer);
1740
- case 5:
1741
- case "end":
1742
- return _context40.stop();
1743
- }
1744
- }, _callee40);
1745
- })), /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee41() {
1746
- var answer;
1747
- return _regenerator["default"].wrap(function _callee41$(_context41) {
1748
- while (1) switch (_context41.prev = _context41.next) {
1749
- case 0:
1750
- _context41.next = 2;
1751
- return _index2.utiller.syncDelayRandom(2000, 5000);
1752
- case 2:
1753
- answer = _context41.sent;
1754
- console.log(answer);
1755
- test.push(answer);
1756
- case 5:
1757
- case "end":
1758
- return _context41.stop();
1759
- }
1760
- }, _callee41);
1761
- })), /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee42() {
1762
- var answer;
1763
- return _regenerator["default"].wrap(function _callee42$(_context42) {
1764
- while (1) switch (_context42.prev = _context42.next) {
1765
- case 0:
1766
- _context42.next = 2;
1767
- return _index2.utiller.syncDelayRandom(2000, 5000);
1768
- case 2:
1769
- answer = _context42.sent;
1770
- console.log(answer);
1771
- test.push(answer);
1772
- case 5:
1773
- case "end":
1774
- return _context42.stop();
1775
- }
1776
- }, _callee42);
1777
- })), /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee43() {
1778
- var answer;
1779
- return _regenerator["default"].wrap(function _callee43$(_context43) {
1780
- while (1) switch (_context43.prev = _context43.next) {
1781
- case 0:
1782
- _context43.next = 2;
1783
- return _index2.utiller.syncDelayRandom(2000, 5000);
1784
- case 2:
1785
- answer = _context43.sent;
1786
- console.log(answer);
1787
- test.push(answer);
1788
- case 5:
1789
- case "end":
1790
- return _context43.stop();
1791
- }
1792
- }, _callee43);
1793
- }))]);
1794
- case 3:
1795
- console.log("我好了!!", test);
1796
- case 4:
1797
- case "end":
1798
- return _context44.stop();
1799
- }
1800
- }, _callee44);
1801
- }));
1802
- function sampleOfEachTaskInFreeMarker() {
1803
- return _sampleOfEachTaskInFreeMarker.apply(this, arguments);
1804
- }
1805
- return sampleOfEachTaskInFreeMarker;
1806
- }()
1807
1042
  }]);
1808
1043
  }();
1809
1044
  var _default = exports["default"] = InfinitePool;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utiller",
3
- "version": "1.0.379",
3
+ "version": "1.0.380",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -11,7 +11,7 @@
11
11
  "license": "ISC",
12
12
  "dependencies": {
13
13
  "configerer": "^1.0.23",
14
- "utiller": "^1.0.378",
14
+ "utiller": "^1.0.379",
15
15
  "linepayer": "^1.0.15",
16
16
  "databazer": "^1.0.17",
17
17
  "lodash": "^4.17.20",