utiller 1.0.77 → 1.0.78

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.
@@ -37,8 +37,6 @@ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o =
37
37
 
38
38
  function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
39
39
 
40
- var _run = new WeakMap();
41
-
42
40
  /**
43
41
  *
44
42
  Pooller 有以下特點:
@@ -50,6 +48,10 @@ Pooller 有以下特點:
50
48
  6.可以設定taskFailHandler, 這樣遇到錯誤就不會停掉poollers
51
49
  *
52
50
  */
51
+ var SPECIFICITY_DEBUG = false;
52
+
53
+ var _run = new WeakMap();
54
+
53
55
  var InfinitePool = /*#__PURE__*/function () {
54
56
  /** 用來處理Task的延遲,假設要偷網頁東西, 不能太頻繁, 要偽裝成手動只能透過這方式, 如果是multi thread, 延遲是針對worker滿載後,再加進去的那一個 */
55
57
 
@@ -59,6 +61,8 @@ var InfinitePool = /*#__PURE__*/function () {
59
61
 
60
62
  /** 用於 runByParam */
61
63
 
64
+ /** runByTimes 的次數 */
65
+
62
66
  /** 裡面放 {high:[], low:[], medium }
63
67
  * taskQueue就是指裡面有多少Task!
64
68
  * 通常Infinite模式,裡面只會有一個asyncTask
@@ -88,6 +92,7 @@ var InfinitePool = /*#__PURE__*/function () {
88
92
  (0, _defineProperty2["default"])(this, "maxWorker", void 0);
89
93
  (0, _defineProperty2["default"])(this, "ignoreFirstRun", false);
90
94
  (0, _defineProperty2["default"])(this, "queueOfWaitingParam", []);
95
+ (0, _defineProperty2["default"])(this, "countsOfRunByTimes", -1);
91
96
  (0, _defineProperty2["default"])(this, "queueOfAssignTask", {});
92
97
  (0, _defineProperty2["default"])(this, "queueOfExecutingTask", []);
93
98
  (0, _defineProperty2["default"])(this, "isQueuePolling", false);
@@ -139,7 +144,7 @@ var InfinitePool = /*#__PURE__*/function () {
139
144
  (0, _defineProperty2["default"])(this, "isRunning", function () {
140
145
  return _this.isQueuePolling;
141
146
  });
142
- (0, _defineProperty2["default"])(this, "getTaskQueueCount", function () {
147
+ (0, _defineProperty2["default"])(this, "getCountOfAssignTaskInQueue", function () {
143
148
  var size = 0;
144
149
  if (_this.state === _configerer.configerer.POOLLER_STATE.RUN_BY_PARAMS) return _this.queueOfWaitingParam.length;
145
150
 
@@ -196,13 +201,12 @@ var InfinitePool = /*#__PURE__*/function () {
196
201
  task.state = "ING";
197
202
  }
198
203
  });
199
- (0, _defineProperty2["default"])(this, "taskWrapper", function (assignedTask, hashOfTask) {
204
+ (0, _defineProperty2["default"])(this, "taskWrapper", function (assignedTask, hashOfTask, param) {
200
205
  return function () {
201
206
  var self = _this;
202
207
  var timeoutHash = "";
203
208
  var assignedTaskResult;
204
209
  var assignedTaskError;
205
- var param = self.queueOfWaitingParam.shift();
206
210
  var isAssignedTaskCompleted = true;
207
211
  /** 用來判斷task 有沒有走到 catch裡面, 不然resolve了但return undefined, task會不知所措 */
208
212
 
@@ -354,7 +358,7 @@ var InfinitePool = /*#__PURE__*/function () {
354
358
  break;
355
359
  }
356
360
 
357
- _this.printLogMessage("415123, runInInfinite() \u6B63\u5728\u7121\u9650Loop\u4E2D,".concat(_this.getLogMessageOfExecutingTaskQueueCount()));
361
+ _this.printLogMessage("415123, runInInfinite() \u6B63\u5728\u7121\u9650Loop\u4E2D, ".concat(_this.getLogMessageOfExecutingTaskQueueCount()));
358
362
 
359
363
  _context2.next = 18;
360
364
  return (0, _classPrivateFieldGet2["default"])(_this, _run).call(_this);
@@ -427,7 +431,7 @@ var InfinitePool = /*#__PURE__*/function () {
427
431
  _this.beforeRun();
428
432
 
429
433
  case 9:
430
- if (!(_this.ruleOfInfiniteRun() && !_lodash["default"].isEmpty(_this.queueOfWaitingParam))) {
434
+ if (!(_this.ruleOfInfiniteRun() && _lodash["default"].size(_this.queueOfWaitingParam) > 0)) {
431
435
  _context3.next = 14;
432
436
  break;
433
437
  }
@@ -454,9 +458,10 @@ var InfinitePool = /*#__PURE__*/function () {
454
458
  (0, _defineProperty2["default"])(this, "runByEachTaskInBackGround", function () {
455
459
  if (_this.atomicBgInstance !== undefined) clearTimeout(_this.atomicBgInstance);
456
460
  _this.atomicBgInstance = _this.runInBackGround(_this.runByEachTask);
457
- /** 因為偷懶, 所以回傳整個instance, 這樣程式碼就只要寫一行
458
- * const pool = new InfinitePool(1).runByEachTaskInBackGround();
459
- * */
461
+ /**
462
+ * 因為偷懶, 所以回傳整個instance, 這樣程式碼就只要寫一行
463
+ * const pool = new InfinitePool(1).runByEachTaskInBackGround();
464
+ */
460
465
 
461
466
  return _this;
462
467
  });
@@ -486,7 +491,7 @@ var InfinitePool = /*#__PURE__*/function () {
486
491
  return (0, _classPrivateFieldGet2["default"])(_this, _run).call(_this, _this.id);
487
492
 
488
493
  case 8:
489
- if (_this.getTaskQueueCount() <= 0) {
494
+ if (_this.getCountOfAssignTaskInQueue() <= 0) {
490
495
  _this.terminate();
491
496
 
492
497
  _this.printLogMessage("788121, runByEachTask() \u56E0\u70BA taskOfWaitingQueue \u6E05\u7A7A\u800C\u505C\u6B62");
@@ -503,13 +508,15 @@ var InfinitePool = /*#__PURE__*/function () {
503
508
  }, _callee4);
504
509
  })));
505
510
  (0, _defineProperty2["default"])(this, "runByTimes", /*#__PURE__*/function () {
506
- var _ref5 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5(functionOfAsyncTask, times) {
507
- var index;
511
+ var _ref5 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5(functionOfAsyncTask) {
512
+ var times,
513
+ _args5 = arguments;
508
514
  return _regenerator["default"].wrap(function _callee5$(_context5) {
509
515
  while (1) {
510
516
  switch (_context5.prev = _context5.next) {
511
517
  case 0:
512
- _this.maxWorker = 1;
518
+ times = _args5.length > 1 && _args5[1] !== undefined ? _args5[1] : 1;
519
+ _this.countsOfRunByTimes = times;
513
520
 
514
521
  _this.add(functionOfAsyncTask);
515
522
 
@@ -517,11 +524,9 @@ var InfinitePool = /*#__PURE__*/function () {
517
524
 
518
525
  _this.setState(_configerer.configerer.POOLLER_STATE.RUN_BY_TIMES);
519
526
 
520
- index = 0;
521
-
522
527
  case 5:
523
- if (!(index < times)) {
524
- _context5.next = 11;
528
+ if (!(_this.ruleOfInfiniteRun() && _this.countsOfRunByTimes > 0)) {
529
+ _context5.next = 10;
525
530
  break;
526
531
  }
527
532
 
@@ -529,11 +534,10 @@ var InfinitePool = /*#__PURE__*/function () {
529
534
  return (0, _classPrivateFieldGet2["default"])(_this, _run).call(_this);
530
535
 
531
536
  case 8:
532
- index++;
533
537
  _context5.next = 5;
534
538
  break;
535
539
 
536
- case 11:
540
+ case 10:
537
541
  case "end":
538
542
  return _context5.stop();
539
543
  }
@@ -541,7 +545,7 @@ var InfinitePool = /*#__PURE__*/function () {
541
545
  }, _callee5);
542
546
  }));
543
547
 
544
- return function (_x2, _x3) {
548
+ return function (_x2) {
545
549
  return _ref5.apply(this, arguments);
546
550
  };
547
551
  }());
@@ -600,7 +604,29 @@ var InfinitePool = /*#__PURE__*/function () {
600
604
  };
601
605
  _this.handlerOfAssignTaskFail = listener;
602
606
  });
607
+ (0, _defineProperty2["default"])(this, "rulesOfAppendToExecutingTask", function () {
608
+ switch (_this.state) {
609
+ case _configerer.configerer.POOLLER_STATE.RUN_BY_EACH_TASK:
610
+ return _this.isRunning() && !_this.isExecutingQueueFull() && _this.getCountOfAssignTaskInQueue() > 0;
611
+
612
+ case _configerer.configerer.POOLLER_STATE.RUN_BY_PARAMS:
613
+ return _this.isRunning() && !_this.isExecutingQueueFull() && _lodash["default"].size(_this.queueOfWaitingParam) > 0;
614
+
615
+ case _configerer.configerer.POOLLER_STATE.RUN_BY_TIMES:
616
+ return _this.isRunning() && !_this.isExecutingQueueFull() && _this.countsOfRunByTimes > 0;
617
+
618
+ case _configerer.configerer.POOLLER_STATE.RUN_INFINITE:
619
+ return _this.isRunning() && !_this.isExecutingQueueFull();
620
+
621
+ default:
622
+ throw new _exceptioner["default"](4005, "this.state ==> ".concat(_this.state));
623
+ }
624
+ });
603
625
  (0, _defineProperty2["default"])(this, "appendTaskToExecuteQueue", function (hash, promise) {
626
+ if (_lodash["default"].isEqual(_this.state, _configerer.configerer.POOLLER_STATE.RUN_BY_TIMES)) {
627
+ _this.countsOfRunByTimes = _this.countsOfRunByTimes - 1;
628
+ }
629
+
604
630
  var task = {
605
631
  state: "NOT",
606
632
  hash: hash,
@@ -620,7 +646,7 @@ var InfinitePool = /*#__PURE__*/function () {
620
646
  (0, _defineProperty2["default"])(this, "showState", function () {
621
647
  _index2.utiller.appendInfo(_this.getPoollerLogFormat("workerCount: ".concat(_this.maxWorker)));
622
648
 
623
- _index2.utiller.appendInfo(_this.getPoollerLogFormat("taskQueue(\u9084\u5728\u6392\u968A\u7684Task): ".concat(_this.getTaskQueueCount())));
649
+ _index2.utiller.appendInfo(_this.getPoollerLogFormat("taskQueue(\u9084\u5728\u6392\u968A\u7684Task): ".concat(_this.getCountOfAssignTaskInQueue())));
624
650
 
625
651
  _index2.utiller.appendInfo(_this.getPoollerLogFormat("QueueOfExecutingTask(\u6B63\u5728\u57F7\u884C\u7684AsyncTask, \u8D85\u904EworkerCount\u5C31\u662Fbug): ".concat(_lodash["default"].size(_this.queueOfExecutingTask))));
626
652
 
@@ -840,7 +866,7 @@ var InfinitePool = /*#__PURE__*/function () {
840
866
  infos[_key5 - 2] = arguments[_key5];
841
867
  }
842
868
 
843
- _index2.utiller.printLogMessage.apply(_index2.utiller, [this.getPoollerLogFormat(message), error].concat(infos));
869
+ if (SPECIFICITY_DEBUG) _index2.utiller.printLogMessage.apply(_index2.utiller, [this.getPoollerLogFormat(message), error].concat(infos));
844
870
  }
845
871
  /** return true if task completed, after 15 secs, force leave
846
872
  * TODO:應該要設計成當terminate後, 監聽executingTaskInQueue為零時,回傳結束 */
@@ -924,7 +950,7 @@ var InfinitePool = /*#__PURE__*/function () {
924
950
  }, _callee9, this);
925
951
  }));
926
952
 
927
- function addTaskAndWait4Result(_x4) {
953
+ function addTaskAndWait4Result(_x3) {
928
954
  return _addTaskAndWait4Result.apply(this, arguments);
929
955
  }
930
956
 
@@ -1034,7 +1060,7 @@ var InfinitePool = /*#__PURE__*/function () {
1034
1060
  }, {
1035
1061
  key: "isTaskQueueEmpty",
1036
1062
  value: function isTaskQueueEmpty() {
1037
- return this.getTaskQueueCount() === 0;
1063
+ return this.getCountOfAssignTaskInQueue() === 0;
1038
1064
  }
1039
1065
  /** 依照config 把委託任務放置到Queue裡面 */
1040
1066
 
@@ -1062,7 +1088,7 @@ var InfinitePool = /*#__PURE__*/function () {
1062
1088
  this.printLogMessage("4484121, \u8D70\u5230\u4E00\u5761\u7761\u89BA\u5340 enableOfTaskSleepByInterval:".concat(this.enableOfTaskSleepByInterval, " || ").concat(restInInterval, " ms"));
1063
1089
 
1064
1090
  case 6:
1065
- if (!(this.isRunning() && !this.isExecutingQueueFull())) {
1091
+ if (!this.rulesOfAppendToExecutingTask()) {
1066
1092
  _context10.next = 18;
1067
1093
  break;
1068
1094
  }
@@ -1074,17 +1100,18 @@ var InfinitePool = /*#__PURE__*/function () {
1074
1100
  break;
1075
1101
  }
1076
1102
 
1077
- promise = this.taskWrapper(taskInfo.task, taskInfo.hash);
1103
+ promise = this.taskWrapper(taskInfo.task, taskInfo.hash, this.queueOfWaitingParam.shift());
1078
1104
  this.removeTaskMapByHash(taskInfo.hash);
1079
1105
  this.appendTaskToExecuteQueue(taskInfo.hash, promise);
1080
- _context10.next = 15;
1106
+ _context10.next = 16;
1081
1107
  break;
1082
1108
 
1083
1109
  case 14:
1110
+ /** 沒有taskInfo, 也許有未知的isssue,保險起見break */
1111
+ this.printLogMessage("848451 \u4E5F\u8A31\u6709\u672A\u77E5\u7684isssue,\u4FDD\u96AA\u8D77\u898Bbreak,\u662F\u4E0D\u662F\u5728\u9019\u88E1\u7121\u9650\u8FF4\u5708\u8DD1\u8DD1\u8DD1", true);
1084
1112
  return _context10.abrupt("break", 18);
1085
1113
 
1086
- case 15:
1087
- this.printLogMessage("848451 \u662F\u4E0D\u662F\u5728\u9019\u88E1\u7121\u9650\u8FF4\u5708\u8DD1\u8DD1\u8DD1", true);
1114
+ case 16:
1088
1115
  _context10.next = 6;
1089
1116
  break;
1090
1117
 
@@ -1102,6 +1129,8 @@ var InfinitePool = /*#__PURE__*/function () {
1102
1129
 
1103
1130
  return syncTaskDispatcher;
1104
1131
  }()
1132
+ /** 把assignedTask 加入到 QueueOfExecutingTask 的規則*/
1133
+
1105
1134
  }, {
1106
1135
  key: "isWait4ResultTask",
1107
1136
  value:
@@ -1375,9 +1404,10 @@ var InfinitePool = /*#__PURE__*/function () {
1375
1404
  while (1) {
1376
1405
  switch (_context20.prev = _context20.next) {
1377
1406
  case 0:
1378
- pool = new InfinitePool(2);
1407
+ pool = new InfinitePool(5);
1379
1408
  pool.runByParamInBackground( /*#__PURE__*/function () {
1380
1409
  var _ref11 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee19(param) {
1410
+ var ms;
1381
1411
  return _regenerator["default"].wrap(function _callee19$(_context19) {
1382
1412
  while (1) {
1383
1413
  switch (_context19.prev = _context19.next) {
@@ -1386,9 +1416,10 @@ var InfinitePool = /*#__PURE__*/function () {
1386
1416
  return _index2.utiller.syncDelayRandom();
1387
1417
 
1388
1418
  case 2:
1389
- console.log("param", param);
1419
+ ms = _context19.sent;
1420
+ console.log("wait ".concat(ms, " ms"), "param", param);
1390
1421
 
1391
- case 3:
1422
+ case 4:
1392
1423
  case "end":
1393
1424
  return _context19.stop();
1394
1425
  }
@@ -1396,7 +1427,7 @@ var InfinitePool = /*#__PURE__*/function () {
1396
1427
  }, _callee19);
1397
1428
  }));
1398
1429
 
1399
- return function (_x5) {
1430
+ return function (_x4) {
1400
1431
  return _ref11.apply(this, arguments);
1401
1432
  };
1402
1433
  }(), "david", "susan", "golden", "weber", "kevin");
@@ -1439,9 +1470,10 @@ var InfinitePool = /*#__PURE__*/function () {
1439
1470
  while (1) {
1440
1471
  switch (_context22.prev = _context22.next) {
1441
1472
  case 0:
1442
- pool = new InfinitePool(1);
1473
+ pool = new InfinitePool(6);
1443
1474
  count = 0;
1444
1475
  pool.runByTimesInBackground( /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee21() {
1476
+ var ms;
1445
1477
  return _regenerator["default"].wrap(function _callee21$(_context21) {
1446
1478
  while (1) {
1447
1479
  switch (_context21.prev = _context21.next) {
@@ -1450,16 +1482,17 @@ var InfinitePool = /*#__PURE__*/function () {
1450
1482
  return _index2.utiller.syncDelayRandom();
1451
1483
 
1452
1484
  case 2:
1485
+ ms = _context21.sent;
1453
1486
  count++;
1454
- console.log("count:", count);
1487
+ console.log("wait for time: ".concat(ms, " ms, count:").concat(count));
1455
1488
 
1456
- case 4:
1489
+ case 5:
1457
1490
  case "end":
1458
1491
  return _context21.stop();
1459
1492
  }
1460
1493
  }
1461
1494
  }, _callee21);
1462
- })), 10);
1495
+ })), 20);
1463
1496
 
1464
1497
  case 3:
1465
1498
  if (!pool.isRunning()) {
@@ -1500,7 +1533,7 @@ var InfinitePool = /*#__PURE__*/function () {
1500
1533
  switch (_context24.prev = _context24.next) {
1501
1534
  case 0:
1502
1535
  oneToTen = _lodash["default"].range(1, 10);
1503
- pool = new InfinitePool(2);
1536
+ pool = new InfinitePool(5);
1504
1537
  _context24.next = 4;
1505
1538
  return pool.runByParams.apply(pool, [/*#__PURE__*/function () {
1506
1539
  var _ref13 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee23(param) {
@@ -1524,7 +1557,7 @@ var InfinitePool = /*#__PURE__*/function () {
1524
1557
  }, _callee23);
1525
1558
  }));
1526
1559
 
1527
- return function (_x6) {
1560
+ return function (_x5) {
1528
1561
  return _ref13.apply(this, arguments);
1529
1562
  };
1530
1563
  }()].concat((0, _toConsumableArray2["default"])(oneToTen)));
@@ -1555,7 +1588,7 @@ var InfinitePool = /*#__PURE__*/function () {
1555
1588
  pool = new InfinitePool(1);
1556
1589
  time = 0;
1557
1590
  _context26.next = 4;
1558
- return pool.runByTimes(20, /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee25() {
1591
+ return pool.runByTimes( /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee25() {
1559
1592
  return _regenerator["default"].wrap(function _callee25$(_context25) {
1560
1593
  while (1) {
1561
1594
  switch (_context25.prev = _context25.next) {
@@ -1573,7 +1606,7 @@ var InfinitePool = /*#__PURE__*/function () {
1573
1606
  }
1574
1607
  }
1575
1608
  }, _callee25);
1576
- })));
1609
+ })), 20);
1577
1610
 
1578
1611
  case 4:
1579
1612
  case "end":
@@ -1208,7 +1208,7 @@ var NodeUtiller = /*#__PURE__*/function (_Utiller) {
1208
1208
  _iterator8 = _createForOfIteratorHelper(packagejsons);
1209
1209
  _context11.prev = 7;
1210
1210
  _loop = /*#__PURE__*/_regenerator["default"].mark(function _loop() {
1211
- var path, tempFolderPath, release, pathOfPackageJson, indexFileName, templatePath, _yield$_this8$upgrade, name, version;
1211
+ var path, tempFolderPath, release, pathOfPackageJson, indexFileName, templatePath, _yield$_this8$upgrade, moduleName, version;
1212
1212
 
1213
1213
  return _regenerator["default"].wrap(function _loop$(_context10) {
1214
1214
  while (1) {
@@ -1264,10 +1264,10 @@ var NodeUtiller = /*#__PURE__*/function (_Utiller) {
1264
1264
 
1265
1265
  case 17:
1266
1266
  _yield$_this8$upgrade = _context10.sent;
1267
- name = _yield$_this8$upgrade.name;
1267
+ moduleName = _yield$_this8$upgrade.moduleName;
1268
1268
  version = _yield$_this8$upgrade.version;
1269
1269
  _context10.next = 22;
1270
- return _this8.updateVersionOfTemplate(name, version);
1270
+ return _this8.updateVersionOfTemplate(moduleName, version);
1271
1271
 
1272
1272
  case 22:
1273
1273
  /** 把package.json release放進去 */
@@ -1634,7 +1634,7 @@ var NodeUtiller = /*#__PURE__*/function (_Utiller) {
1634
1634
  case 8:
1635
1635
  return _context16.abrupt("return", {
1636
1636
  version: json.version,
1637
- name: json.name
1637
+ moduleName: json.name
1638
1638
  });
1639
1639
 
1640
1640
  case 11:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utiller",
3
- "version": "1.0.77",
3
+ "version": "1.0.78",
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.11",
14
- "utiller": "^1.0.76",
14
+ "utiller": "^1.0.77",
15
15
  "linepayer": "^1.0.4",
16
16
  "databazer": "^1.0.9",
17
17
  "lodash": "^4.17.20",