hunter-open-sdk 2.0.0-beta.19 → 2.0.0-beta.20

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.
@@ -10,39 +10,55 @@ var _utils = require("../../utils/utils.common");
10
10
  var _utils2 = require("../../utils/utils.path");
11
11
  var _utils3 = require("../../utils/utils.ability");
12
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
- class GetGeneralReport {
14
- constructor() {
15
- this.initWorker();
16
- this.initWorkerListener();
17
- }
18
- worker = null;
19
- callback = null;
20
- exec(payload, callback) {
21
- this.callback = typeof callback === 'function' ? callback : _utils3.defaultCallback;
22
- this.worker && this.worker.postMessage({
23
- payload
24
- });
13
+ const DEFAULT_POOL_SIZE = 8;
14
+ class WorkerPool {
15
+ constructor(size) {
16
+ this.poolSize = size;
17
+ this.idleWorkers = [];
18
+ this.taskQueue = [];
19
+ for (let i = 0; i < this.poolSize; i++) {
20
+ this.idleWorkers.push(this._createWorker());
21
+ }
25
22
  }
26
- initWorkerListener() {
27
- this.worker && this.worker.addEventListener('message', event => {
28
- const {
29
- payload
30
- } = event.data;
31
- this.callback(payload);
32
- this.callback = null;
23
+ _createWorker() {
24
+ const workerPath = new _url.default.URL('file://' + (0, _utils2.getCurrentEnvPath)(__dirname, './worker.js'));
25
+ return new _webWorker.default(workerPath, {
26
+ type: 'module'
33
27
  });
34
28
  }
35
- initWorker() {
36
- if (!this.worker) {
37
- const workerPath = new _url.default.URL('file://' + (0, _utils2.getCurrentEnvPath)(__dirname, './worker.js'));
38
- this.worker = new _webWorker.default(workerPath, {
39
- type: 'module'
29
+ exec(payload, callback) {
30
+ if (this.idleWorkers.length > 0) {
31
+ this._run(this.idleWorkers.pop(), payload, callback);
32
+ } else {
33
+ this.taskQueue.push({
34
+ payload,
35
+ callback
40
36
  });
41
37
  }
42
38
  }
39
+ _run(worker, payload, callback) {
40
+ const onMessage = event => {
41
+ worker.removeEventListener('message', onMessage);
42
+ callback(event.data.payload);
43
+ if (this.taskQueue.length > 0) {
44
+ const next = this.taskQueue.shift();
45
+ this._run(worker, next.payload, next.callback);
46
+ } else {
47
+ this.idleWorkers.push(worker);
48
+ }
49
+ };
50
+ worker.addEventListener('message', onMessage);
51
+ worker.postMessage({
52
+ payload
53
+ });
54
+ }
43
55
  }
44
- const entity = new GetGeneralReport();
45
- function getGeneralReport(params, callback) {
56
+ let pool = new WorkerPool(DEFAULT_POOL_SIZE);
57
+ function getGeneralReport(params, callback, options = {}) {
58
+ const cb = typeof callback === 'function' ? callback : _utils3.defaultCallback;
46
59
  const payload = (0, _utils.parsePayload)(params);
47
- entity.exec(payload, callback);
60
+ if (options?.poolSize != null && options.poolSize !== pool.poolSize) {
61
+ pool = new WorkerPool(options.poolSize);
62
+ }
63
+ pool.exec(payload, cb);
48
64
  }
@@ -1 +1 @@
1
- 0.4.0
1
+ 0.4.1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hunter-open-sdk",
3
- "version": "2.0.0-beta.19",
3
+ "version": "2.0.0-beta.20",
4
4
  "description": "采货侠SaaS版本桌面端sdk",
5
5
  "scripts": {
6
6
  "start": "zz dev",