utiller 1.0.23
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/index.js +21 -0
- package/lib/exceptioner/ERRORs.js +191 -0
- package/lib/exceptioner/index.js +89 -0
- package/lib/index.js +39 -0
- package/lib/pooller/index.js +1249 -0
- package/lib/utiller/index.js +1439 -0
- package/lib/utiller/nodeutiller.js +1745 -0
- package/package.json +33 -0
- package/template/sample.babel.config.js +18 -0
- package/template/sample.index.js +21 -0
- package/template/sample.package.json +29 -0
- package/template/sample.src.index.js +23 -0
- package/template/sample.warning.index.js +1 -0
|
@@ -0,0 +1,1249 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports["default"] = void 0;
|
|
9
|
+
|
|
10
|
+
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
|
11
|
+
|
|
12
|
+
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
|
|
13
|
+
|
|
14
|
+
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
|
15
|
+
|
|
16
|
+
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
|
17
|
+
|
|
18
|
+
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
|
19
|
+
|
|
20
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
21
|
+
|
|
22
|
+
var _classPrivateFieldGet2 = _interopRequireDefault(require("@babel/runtime/helpers/classPrivateFieldGet"));
|
|
23
|
+
|
|
24
|
+
var _lodash = _interopRequireDefault(require("lodash"));
|
|
25
|
+
|
|
26
|
+
var _index2 = require("../index.js");
|
|
27
|
+
|
|
28
|
+
var _configer = require("configer");
|
|
29
|
+
|
|
30
|
+
var _exceptioner = _interopRequireDefault(require("../exceptioner"));
|
|
31
|
+
|
|
32
|
+
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
|
|
33
|
+
|
|
34
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
35
|
+
|
|
36
|
+
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; }
|
|
37
|
+
|
|
38
|
+
function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); }
|
|
39
|
+
|
|
40
|
+
function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
|
|
41
|
+
|
|
42
|
+
var _run = /*#__PURE__*/new WeakMap();
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
*
|
|
46
|
+
Pooller 有以下特點:
|
|
47
|
+
1.task可以設定timeout
|
|
48
|
+
2.queue滿了的可以設定task interval
|
|
49
|
+
3.如果是runByEachTask length, queue裡面沒有task時,可以設定sleeptime, 以及Sleepcounts
|
|
50
|
+
4.task 可以cancelled by hash
|
|
51
|
+
5.runByParams,runByTimes,runInInfinite,runByEachTask
|
|
52
|
+
6.可以設定taskFailHandler, 這樣遇到錯誤就不會停掉poollers
|
|
53
|
+
*
|
|
54
|
+
*/
|
|
55
|
+
var InfinitePool = /*#__PURE__*/function () {
|
|
56
|
+
/** 用來處理Task的延遲,假設要偷網頁東西, 不能太頻繁, 要偽裝成手動只能透過這方式, 如果是multi thread, 延遲是針對worker滿載後,再加進去的那一個 */
|
|
57
|
+
|
|
58
|
+
/** 用來處理每一個task的timeout, 避免task處理太久卡在Queue裡面 */
|
|
59
|
+
|
|
60
|
+
/** 裡面放 {high:[], low:[], medium }*/
|
|
61
|
+
|
|
62
|
+
/** 裡面放準備要執行的Task, 這邊的task就沒辦法remove了 */
|
|
63
|
+
|
|
64
|
+
/** 目前queue機制是while(isQueuePolling) 沒任務就睡一下, 有任務就做事情, 發現task有延遲, 就要注意是不是taskInterval*/
|
|
65
|
+
|
|
66
|
+
/** 為了刪除未執行的task, 但只限於runByTask, 因為下一個run之後, hash就改變了 */
|
|
67
|
+
|
|
68
|
+
/** 為了刪除執行完的promise */
|
|
69
|
+
function InfinitePool() {
|
|
70
|
+
var _this = this;
|
|
71
|
+
|
|
72
|
+
var maxWorkers = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _configer.configer.POOLLER_WORKER_DEFAULT;
|
|
73
|
+
var name = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _index2.utiller.getRandomValue(0, 100000000000);
|
|
74
|
+
(0, _classCallCheck2["default"])(this, InfinitePool);
|
|
75
|
+
(0, _defineProperty2["default"])(this, "state", _configer.configer.POOLLER_STATE.RUN_BY_EACH_TASK);
|
|
76
|
+
(0, _defineProperty2["default"])(this, "enableOfTaskSleepByInterval", _configer.configer.POOLLER_ENABLE_TASK_SLEEP_BY_INTERVAL);
|
|
77
|
+
(0, _defineProperty2["default"])(this, "taskSleepInterval", _configer.configer.POOLLER_TASK_OF_INTERVAL_DEFAULT);
|
|
78
|
+
(0, _defineProperty2["default"])(this, "enableOfTaskTimeout", _configer.configer.POOLLER_ENABLE_TIMEOUT);
|
|
79
|
+
(0, _defineProperty2["default"])(this, "timeOfTaskTimeout", _configer.configer.POOLLER_TASK_TIMEOUT_DEFAULT);
|
|
80
|
+
(0, _defineProperty2["default"])(this, "maxWorker", void 0);
|
|
81
|
+
(0, _defineProperty2["default"])(this, "ignoreFirstRun", false);
|
|
82
|
+
(0, _defineProperty2["default"])(this, "paramQueue", []);
|
|
83
|
+
(0, _defineProperty2["default"])(this, "taskQueue", {});
|
|
84
|
+
(0, _defineProperty2["default"])(this, "executingTaskQueue", []);
|
|
85
|
+
(0, _defineProperty2["default"])(this, "isQueuePolling", false);
|
|
86
|
+
(0, _defineProperty2["default"])(this, "dispatchers", []);
|
|
87
|
+
(0, _defineProperty2["default"])(this, "initialTaskKickOff", false);
|
|
88
|
+
(0, _defineProperty2["default"])(this, "mHashNTaskMap", {});
|
|
89
|
+
(0, _defineProperty2["default"])(this, "mHashNPromiseMap", {});
|
|
90
|
+
(0, _defineProperty2["default"])(this, "hashCallbackMapOfWaiting4Result", {});
|
|
91
|
+
(0, _defineProperty2["default"])(this, "poolId", "");
|
|
92
|
+
(0, _defineProperty2["default"])(this, "setPoolId", function () {
|
|
93
|
+
var id = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _this.poolId;
|
|
94
|
+
_this.poolId = id;
|
|
95
|
+
});
|
|
96
|
+
(0, _defineProperty2["default"])(this, "getPoolId", function () {
|
|
97
|
+
return _this.poolId;
|
|
98
|
+
});
|
|
99
|
+
(0, _defineProperty2["default"])(this, "stopInBackground", /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
|
|
100
|
+
return _regenerator["default"].wrap(function _callee$(_context) {
|
|
101
|
+
while (1) {
|
|
102
|
+
switch (_context.prev = _context.next) {
|
|
103
|
+
case 0:
|
|
104
|
+
_this.isQueuePolling = false;
|
|
105
|
+
|
|
106
|
+
case 1:
|
|
107
|
+
if (!(_lodash["default"].size(_this.executingTaskQueue) > 0)) {
|
|
108
|
+
_context.next = 6;
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
_context.next = 4;
|
|
113
|
+
return _index2.utiller.syncDelay(1000);
|
|
114
|
+
|
|
115
|
+
case 4:
|
|
116
|
+
_context.next = 1;
|
|
117
|
+
break;
|
|
118
|
+
|
|
119
|
+
case 6:
|
|
120
|
+
return _context.abrupt("return", true);
|
|
121
|
+
|
|
122
|
+
case 7:
|
|
123
|
+
case "end":
|
|
124
|
+
return _context.stop();
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}, _callee);
|
|
128
|
+
})));
|
|
129
|
+
(0, _defineProperty2["default"])(this, "isRunning", function () {
|
|
130
|
+
return _this.isQueuePolling;
|
|
131
|
+
});
|
|
132
|
+
(0, _defineProperty2["default"])(this, "getTaskQueueCount", function () {
|
|
133
|
+
var size = 0;
|
|
134
|
+
if (_this.state === _configer.configer.POOLLER_STATE.RUN_BY_PARAMS) return _this.paramQueue.length;
|
|
135
|
+
|
|
136
|
+
var _iterator = _createForOfIteratorHelper(_configer.configer.POOLLER_PRIORITY),
|
|
137
|
+
_step;
|
|
138
|
+
|
|
139
|
+
try {
|
|
140
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
141
|
+
var prior = _step.value;
|
|
142
|
+
size += _this.taskQueue[prior].length;
|
|
143
|
+
}
|
|
144
|
+
} catch (err) {
|
|
145
|
+
_iterator.e(err);
|
|
146
|
+
} finally {
|
|
147
|
+
_iterator.f();
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return size;
|
|
151
|
+
});
|
|
152
|
+
(0, _defineProperty2["default"])(this, "add", function (task) {
|
|
153
|
+
var priority = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "low";
|
|
154
|
+
|
|
155
|
+
if (typeof task === "function") {
|
|
156
|
+
if (_configer.configer.POOLLER_PRIORITY.indexOf(priority) < 0) {
|
|
157
|
+
throw new _exceptioner["default"](4001, "priority can't be ".concat(priority));
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
var hash = _index2.utiller.getRandomHash();
|
|
161
|
+
|
|
162
|
+
var taskInfo = {
|
|
163
|
+
task: task,
|
|
164
|
+
hash: hash
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
_this.appendHashTaskMap(taskInfo);
|
|
168
|
+
|
|
169
|
+
_this.taskQueue[priority].push(taskInfo);
|
|
170
|
+
|
|
171
|
+
return hash;
|
|
172
|
+
} else {
|
|
173
|
+
throw new _exceptioner["default"](4002, "task can't be ".concat((0, _typeof2["default"])(task)));
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
(0, _defineProperty2["default"])(this, "taskWrapper", function (task, hash) {
|
|
177
|
+
return function () {
|
|
178
|
+
var self = _this;
|
|
179
|
+
var timeoutHash = "";
|
|
180
|
+
var taskResult;
|
|
181
|
+
var taskError;
|
|
182
|
+
var param = self.paramQueue.shift();
|
|
183
|
+
var taskResolved = true;
|
|
184
|
+
/** 用來判斷task 有沒有走到 catch裡面, 不然resolve了但return undefined, task會不知所措 */
|
|
185
|
+
|
|
186
|
+
return new Promise(function (resolve, reject) {
|
|
187
|
+
if (self.enableOfTaskTimeout) {
|
|
188
|
+
timeoutHash = setTimeout(function () {
|
|
189
|
+
try {
|
|
190
|
+
throw new _exceptioner["default"](4010, self.getPoollerLogFormat("TASK HASH:".concat(hash, " IS TIMEOUT ").concat(self.timeOfTaskTimeout, "} ms ").concat(param ? ",PARAMS IS ".concat(JSON.stringify(param)) : "")));
|
|
191
|
+
} catch (error) {
|
|
192
|
+
reject(error);
|
|
193
|
+
}
|
|
194
|
+
}, self.timeOfTaskTimeout);
|
|
195
|
+
}
|
|
196
|
+
/** 真正的任務是從這裡開始 */
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
console.log(self.getPoollerLogFormat("task executing"));
|
|
200
|
+
task(param).then(function (result) {
|
|
201
|
+
// console.log(self.getPoollerLogFormat('task get result'));
|
|
202
|
+
taskResult = result;
|
|
203
|
+
})["catch"](function (error) {
|
|
204
|
+
// console.error(self.getPoollerLogFormat('inner error'), error);
|
|
205
|
+
taskError = error;
|
|
206
|
+
taskResolved = false;
|
|
207
|
+
})["finally"](function () {
|
|
208
|
+
clearTimeout(timeoutHash);
|
|
209
|
+
/** 要記得 resolve, reject 在這裡代表的是放在executingQueue裡面的promise, reject,resolve其中一個必須觸發, executingQueue才不會堵塞
|
|
210
|
+
** 而任務需要的task是透過 removeResolveOrRejectPromiseByHash() */
|
|
211
|
+
// console.info(self.getPoollerLogFormat(self.getPoollerLogFormat('inner finally')));
|
|
212
|
+
|
|
213
|
+
resolve("".concat(hash, " done"));
|
|
214
|
+
});
|
|
215
|
+
})["catch"](function (error) {
|
|
216
|
+
// console.error(self.getPoollerLogFormat(self.getPoollerLogFormat('outer error')), error);
|
|
217
|
+
taskResolved = false;
|
|
218
|
+
taskError = error;
|
|
219
|
+
})["finally"](function () {
|
|
220
|
+
// console.info(self.getPoollerLogFormat(self.getPoollerLogFormat('outer finally')));
|
|
221
|
+
self.removeResolveOrRejectPromiseByHash(hash, {
|
|
222
|
+
taskResolved: taskResolved,
|
|
223
|
+
resolve: taskResult,
|
|
224
|
+
reject: taskError
|
|
225
|
+
});
|
|
226
|
+
});
|
|
227
|
+
};
|
|
228
|
+
});
|
|
229
|
+
(0, _defineProperty2["default"])(this, "adds", function (tasks) {
|
|
230
|
+
var priority = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "low";
|
|
231
|
+
var hashes = [];
|
|
232
|
+
|
|
233
|
+
if (_lodash["default"].isArray(tasks)) {
|
|
234
|
+
var _iterator2 = _createForOfIteratorHelper(tasks),
|
|
235
|
+
_step2;
|
|
236
|
+
|
|
237
|
+
try {
|
|
238
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
239
|
+
var task = _step2.value;
|
|
240
|
+
hashes.push(_this.add(task, priority));
|
|
241
|
+
}
|
|
242
|
+
} catch (err) {
|
|
243
|
+
_iterator2.e(err);
|
|
244
|
+
} finally {
|
|
245
|
+
_iterator2.f();
|
|
246
|
+
}
|
|
247
|
+
} else {
|
|
248
|
+
throw new _exceptioner["default"](4003, "should be async function array, not ".concat((0, _typeof2["default"])(tasks)));
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
return hashes;
|
|
252
|
+
});
|
|
253
|
+
(0, _defineProperty2["default"])(this, "removeTaskMapByHash", function (hash) {
|
|
254
|
+
delete _this.mHashNTaskMap[hash];
|
|
255
|
+
});
|
|
256
|
+
(0, _defineProperty2["default"])(this, "beforeRun", function () {
|
|
257
|
+
_this.isQueuePolling = true;
|
|
258
|
+
/** 有點多餘的設計, 本來是想要當沒有task時, 有個house-keeping的設計, 但發現只要在任務加入時, 觸發runByEachTask即可
|
|
259
|
+
this.currentSleepCounts = 0;
|
|
260
|
+
*/
|
|
261
|
+
});
|
|
262
|
+
(0, _defineProperty2["default"])(this, "afterRun", function () {
|
|
263
|
+
_this.clearCache();
|
|
264
|
+
});
|
|
265
|
+
(0, _defineProperty2["default"])(this, "runInInfinite", /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2() {
|
|
266
|
+
var task,
|
|
267
|
+
interval,
|
|
268
|
+
_args2 = arguments;
|
|
269
|
+
return _regenerator["default"].wrap(function _callee2$(_context2) {
|
|
270
|
+
while (1) {
|
|
271
|
+
switch (_context2.prev = _context2.next) {
|
|
272
|
+
case 0:
|
|
273
|
+
task = _args2.length > 0 && _args2[0] !== undefined ? _args2[0] : [];
|
|
274
|
+
interval = _args2.length > 1 ? _args2[1] : undefined;
|
|
275
|
+
|
|
276
|
+
_this.beforeRun();
|
|
277
|
+
|
|
278
|
+
if (!_lodash["default"].isFunction(task)) {
|
|
279
|
+
_context2.next = 7;
|
|
280
|
+
break;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
_this.add(task);
|
|
284
|
+
|
|
285
|
+
_context2.next = 12;
|
|
286
|
+
break;
|
|
287
|
+
|
|
288
|
+
case 7:
|
|
289
|
+
if (!_lodash["default"].isArray(task)) {
|
|
290
|
+
_context2.next = 11;
|
|
291
|
+
break;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
_this.adds(task);
|
|
295
|
+
|
|
296
|
+
_context2.next = 12;
|
|
297
|
+
break;
|
|
298
|
+
|
|
299
|
+
case 11:
|
|
300
|
+
throw new _exceptioner["default"](4006, "type of task is ===> ".concat((0, _typeof2["default"])(task)));
|
|
301
|
+
|
|
302
|
+
case 12:
|
|
303
|
+
_this.enableTaskSleepInterval(!!interval, interval);
|
|
304
|
+
|
|
305
|
+
_this.setState(_configer.configer.POOLLER_STATE.RUN_INFINITE);
|
|
306
|
+
|
|
307
|
+
case 14:
|
|
308
|
+
if (!_this.isRunning()) {
|
|
309
|
+
_context2.next = 19;
|
|
310
|
+
break;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
_context2.next = 17;
|
|
314
|
+
return (0, _classPrivateFieldGet2["default"])(_this, _run).call(_this);
|
|
315
|
+
|
|
316
|
+
case 17:
|
|
317
|
+
_context2.next = 14;
|
|
318
|
+
break;
|
|
319
|
+
|
|
320
|
+
case 19:
|
|
321
|
+
case "end":
|
|
322
|
+
return _context2.stop();
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}, _callee2);
|
|
326
|
+
})));
|
|
327
|
+
(0, _defineProperty2["default"])(this, "runByParams", /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3() {
|
|
328
|
+
var params,
|
|
329
|
+
task,
|
|
330
|
+
_iterator3,
|
|
331
|
+
_step3,
|
|
332
|
+
param,
|
|
333
|
+
_args3 = arguments;
|
|
334
|
+
|
|
335
|
+
return _regenerator["default"].wrap(function _callee3$(_context3) {
|
|
336
|
+
while (1) {
|
|
337
|
+
switch (_context3.prev = _context3.next) {
|
|
338
|
+
case 0:
|
|
339
|
+
params = _args3.length > 0 && _args3[0] !== undefined ? _args3[0] : [];
|
|
340
|
+
task = _args3.length > 1 && _args3[1] !== undefined ? _args3[1] : undefined;
|
|
341
|
+
|
|
342
|
+
if (_lodash["default"].isFunction(task)) {
|
|
343
|
+
_context3.next = 4;
|
|
344
|
+
break;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
throw new _exceptioner["default"](4006, "runByParams error, typeof task can't be ".concat((0, _typeof2["default"])(task)));
|
|
348
|
+
|
|
349
|
+
case 4:
|
|
350
|
+
if (_lodash["default"].isArray(params)) {
|
|
351
|
+
_context3.next = 6;
|
|
352
|
+
break;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
throw new _exceptioner["default"](4006, "runByParams error, typeof params can't be ".concat((0, _typeof2["default"])(params)));
|
|
356
|
+
|
|
357
|
+
case 6:
|
|
358
|
+
_iterator3 = _createForOfIteratorHelper(params);
|
|
359
|
+
|
|
360
|
+
try {
|
|
361
|
+
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
|
362
|
+
param = _step3.value;
|
|
363
|
+
|
|
364
|
+
_this.paramQueue.push(param);
|
|
365
|
+
}
|
|
366
|
+
} catch (err) {
|
|
367
|
+
_iterator3.e(err);
|
|
368
|
+
} finally {
|
|
369
|
+
_iterator3.f();
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
_this.add(task);
|
|
373
|
+
|
|
374
|
+
_this.setState(_configer.configer.POOLLER_STATE.RUN_BY_PARAMS);
|
|
375
|
+
|
|
376
|
+
_this.beforeRun();
|
|
377
|
+
|
|
378
|
+
case 11:
|
|
379
|
+
if (!(_this.isRunning() && !_lodash["default"].isEmpty(_this.paramQueue))) {
|
|
380
|
+
_context3.next = 16;
|
|
381
|
+
break;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
_context3.next = 14;
|
|
385
|
+
return (0, _classPrivateFieldGet2["default"])(_this, _run).call(_this);
|
|
386
|
+
|
|
387
|
+
case 14:
|
|
388
|
+
_context3.next = 11;
|
|
389
|
+
break;
|
|
390
|
+
|
|
391
|
+
case 16:
|
|
392
|
+
case "end":
|
|
393
|
+
return _context3.stop();
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
}, _callee3);
|
|
397
|
+
})));
|
|
398
|
+
(0, _defineProperty2["default"])(this, "runByEachTask", /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4() {
|
|
399
|
+
var tasks,
|
|
400
|
+
_args4 = arguments;
|
|
401
|
+
return _regenerator["default"].wrap(function _callee4$(_context4) {
|
|
402
|
+
while (1) {
|
|
403
|
+
switch (_context4.prev = _context4.next) {
|
|
404
|
+
case 0:
|
|
405
|
+
tasks = _args4.length > 0 && _args4[0] !== undefined ? _args4[0] : [];
|
|
406
|
+
_this.id = _index2.utiller.getRandomHash(3);
|
|
407
|
+
|
|
408
|
+
_this.beforeRun();
|
|
409
|
+
|
|
410
|
+
_this.adds(tasks);
|
|
411
|
+
|
|
412
|
+
_this.setState(_configer.configer.POOLLER_STATE.RUN_BY_EACH_TASK);
|
|
413
|
+
|
|
414
|
+
case 5:
|
|
415
|
+
if (!_this.isRunning()) {
|
|
416
|
+
_context4.next = 11;
|
|
417
|
+
break;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
if (_this.getTaskQueueCount() <= 0) {
|
|
421
|
+
_this.terminate();
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
_context4.next = 9;
|
|
425
|
+
return (0, _classPrivateFieldGet2["default"])(_this, _run).call(_this, _this.id);
|
|
426
|
+
|
|
427
|
+
case 9:
|
|
428
|
+
_context4.next = 5;
|
|
429
|
+
break;
|
|
430
|
+
|
|
431
|
+
case 11:
|
|
432
|
+
_this.terminate();
|
|
433
|
+
|
|
434
|
+
case 12:
|
|
435
|
+
case "end":
|
|
436
|
+
return _context4.stop();
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
}, _callee4);
|
|
440
|
+
})));
|
|
441
|
+
(0, _defineProperty2["default"])(this, "runByTimes", /*#__PURE__*/function () {
|
|
442
|
+
var _ref5 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5(times) {
|
|
443
|
+
var tasks,
|
|
444
|
+
index,
|
|
445
|
+
_args5 = arguments;
|
|
446
|
+
return _regenerator["default"].wrap(function _callee5$(_context5) {
|
|
447
|
+
while (1) {
|
|
448
|
+
switch (_context5.prev = _context5.next) {
|
|
449
|
+
case 0:
|
|
450
|
+
tasks = _args5.length > 1 && _args5[1] !== undefined ? _args5[1] : [];
|
|
451
|
+
|
|
452
|
+
_this.adds(tasks);
|
|
453
|
+
|
|
454
|
+
_this.beforeRun();
|
|
455
|
+
|
|
456
|
+
_this.setState(_configer.configer.POOLLER_STATE.RUN_BY_TIMES);
|
|
457
|
+
|
|
458
|
+
index = 0;
|
|
459
|
+
|
|
460
|
+
case 5:
|
|
461
|
+
if (!(index < times)) {
|
|
462
|
+
_context5.next = 11;
|
|
463
|
+
break;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
_context5.next = 8;
|
|
467
|
+
return (0, _classPrivateFieldGet2["default"])(_this, _run).call(_this);
|
|
468
|
+
|
|
469
|
+
case 8:
|
|
470
|
+
index++;
|
|
471
|
+
_context5.next = 5;
|
|
472
|
+
break;
|
|
473
|
+
|
|
474
|
+
case 11:
|
|
475
|
+
case "end":
|
|
476
|
+
return _context5.stop();
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
}, _callee5);
|
|
480
|
+
}));
|
|
481
|
+
|
|
482
|
+
return function (_x) {
|
|
483
|
+
return _ref5.apply(this, arguments);
|
|
484
|
+
};
|
|
485
|
+
}());
|
|
486
|
+
(0, _defineProperty2["default"])(this, "runInBackGround", function (_asyncfunc) {
|
|
487
|
+
for (var _len = arguments.length, params = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
488
|
+
params[_key - 1] = arguments[_key];
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
if (!(typeof _asyncfunc === "function")) {
|
|
492
|
+
throw new _exceptioner["default"](4002, "_asyncfunc can't be ".concat((0, _typeof2["default"])(_asyncfunc)));
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
return setTimeout( /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee6() {
|
|
496
|
+
return _regenerator["default"].wrap(function _callee6$(_context6) {
|
|
497
|
+
while (1) {
|
|
498
|
+
switch (_context6.prev = _context6.next) {
|
|
499
|
+
case 0:
|
|
500
|
+
_context6.prev = 0;
|
|
501
|
+
_context6.next = 3;
|
|
502
|
+
return _asyncfunc.apply(void 0, params);
|
|
503
|
+
|
|
504
|
+
case 3:
|
|
505
|
+
_context6.next = 8;
|
|
506
|
+
break;
|
|
507
|
+
|
|
508
|
+
case 5:
|
|
509
|
+
_context6.prev = 5;
|
|
510
|
+
_context6.t0 = _context6["catch"](0);
|
|
511
|
+
throw new _exceptioner["default"](4009, {
|
|
512
|
+
message: "".concat(_this.getPoollerLogFormat(""))
|
|
513
|
+
}, _context6.t0);
|
|
514
|
+
|
|
515
|
+
case 8:
|
|
516
|
+
_context6.prev = 8;
|
|
517
|
+
|
|
518
|
+
_this.terminate();
|
|
519
|
+
|
|
520
|
+
_index2.utiller.appendInfo("".concat(_this.getPoollerLogFormat("background gc after terminated"), " "));
|
|
521
|
+
|
|
522
|
+
return _context6.finish(8);
|
|
523
|
+
|
|
524
|
+
case 12:
|
|
525
|
+
case "end":
|
|
526
|
+
return _context6.stop();
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
}, _callee6, null, [[0, 5, 8, 12]]);
|
|
530
|
+
})), 1);
|
|
531
|
+
});
|
|
532
|
+
(0, _defineProperty2["default"])(this, "getPoollerLogFormat", function (msg) {
|
|
533
|
+
return "POOLLER ID: ".concat(_this.getPoolId()).concat(_lodash["default"].isEmpty(msg) ? "" : " , ").concat(msg);
|
|
534
|
+
});
|
|
535
|
+
(0, _defineProperty2["default"])(this, "setTaskFailHandler", function (listener) {
|
|
536
|
+
_this.taskFailHandler = listener;
|
|
537
|
+
});
|
|
538
|
+
(0, _defineProperty2["default"])(this, "touchTaskDispatcher", function () {
|
|
539
|
+
var dispatcher = setTimeout( /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee7() {
|
|
540
|
+
return _regenerator["default"].wrap(function _callee7$(_context7) {
|
|
541
|
+
while (1) {
|
|
542
|
+
switch (_context7.prev = _context7.next) {
|
|
543
|
+
case 0:
|
|
544
|
+
_context7.next = 2;
|
|
545
|
+
return _this.syncTaskDispatcher();
|
|
546
|
+
|
|
547
|
+
case 2:
|
|
548
|
+
_this.dispatchers.splice(_this.dispatchers.indexOf(dispatcher), 1);
|
|
549
|
+
|
|
550
|
+
case 3:
|
|
551
|
+
case "end":
|
|
552
|
+
return _context7.stop();
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
}, _callee7);
|
|
556
|
+
})), 0);
|
|
557
|
+
|
|
558
|
+
_this.dispatchers.push(dispatcher);
|
|
559
|
+
});
|
|
560
|
+
(0, _defineProperty2["default"])(this, "showState", function () {
|
|
561
|
+
_index2.utiller.appendInfo(_this.getPoollerLogFormat("workerCount: ".concat(_this.maxWorker)));
|
|
562
|
+
|
|
563
|
+
_index2.utiller.appendInfo(_this.getPoollerLogFormat("taskQueue: ".concat(_this.getTaskQueueCount())));
|
|
564
|
+
|
|
565
|
+
_index2.utiller.appendInfo(_this.getPoollerLogFormat("executingQueue: ".concat(_this.executingTaskQueue.length)));
|
|
566
|
+
|
|
567
|
+
_index2.utiller.appendInfo(_this.getPoollerLogFormat("mHashNTaskMap: ".concat(_lodash["default"].size(_this.mHashNTaskMap))));
|
|
568
|
+
|
|
569
|
+
_index2.utiller.appendInfo(_this.getPoollerLogFormat("mHashNPromiseMap: ".concat(_lodash["default"].size(_this.mHashNPromiseMap))));
|
|
570
|
+
});
|
|
571
|
+
|
|
572
|
+
_classPrivateFieldInitSpec(this, _run, {
|
|
573
|
+
writable: true,
|
|
574
|
+
value: function () {
|
|
575
|
+
var _value = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee8() {
|
|
576
|
+
return _regenerator["default"].wrap(function _callee8$(_context8) {
|
|
577
|
+
while (1) {
|
|
578
|
+
switch (_context8.prev = _context8.next) {
|
|
579
|
+
case 0:
|
|
580
|
+
_context8.next = 2;
|
|
581
|
+
return _this.syncTaskDispatcher();
|
|
582
|
+
|
|
583
|
+
case 2:
|
|
584
|
+
if (!(_this.executingTaskQueue.length >= _this.maxWorker)) {
|
|
585
|
+
_context8.next = 7;
|
|
586
|
+
break;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
_context8.next = 5;
|
|
590
|
+
return Promise.race(_this.executingTaskQueue.map(function (asyncTask) {
|
|
591
|
+
return asyncTask();
|
|
592
|
+
}));
|
|
593
|
+
|
|
594
|
+
case 5:
|
|
595
|
+
_context8.next = 10;
|
|
596
|
+
break;
|
|
597
|
+
|
|
598
|
+
case 7:
|
|
599
|
+
if (!(_this.getTaskQueueCount() === 0 && _this.executingTaskQueue.length > 0)) {
|
|
600
|
+
_context8.next = 10;
|
|
601
|
+
break;
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
_context8.next = 10;
|
|
605
|
+
return Promise.race(_this.executingTaskQueue.map(function (asyncTask) {
|
|
606
|
+
return asyncTask();
|
|
607
|
+
}));
|
|
608
|
+
|
|
609
|
+
case 10:
|
|
610
|
+
if (_this.executingTaskQueue.length > _this.maxWorker) {
|
|
611
|
+
_index2.utiller.appendError("\u4E00\u5B9A\u662F\u6539\u58DE\u4E86\uFF01\uFF01\uFF01\uFF01 ".concat(_this.getPoollerLogFormat("executing queue ".concat(_this.executingTaskQueue.length, " !!"))));
|
|
612
|
+
}
|
|
613
|
+
/** 只要完成run 就要把sleepTimeCount歸零
|
|
614
|
+
* this.currentSleepCounts = 0;
|
|
615
|
+
*/
|
|
616
|
+
|
|
617
|
+
|
|
618
|
+
case 11:
|
|
619
|
+
case "end":
|
|
620
|
+
return _context8.stop();
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
}, _callee8);
|
|
624
|
+
}));
|
|
625
|
+
|
|
626
|
+
function value() {
|
|
627
|
+
return _value.apply(this, arguments);
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
return value;
|
|
631
|
+
}()
|
|
632
|
+
});
|
|
633
|
+
|
|
634
|
+
(0, _defineProperty2["default"])(this, "getTaskInfoDependOnPriority", function () {
|
|
635
|
+
var _iterator4 = _createForOfIteratorHelper(_configer.configer.POOLLER_PRIORITY),
|
|
636
|
+
_step4;
|
|
637
|
+
|
|
638
|
+
try {
|
|
639
|
+
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
|
|
640
|
+
var prior = _step4.value;
|
|
641
|
+
|
|
642
|
+
if (_this.taskQueue[prior].length > 0) {
|
|
643
|
+
switch (_this.state) {
|
|
644
|
+
case _configer.configer.POOLLER_STATE.RUN_BY_EACH_TASK:
|
|
645
|
+
return _this.taskQueue[prior].shift();
|
|
646
|
+
|
|
647
|
+
case _configer.configer.POOLLER_STATE.RUN_BY_PARAMS:
|
|
648
|
+
case _configer.configer.POOLLER_STATE.RUN_BY_TIMES:
|
|
649
|
+
case _configer.configer.POOLLER_STATE.RUN_INFINITE:
|
|
650
|
+
var taskInfo = _this.taskQueue[prior].shift();
|
|
651
|
+
|
|
652
|
+
_this.add(taskInfo.task);
|
|
653
|
+
|
|
654
|
+
return taskInfo;
|
|
655
|
+
|
|
656
|
+
default:
|
|
657
|
+
throw new _exceptioner["default"](4005, "this.state ==> ".concat(_this.state));
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
} catch (err) {
|
|
662
|
+
_iterator4.e(err);
|
|
663
|
+
} finally {
|
|
664
|
+
_iterator4.f();
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
if (!_lodash["default"].isEqual(_this.state, _configer.configer.POOLLER_STATE.RUN_BY_EACH_TASK)) throw new _exceptioner["default"](4007);
|
|
668
|
+
});
|
|
669
|
+
(0, _defineProperty2["default"])(this, "removeResolveOrRejectPromiseByHash", function (hash, result) {
|
|
670
|
+
var callback = _this.hashCallbackMapOfWaiting4Result[hash];
|
|
671
|
+
|
|
672
|
+
if (callback !== undefined) {
|
|
673
|
+
callback(result);
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
delete _this.hashCallbackMapOfWaiting4Result[hash];
|
|
677
|
+
|
|
678
|
+
_this.removePromiseFromExecutingQueue(hash);
|
|
679
|
+
|
|
680
|
+
_this.removeCompletedPromiseFromMapByHash(hash);
|
|
681
|
+
});
|
|
682
|
+
(0, _defineProperty2["default"])(this, "removeCompletedPromiseFromMapByHash", function (hash) {
|
|
683
|
+
delete _this.mHashNPromiseMap[hash];
|
|
684
|
+
});
|
|
685
|
+
this.maxWorker = maxWorkers;
|
|
686
|
+
this.setPoolId(_lodash["default"].toString(name));
|
|
687
|
+
|
|
688
|
+
var _iterator5 = _createForOfIteratorHelper(_configer.configer.POOLLER_PRIORITY),
|
|
689
|
+
_step5;
|
|
690
|
+
|
|
691
|
+
try {
|
|
692
|
+
for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
|
|
693
|
+
var prior = _step5.value;
|
|
694
|
+
this.taskQueue[prior] = [];
|
|
695
|
+
}
|
|
696
|
+
} catch (err) {
|
|
697
|
+
_iterator5.e(err);
|
|
698
|
+
} finally {
|
|
699
|
+
_iterator5.f();
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
(0, _createClass2["default"])(InfinitePool, [{
|
|
704
|
+
key: "enableQueueTerminateBySleepCount",
|
|
705
|
+
value:
|
|
706
|
+
/**
|
|
707
|
+
* @deprecated there's no sleep mechanism
|
|
708
|
+
*/
|
|
709
|
+
function enableQueueTerminateBySleepCount() {
|
|
710
|
+
var enable = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
|
|
711
|
+
var interval = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _configer.configer.POOLLER_QUEUE_TIME_OF_SLEEP_INTERVAL_DEFAULT;
|
|
712
|
+
var times = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _configer.configer.POOLLER_MAX_SLEEP_COUNTS_DEFAULT;
|
|
713
|
+
this.enableOfQueueTerminateSleepCount = enable;
|
|
714
|
+
this.queueMaxSleepCounts = times;
|
|
715
|
+
this.intervalOfQueueSleep = interval;
|
|
716
|
+
}
|
|
717
|
+
}, {
|
|
718
|
+
key: "clearCache",
|
|
719
|
+
value: function clearCache() {
|
|
720
|
+
this.executingTaskQueue.length = 0;
|
|
721
|
+
this.mHashNTaskMap = {};
|
|
722
|
+
this.taskQueue = {};
|
|
723
|
+
}
|
|
724
|
+
}, {
|
|
725
|
+
key: "terminate",
|
|
726
|
+
value: function terminate() {
|
|
727
|
+
this.isQueuePolling = false;
|
|
728
|
+
}
|
|
729
|
+
/** return true if task completed, after 15 secs, force leave */
|
|
730
|
+
|
|
731
|
+
}, {
|
|
732
|
+
key: "setWorker",
|
|
733
|
+
value: function setWorker(counts) {
|
|
734
|
+
this.maxWorker = counts;
|
|
735
|
+
}
|
|
736
|
+
}, {
|
|
737
|
+
key: "cleanTaskInterval",
|
|
738
|
+
value: function cleanTaskInterval() {
|
|
739
|
+
this.taskSleepInterval = {
|
|
740
|
+
min: 0,
|
|
741
|
+
max: 0
|
|
742
|
+
};
|
|
743
|
+
}
|
|
744
|
+
/**
|
|
745
|
+
* interval:{min: 800, max: 1000}
|
|
746
|
+
* */
|
|
747
|
+
|
|
748
|
+
}, {
|
|
749
|
+
key: "enableTaskSleepInterval",
|
|
750
|
+
value: function enableTaskSleepInterval() {
|
|
751
|
+
var enable = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
|
|
752
|
+
var interval = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _configer.configer.POOLLER_TASK_OF_INTERVAL_DEFAULT;
|
|
753
|
+
this.enableOfTaskSleepByInterval = enable;
|
|
754
|
+
|
|
755
|
+
if (_lodash["default"].isNumber(interval)) {
|
|
756
|
+
interval = {
|
|
757
|
+
min: interval,
|
|
758
|
+
max: interval
|
|
759
|
+
};
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
this.taskSleepInterval = interval;
|
|
763
|
+
}
|
|
764
|
+
}, {
|
|
765
|
+
key: "enableTaskTimeout",
|
|
766
|
+
value: function enableTaskTimeout() {
|
|
767
|
+
var enable = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
|
|
768
|
+
var millionSec = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _configer.configer.POOLLER_TASK_TIMEOUT_DEFAULT;
|
|
769
|
+
this.enableOfTaskTimeout = enable;
|
|
770
|
+
this.timeOfTaskTimeout = millionSec;
|
|
771
|
+
}
|
|
772
|
+
}, {
|
|
773
|
+
key: "addTaskAndWait4Result",
|
|
774
|
+
value: function () {
|
|
775
|
+
var _addTaskAndWait4Result = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee9(asyncTask) {
|
|
776
|
+
var _this2 = this;
|
|
777
|
+
|
|
778
|
+
var priority,
|
|
779
|
+
taskName,
|
|
780
|
+
_args9 = arguments;
|
|
781
|
+
return _regenerator["default"].wrap(function _callee9$(_context9) {
|
|
782
|
+
while (1) {
|
|
783
|
+
switch (_context9.prev = _context9.next) {
|
|
784
|
+
case 0:
|
|
785
|
+
priority = _args9.length > 1 && _args9[1] !== undefined ? _args9[1] : "low";
|
|
786
|
+
taskName = _args9.length > 2 && _args9[2] !== undefined ? _args9[2] : "noName";
|
|
787
|
+
this.trigger();
|
|
788
|
+
return _context9.abrupt("return", new Promise(function (resolve, reject) {
|
|
789
|
+
var callback = function callback(result) {
|
|
790
|
+
if (result.taskResolved) {
|
|
791
|
+
resolve(result.resolve);
|
|
792
|
+
} else {
|
|
793
|
+
reject(result.reject);
|
|
794
|
+
}
|
|
795
|
+
};
|
|
796
|
+
|
|
797
|
+
var hash = _this2.add(asyncTask, priority);
|
|
798
|
+
|
|
799
|
+
_this2.registerHash4Result(hash, callback);
|
|
800
|
+
}));
|
|
801
|
+
|
|
802
|
+
case 4:
|
|
803
|
+
case "end":
|
|
804
|
+
return _context9.stop();
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
}, _callee9, this);
|
|
808
|
+
}));
|
|
809
|
+
|
|
810
|
+
function addTaskAndWait4Result(_x2) {
|
|
811
|
+
return _addTaskAndWait4Result.apply(this, arguments);
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
return addTaskAndWait4Result;
|
|
815
|
+
}()
|
|
816
|
+
}, {
|
|
817
|
+
key: "registerHash4Result",
|
|
818
|
+
value: function registerHash4Result(hash, callback) {
|
|
819
|
+
this.hashCallbackMapOfWaiting4Result[hash] = callback;
|
|
820
|
+
}
|
|
821
|
+
}, {
|
|
822
|
+
key: "appendHashTaskMap",
|
|
823
|
+
value: function appendHashTaskMap(taskInfo) {
|
|
824
|
+
this.mHashNTaskMap[taskInfo.hash] = taskInfo;
|
|
825
|
+
}
|
|
826
|
+
}, {
|
|
827
|
+
key: "getTaskInfoByHash",
|
|
828
|
+
value: function getTaskInfoByHash(hash) {
|
|
829
|
+
return this.mHashNTaskMap[hash];
|
|
830
|
+
}
|
|
831
|
+
/**
|
|
832
|
+
* remove task in queue by its hash, hash was created when add to queue
|
|
833
|
+
*
|
|
834
|
+
* method will return true when succeed delete
|
|
835
|
+
*
|
|
836
|
+
* 放到executing queue, 就沒辦法刪除了
|
|
837
|
+
*
|
|
838
|
+
**/
|
|
839
|
+
|
|
840
|
+
}, {
|
|
841
|
+
key: "remove",
|
|
842
|
+
value: function remove(hash) {
|
|
843
|
+
var taskInfo = this.getTaskInfoByHash(hash);
|
|
844
|
+
|
|
845
|
+
if (taskInfo) {
|
|
846
|
+
var _iterator6 = _createForOfIteratorHelper(_configer.configer.POOLLER_PRIORITY),
|
|
847
|
+
_step6;
|
|
848
|
+
|
|
849
|
+
try {
|
|
850
|
+
for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
|
|
851
|
+
var prior = _step6.value;
|
|
852
|
+
|
|
853
|
+
var _index = _lodash["default"].indexOf(this.taskQueue[prior], taskInfo);
|
|
854
|
+
|
|
855
|
+
if (_index > 0) {
|
|
856
|
+
this.taskQueue[prior].splice(_index, 1);
|
|
857
|
+
this.removeTaskMapByHash(hash);
|
|
858
|
+
return true;
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
} catch (err) {
|
|
862
|
+
_iterator6.e(err);
|
|
863
|
+
} finally {
|
|
864
|
+
_iterator6.f();
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
return false;
|
|
868
|
+
} else {
|
|
869
|
+
throw new _exceptioner["default"](4004, hash);
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
}, {
|
|
873
|
+
key: "runByEachTaskInBackGround",
|
|
874
|
+
value: function runByEachTaskInBackGround() {
|
|
875
|
+
if (this.atomicBgInstance !== undefined) ;
|
|
876
|
+
clearTimeout(this.atomicBgInstance);
|
|
877
|
+
this.atomicBgInstance = this.runInBackGround(this.runByEachTask);
|
|
878
|
+
return this;
|
|
879
|
+
}
|
|
880
|
+
}, {
|
|
881
|
+
key: "setState",
|
|
882
|
+
value:
|
|
883
|
+
/** run by how many task in queue, FIFO, if task completed, pool with intervalOfQueueSleep for a while
|
|
884
|
+
* ,after this.queueMaxSleepCounts, pooller would closed */
|
|
885
|
+
function setState(_state) {
|
|
886
|
+
this.state = _state;
|
|
887
|
+
}
|
|
888
|
+
}, {
|
|
889
|
+
key: "firstTaskDone",
|
|
890
|
+
value: function firstTaskDone() {
|
|
891
|
+
if (!this.initialTaskKickOff) {
|
|
892
|
+
this.initialTaskKickOff = true;
|
|
893
|
+
return false;
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
return this.initialTaskKickOff;
|
|
897
|
+
}
|
|
898
|
+
/** 如果設定interval, 第一個run不要執行的話,就設定true */
|
|
899
|
+
|
|
900
|
+
}, {
|
|
901
|
+
key: "setIgnoreFirstRun",
|
|
902
|
+
value: function setIgnoreFirstRun() {
|
|
903
|
+
this.ignoreFirstRun = true;
|
|
904
|
+
}
|
|
905
|
+
/** 加上一個 被動式啟動, 不然一直while() run, 可能有效能上的問題,現階端只支援RUN_BY_TASK */
|
|
906
|
+
|
|
907
|
+
}, {
|
|
908
|
+
key: "trigger",
|
|
909
|
+
value: function trigger() {
|
|
910
|
+
if (this.isQueuePolling) {
|
|
911
|
+
return;
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
if (this.state === _configer.configer.POOLLER_STATE.RUN_BY_EACH_TASK) {
|
|
915
|
+
/** 因為不這樣做, 就會產生 race condition, 會產生出3個runInGround instance */
|
|
916
|
+
this.runByEachTaskInBackGround();
|
|
917
|
+
return;
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
throw new _exceptioner["default"](4011, "this.state is ==> ".concat(_index2.utiller.getItsKeyByValue(_configer.configer.POOLLER_STATE, this.state)));
|
|
921
|
+
}
|
|
922
|
+
}, {
|
|
923
|
+
key: "isQueueFull",
|
|
924
|
+
value: function isQueueFull() {
|
|
925
|
+
return this.executingTaskQueue.length >= this.maxWorker - 1;
|
|
926
|
+
}
|
|
927
|
+
}, {
|
|
928
|
+
key: "syncTaskDispatcher",
|
|
929
|
+
value: function () {
|
|
930
|
+
var _syncTaskDispatcher = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee10() {
|
|
931
|
+
var initialTaskShouldNotRun, restInInterval, taskInfo, promise;
|
|
932
|
+
return _regenerator["default"].wrap(function _callee10$(_context10) {
|
|
933
|
+
while (1) {
|
|
934
|
+
switch (_context10.prev = _context10.next) {
|
|
935
|
+
case 0:
|
|
936
|
+
initialTaskShouldNotRun = this.ignoreFirstRun && !this.firstTaskDone();
|
|
937
|
+
|
|
938
|
+
if (!(initialTaskShouldNotRun || this.firstTaskDone() && this.isQueueFull() && this.enableOfTaskSleepByInterval)) {
|
|
939
|
+
_context10.next = 9;
|
|
940
|
+
break;
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
_context10.next = 4;
|
|
944
|
+
return _index2.utiller.syncDelayRandom(this.taskSleepInterval.min, this.taskSleepInterval.max);
|
|
945
|
+
|
|
946
|
+
case 4:
|
|
947
|
+
restInInterval = _context10.sent;
|
|
948
|
+
if (_configer.configer.MODULE_MSG.SHOW_SUCCEED) _index2.utiller.appendInfo("".concat(this.getPoollerLogFormat("Dispatcher \u7167\u898F\u77E9 \u7761 ".concat(restInInterval, " million-secs \u5F8C\u624D\u80FDDispatch Task")), " "));
|
|
949
|
+
|
|
950
|
+
if (this.isRunning()) {
|
|
951
|
+
_context10.next = 9;
|
|
952
|
+
break;
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
_index2.utiller.appendInfo("".concat(this.getPoollerLogFormat(" Dispatcher \u7761\u8D77\u4F86\u4E4B\u5F8C, \u9047\u5230this.isTaskRunning === true, \u6240\u4EE5\u5C31\u7D50\u675F\u9019\u500BDispatch")));
|
|
956
|
+
|
|
957
|
+
return _context10.abrupt("return");
|
|
958
|
+
|
|
959
|
+
case 9:
|
|
960
|
+
taskInfo = this.getTaskInfoDependOnPriority();
|
|
961
|
+
|
|
962
|
+
if (taskInfo) {
|
|
963
|
+
promise = this.taskWrapper(taskInfo.task, taskInfo.hash);
|
|
964
|
+
this.removeTaskMapByHash(taskInfo.hash);
|
|
965
|
+
this.appendHashPromiseMap(taskInfo.hash, promise);
|
|
966
|
+
this.appendToExecuteQueue(promise);
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
case 11:
|
|
970
|
+
case "end":
|
|
971
|
+
return _context10.stop();
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
}, _callee10, this);
|
|
975
|
+
}));
|
|
976
|
+
|
|
977
|
+
function syncTaskDispatcher() {
|
|
978
|
+
return _syncTaskDispatcher.apply(this, arguments);
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
return syncTaskDispatcher;
|
|
982
|
+
}()
|
|
983
|
+
}, {
|
|
984
|
+
key: "appendToExecuteQueue",
|
|
985
|
+
value: function appendToExecuteQueue(promise) {
|
|
986
|
+
this.executingTaskQueue.push(promise);
|
|
987
|
+
}
|
|
988
|
+
}, {
|
|
989
|
+
key: "removePromiseFromExecutingQueue",
|
|
990
|
+
value: function removePromiseFromExecutingQueue(hash) {
|
|
991
|
+
this.executingTaskQueue.splice(this.executingTaskQueue.indexOf(this.getPromiseByHash(hash)), 1);
|
|
992
|
+
}
|
|
993
|
+
}, {
|
|
994
|
+
key: "appendHashPromiseMap",
|
|
995
|
+
value: function appendHashPromiseMap(hash, promise) {
|
|
996
|
+
this.mHashNPromiseMap[hash] = promise;
|
|
997
|
+
}
|
|
998
|
+
}, {
|
|
999
|
+
key: "getPromiseByHash",
|
|
1000
|
+
value: function getPromiseByHash(hash) {
|
|
1001
|
+
return this.mHashNPromiseMap[hash];
|
|
1002
|
+
}
|
|
1003
|
+
/** 如果有function 就代表是一個需要回傳result的task, task在線等的意思*/
|
|
1004
|
+
|
|
1005
|
+
}, {
|
|
1006
|
+
key: "isWait4ResultTask",
|
|
1007
|
+
value: function isWait4ResultTask(hash) {
|
|
1008
|
+
return this.hashCallbackMapOfWaiting4Result[hash];
|
|
1009
|
+
}
|
|
1010
|
+
}, {
|
|
1011
|
+
key: "exampleOfRunInBackground",
|
|
1012
|
+
value: function () {
|
|
1013
|
+
var _exampleOfRunInBackground = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee13() {
|
|
1014
|
+
var pool, asyncTask, startTimoutTask;
|
|
1015
|
+
return _regenerator["default"].wrap(function _callee13$(_context13) {
|
|
1016
|
+
while (1) {
|
|
1017
|
+
switch (_context13.prev = _context13.next) {
|
|
1018
|
+
case 0:
|
|
1019
|
+
startTimoutTask = function _startTimoutTask(delayTime, taskSpendTime, sign) {
|
|
1020
|
+
var priority = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : "low";
|
|
1021
|
+
setTimeout( /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee12() {
|
|
1022
|
+
var printSign;
|
|
1023
|
+
return _regenerator["default"].wrap(function _callee12$(_context12) {
|
|
1024
|
+
while (1) {
|
|
1025
|
+
switch (_context12.prev = _context12.next) {
|
|
1026
|
+
case 0:
|
|
1027
|
+
_context12.prev = 0;
|
|
1028
|
+
_context12.next = 3;
|
|
1029
|
+
return pool.addTaskAndWait4Result(asyncTask(sign, taskSpendTime), priority);
|
|
1030
|
+
|
|
1031
|
+
case 3:
|
|
1032
|
+
printSign = _context12.sent;
|
|
1033
|
+
|
|
1034
|
+
_index2.utiller.appendInfo("answser => ", printSign);
|
|
1035
|
+
|
|
1036
|
+
_context12.next = 10;
|
|
1037
|
+
break;
|
|
1038
|
+
|
|
1039
|
+
case 7:
|
|
1040
|
+
_context12.prev = 7;
|
|
1041
|
+
_context12.t0 = _context12["catch"](0);
|
|
1042
|
+
|
|
1043
|
+
_index2.utiller.appendError(_context12.t0.message);
|
|
1044
|
+
|
|
1045
|
+
case 10:
|
|
1046
|
+
case "end":
|
|
1047
|
+
return _context12.stop();
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
}, _callee12, null, [[0, 7]]);
|
|
1051
|
+
})), delayTime);
|
|
1052
|
+
};
|
|
1053
|
+
|
|
1054
|
+
asyncTask = function _asyncTask(sign) {
|
|
1055
|
+
var taskSpend = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2000;
|
|
1056
|
+
return /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee11() {
|
|
1057
|
+
return _regenerator["default"].wrap(function _callee11$(_context11) {
|
|
1058
|
+
while (1) {
|
|
1059
|
+
switch (_context11.prev = _context11.next) {
|
|
1060
|
+
case 0:
|
|
1061
|
+
_context11.next = 2;
|
|
1062
|
+
return _index2.utiller.syncDelay(taskSpend);
|
|
1063
|
+
|
|
1064
|
+
case 2:
|
|
1065
|
+
return _context11.abrupt("return", sign);
|
|
1066
|
+
|
|
1067
|
+
case 3:
|
|
1068
|
+
case "end":
|
|
1069
|
+
return _context11.stop();
|
|
1070
|
+
}
|
|
1071
|
+
}
|
|
1072
|
+
}, _callee11);
|
|
1073
|
+
}));
|
|
1074
|
+
};
|
|
1075
|
+
|
|
1076
|
+
pool = new InfinitePool(1).runByEachTaskInBackGround();
|
|
1077
|
+
pool.enableTaskTimeout(true, 3000);
|
|
1078
|
+
startTimoutTask(0, 1000, "A", "low");
|
|
1079
|
+
startTimoutTask(35, 1000, "C", "high");
|
|
1080
|
+
startTimoutTask(500, 3000, "B", "low");
|
|
1081
|
+
startTimoutTask(3000, 1000, "D", "medium");
|
|
1082
|
+
|
|
1083
|
+
case 8:
|
|
1084
|
+
if (!true) {
|
|
1085
|
+
_context13.next = 14;
|
|
1086
|
+
break;
|
|
1087
|
+
}
|
|
1088
|
+
|
|
1089
|
+
_index2.utiller.appendInfo("system is running");
|
|
1090
|
+
|
|
1091
|
+
_context13.next = 12;
|
|
1092
|
+
return _index2.utiller.syncDelay(100000);
|
|
1093
|
+
|
|
1094
|
+
case 12:
|
|
1095
|
+
_context13.next = 8;
|
|
1096
|
+
break;
|
|
1097
|
+
|
|
1098
|
+
case 14:
|
|
1099
|
+
case "end":
|
|
1100
|
+
return _context13.stop();
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
}, _callee13);
|
|
1104
|
+
}));
|
|
1105
|
+
|
|
1106
|
+
function exampleOfRunInBackground() {
|
|
1107
|
+
return _exampleOfRunInBackground.apply(this, arguments);
|
|
1108
|
+
}
|
|
1109
|
+
|
|
1110
|
+
return exampleOfRunInBackground;
|
|
1111
|
+
}()
|
|
1112
|
+
}, {
|
|
1113
|
+
key: "exampleOfRunInfinte",
|
|
1114
|
+
value: function () {
|
|
1115
|
+
var _exampleOfRunInfinte = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee15() {
|
|
1116
|
+
return _regenerator["default"].wrap(function _callee15$(_context15) {
|
|
1117
|
+
while (1) {
|
|
1118
|
+
switch (_context15.prev = _context15.next) {
|
|
1119
|
+
case 0:
|
|
1120
|
+
_context15.next = 2;
|
|
1121
|
+
return new InfinitePool(2).runInInfinite( /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee14() {
|
|
1122
|
+
return _regenerator["default"].wrap(function _callee14$(_context14) {
|
|
1123
|
+
while (1) {
|
|
1124
|
+
switch (_context14.prev = _context14.next) {
|
|
1125
|
+
case 0:
|
|
1126
|
+
console.log("1000");
|
|
1127
|
+
|
|
1128
|
+
case 1:
|
|
1129
|
+
case "end":
|
|
1130
|
+
return _context14.stop();
|
|
1131
|
+
}
|
|
1132
|
+
}
|
|
1133
|
+
}, _callee14);
|
|
1134
|
+
})), 1000);
|
|
1135
|
+
|
|
1136
|
+
case 2:
|
|
1137
|
+
case "end":
|
|
1138
|
+
return _context15.stop();
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
}, _callee15);
|
|
1142
|
+
}));
|
|
1143
|
+
|
|
1144
|
+
function exampleOfRunInfinte() {
|
|
1145
|
+
return _exampleOfRunInfinte.apply(this, arguments);
|
|
1146
|
+
}
|
|
1147
|
+
|
|
1148
|
+
return exampleOfRunInfinte;
|
|
1149
|
+
}()
|
|
1150
|
+
}, {
|
|
1151
|
+
key: "exampleOfRunByParam",
|
|
1152
|
+
value: function () {
|
|
1153
|
+
var _exampleOfRunByParam = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee17() {
|
|
1154
|
+
var oneToTen, pool;
|
|
1155
|
+
return _regenerator["default"].wrap(function _callee17$(_context17) {
|
|
1156
|
+
while (1) {
|
|
1157
|
+
switch (_context17.prev = _context17.next) {
|
|
1158
|
+
case 0:
|
|
1159
|
+
oneToTen = _lodash["default"].range(1, 10);
|
|
1160
|
+
pool = new InfinitePool(1);
|
|
1161
|
+
pool.enableTaskSleepInterval(true, 1000);
|
|
1162
|
+
_context17.next = 5;
|
|
1163
|
+
return pool.runByParams(oneToTen, /*#__PURE__*/function () {
|
|
1164
|
+
var _ref11 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee16(param) {
|
|
1165
|
+
return _regenerator["default"].wrap(function _callee16$(_context16) {
|
|
1166
|
+
while (1) {
|
|
1167
|
+
switch (_context16.prev = _context16.next) {
|
|
1168
|
+
case 0:
|
|
1169
|
+
console.log(param);
|
|
1170
|
+
|
|
1171
|
+
case 1:
|
|
1172
|
+
case "end":
|
|
1173
|
+
return _context16.stop();
|
|
1174
|
+
}
|
|
1175
|
+
}
|
|
1176
|
+
}, _callee16);
|
|
1177
|
+
}));
|
|
1178
|
+
|
|
1179
|
+
return function (_x3) {
|
|
1180
|
+
return _ref11.apply(this, arguments);
|
|
1181
|
+
};
|
|
1182
|
+
}());
|
|
1183
|
+
|
|
1184
|
+
case 5:
|
|
1185
|
+
case "end":
|
|
1186
|
+
return _context17.stop();
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
}, _callee17);
|
|
1190
|
+
}));
|
|
1191
|
+
|
|
1192
|
+
function exampleOfRunByParam() {
|
|
1193
|
+
return _exampleOfRunByParam.apply(this, arguments);
|
|
1194
|
+
}
|
|
1195
|
+
|
|
1196
|
+
return exampleOfRunByParam;
|
|
1197
|
+
}()
|
|
1198
|
+
}, {
|
|
1199
|
+
key: "exampleOfRunByCount",
|
|
1200
|
+
value: function () {
|
|
1201
|
+
var _exampleOfRunByCount = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee19() {
|
|
1202
|
+
var pool, time;
|
|
1203
|
+
return _regenerator["default"].wrap(function _callee19$(_context19) {
|
|
1204
|
+
while (1) {
|
|
1205
|
+
switch (_context19.prev = _context19.next) {
|
|
1206
|
+
case 0:
|
|
1207
|
+
pool = new InfinitePool(1);
|
|
1208
|
+
time = 0;
|
|
1209
|
+
_context19.next = 4;
|
|
1210
|
+
return pool.runByTimes(20, [/*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee18() {
|
|
1211
|
+
return _regenerator["default"].wrap(function _callee18$(_context18) {
|
|
1212
|
+
while (1) {
|
|
1213
|
+
switch (_context18.prev = _context18.next) {
|
|
1214
|
+
case 0:
|
|
1215
|
+
_context18.next = 2;
|
|
1216
|
+
return _index2.utiller.syncDelay(1000);
|
|
1217
|
+
|
|
1218
|
+
case 2:
|
|
1219
|
+
time++;
|
|
1220
|
+
console.log("execute the ".concat(time, " time"));
|
|
1221
|
+
|
|
1222
|
+
case 4:
|
|
1223
|
+
case "end":
|
|
1224
|
+
return _context18.stop();
|
|
1225
|
+
}
|
|
1226
|
+
}
|
|
1227
|
+
}, _callee18);
|
|
1228
|
+
}))]);
|
|
1229
|
+
|
|
1230
|
+
case 4:
|
|
1231
|
+
case "end":
|
|
1232
|
+
return _context19.stop();
|
|
1233
|
+
}
|
|
1234
|
+
}
|
|
1235
|
+
}, _callee19);
|
|
1236
|
+
}));
|
|
1237
|
+
|
|
1238
|
+
function exampleOfRunByCount() {
|
|
1239
|
+
return _exampleOfRunByCount.apply(this, arguments);
|
|
1240
|
+
}
|
|
1241
|
+
|
|
1242
|
+
return exampleOfRunByCount;
|
|
1243
|
+
}()
|
|
1244
|
+
}]);
|
|
1245
|
+
return InfinitePool;
|
|
1246
|
+
}();
|
|
1247
|
+
|
|
1248
|
+
var _default = InfinitePool;
|
|
1249
|
+
exports["default"] = _default;
|