util-helpers 5.7.8 → 5.8.0

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.
@@ -0,0 +1,54 @@
1
+ var ConcurrencyController = (function () {
2
+ function ConcurrencyController(maxConcurrency) {
3
+ if (maxConcurrency === void 0) { maxConcurrency = 2; }
4
+ this.maxConcurrency = Math.max(maxConcurrency, 1);
5
+ this.runningCount = 0;
6
+ this.queue = [];
7
+ this.isPaused = false;
8
+ }
9
+ ConcurrencyController.prototype.add = function (task) {
10
+ var _this = this;
11
+ return new Promise(function (resolve, reject) {
12
+ _this.queue.push({ task: task, resolve: resolve, reject: reject });
13
+ _this._run();
14
+ });
15
+ };
16
+ ConcurrencyController.prototype._run = function () {
17
+ var _this = this;
18
+ if (this.isPaused)
19
+ return;
20
+ while (this.runningCount < this.maxConcurrency && this.queue.length > 0) {
21
+ var _a = this.queue.shift(), task = _a.task, resolve = _a.resolve, reject = _a.reject;
22
+ this.runningCount++;
23
+ task()
24
+ .then(resolve)
25
+ .catch(reject)
26
+ .finally(function () {
27
+ _this.runningCount--;
28
+ _this._run();
29
+ });
30
+ }
31
+ };
32
+ ConcurrencyController.prototype.getStatus = function () {
33
+ return {
34
+ running: this.runningCount,
35
+ waiting: this.queue.length,
36
+ maxConcurrency: this.maxConcurrency,
37
+ paused: this.isPaused
38
+ };
39
+ };
40
+ ConcurrencyController.prototype.setMaxConcurrency = function (maxConcurrency) {
41
+ this.maxConcurrency = Math.max(maxConcurrency, 1);
42
+ this._run();
43
+ };
44
+ ConcurrencyController.prototype.pause = function () {
45
+ this.isPaused = true;
46
+ };
47
+ ConcurrencyController.prototype.resume = function () {
48
+ this.isPaused = false;
49
+ this._run();
50
+ };
51
+ return ConcurrencyController;
52
+ }());
53
+
54
+ export { ConcurrencyController as default };
package/esm/VERSION.js CHANGED
@@ -1,3 +1,3 @@
1
- var VERSION = "5.7.8";
1
+ var VERSION = "5.8.0";
2
2
 
3
3
  export { VERSION as default };
package/esm/index.js CHANGED
@@ -68,3 +68,4 @@ export { default as findTreeSelect } from './findTreeSelect.js';
68
68
  export { setDisableWarning } from './utils/config.js';
69
69
  export { default as VERSION } from './VERSION.js';
70
70
  export { default as AsyncMemo } from './AsyncMemo.js';
71
+ export { default as ConcurrencyController } from './ConcurrencyController.js';
@@ -0,0 +1,56 @@
1
+ 'use strict';
2
+
3
+ var ConcurrencyController = (function () {
4
+ function ConcurrencyController(maxConcurrency) {
5
+ if (maxConcurrency === void 0) { maxConcurrency = 2; }
6
+ this.maxConcurrency = Math.max(maxConcurrency, 1);
7
+ this.runningCount = 0;
8
+ this.queue = [];
9
+ this.isPaused = false;
10
+ }
11
+ ConcurrencyController.prototype.add = function (task) {
12
+ var _this = this;
13
+ return new Promise(function (resolve, reject) {
14
+ _this.queue.push({ task: task, resolve: resolve, reject: reject });
15
+ _this._run();
16
+ });
17
+ };
18
+ ConcurrencyController.prototype._run = function () {
19
+ var _this = this;
20
+ if (this.isPaused)
21
+ return;
22
+ while (this.runningCount < this.maxConcurrency && this.queue.length > 0) {
23
+ var _a = this.queue.shift(), task = _a.task, resolve = _a.resolve, reject = _a.reject;
24
+ this.runningCount++;
25
+ task()
26
+ .then(resolve)
27
+ .catch(reject)
28
+ .finally(function () {
29
+ _this.runningCount--;
30
+ _this._run();
31
+ });
32
+ }
33
+ };
34
+ ConcurrencyController.prototype.getStatus = function () {
35
+ return {
36
+ running: this.runningCount,
37
+ waiting: this.queue.length,
38
+ maxConcurrency: this.maxConcurrency,
39
+ paused: this.isPaused
40
+ };
41
+ };
42
+ ConcurrencyController.prototype.setMaxConcurrency = function (maxConcurrency) {
43
+ this.maxConcurrency = Math.max(maxConcurrency, 1);
44
+ this._run();
45
+ };
46
+ ConcurrencyController.prototype.pause = function () {
47
+ this.isPaused = true;
48
+ };
49
+ ConcurrencyController.prototype.resume = function () {
50
+ this.isPaused = false;
51
+ this._run();
52
+ };
53
+ return ConcurrencyController;
54
+ }());
55
+
56
+ module.exports = ConcurrencyController;
package/lib/VERSION.js CHANGED
@@ -1,5 +1,5 @@
1
1
  'use strict';
2
2
 
3
- var VERSION = "5.7.8";
3
+ var VERSION = "5.8.0";
4
4
 
5
5
  module.exports = VERSION;
package/lib/index.js CHANGED
@@ -70,6 +70,7 @@ var findTreeSelect = require('./findTreeSelect.js');
70
70
  var config = require('./utils/config.js');
71
71
  var VERSION = require('./VERSION.js');
72
72
  var AsyncMemo = require('./AsyncMemo.js');
73
+ var ConcurrencyController = require('./ConcurrencyController.js');
73
74
 
74
75
 
75
76
 
@@ -143,3 +144,4 @@ exports.findTreeSelect = findTreeSelect;
143
144
  exports.setDisableWarning = config.setDisableWarning;
144
145
  exports.VERSION = VERSION;
145
146
  exports.AsyncMemo = AsyncMemo;
147
+ exports.ConcurrencyController = ConcurrencyController;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "util-helpers",
3
- "version": "5.7.8",
3
+ "version": "5.8.0",
4
4
  "description": "一个基于业务场景的工具方法库",
5
5
  "main": "lib/index.js",
6
6
  "module": "esm/index.js",
@@ -3,8 +3,25 @@ type AsyncFunc<T = any> = () => Promise<T>;
3
3
  * 并发控制器
4
4
  *
5
5
  * @class
6
- * @param {number} [maxConcurrency=2] 最大并发数量。
6
+ * @param {number} [maxConcurrency=2] 最大并发数量。默认 `2`。
7
7
  * @example
8
+ * // 最多同时运行 3 个异步任务
9
+ * const controller = new ConcurrencyController(3);
10
+ *
11
+ * const urls = ['/api/1', '/api/2', '/api/3', '/api/4', '/api/5', '/api/6'];
12
+ * const results = await Promise.all(urls.map(url => controller.add(() => fetch(url))));
13
+ *
14
+ * // 获取状态
15
+ * controller.getStatus();
16
+ *
17
+ * // 暂停队列
18
+ * controller.pause();
19
+ *
20
+ * // 恢复队列
21
+ * controller.resume();
22
+ *
23
+ * // 设置最大并发数量,并立即运行队列中的任务。
24
+ * controller.setMaxConcurrency(4);
8
25
  *
9
26
  */
10
27
  declare class ConcurrencyController {
@@ -13,15 +30,41 @@ declare class ConcurrencyController {
13
30
  private queue;
14
31
  private isPaused;
15
32
  constructor(maxConcurrency?: number);
33
+ /**
34
+ * 加入队列
35
+ *
36
+ * @param {Function} task 异步任务函数。
37
+ * @returns 任务的 Promise 对象。
38
+ */
16
39
  add<T>(task: AsyncFunc<T>): Promise<T>;
17
- private run;
40
+ /**
41
+ * 运行队列中的任务
42
+ *
43
+ * @private
44
+ */
45
+ private _run;
46
+ /**
47
+ * 获取状态
48
+ */
18
49
  getStatus(): {
19
50
  running: number;
20
51
  waiting: number;
21
52
  maxConcurrency: number;
53
+ paused: boolean;
22
54
  };
55
+ /**
56
+ * 设置最大并发数量,并立即运行队列中的任务。
57
+ *
58
+ * @param {number} maxConcurrency 最大并发数量。不能小于 `1`。
59
+ */
23
60
  setMaxConcurrency(maxConcurrency: number): void;
61
+ /**
62
+ * 暂停队列
63
+ */
24
64
  pause(): void;
65
+ /**
66
+ * 恢复队列
67
+ */
25
68
  resume(): void;
26
69
  }
27
70
  export default ConcurrencyController;
package/types/index.d.ts CHANGED
@@ -141,3 +141,4 @@ export { setDisableWarning } from './utils/config';
141
141
  import VERSION from './VERSION';
142
142
  export { VERSION };
143
143
  export { default as AsyncMemo } from './AsyncMemo';
144
+ export { default as ConcurrencyController } from './ConcurrencyController';