-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (73 loc) · 1.85 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
class Scheduler {
/**
* 生成一个异步调度器
* @param {number} limit default:4
*/
constructor(limit = 4) {
this.limit = limit;
this.awaitArr = [];
this.count = 0;
}
/**
* 添加函数到待执行列表
* @param {function} handler 可以是异步|同步函数
* @param {array} args 函数的参数(可选)
* @param {function} callback 回调函数(可选)
* @returns 函数执行结果
*/
async add(handler, args, callback) {
if (typeof handler !== 'function') {
throw "handler is not a function";
}
if (callback && typeof callback !== 'function') {
throw "callback is not a function";
}
if (this.count >= this.limit) {
await new Promise(resolve => {
this.awaitArr.push(resolve);
});
}
this.count++;
let res;
try {
res = await handler.apply(null, args);
callback && callback(null, res);
} catch (error) {
callback && callback(error);
}
this.count--;
if (this.awaitArr.length) {
this.awaitArr.shift()();
}
return res;
}
/**
* 添加函数到待执行列表
* @param {array} handlers
* @param {function} callback
* @returns handlers包含的函数执行结果
*/
async adds(handlers, callback) {
if (!Array.isArray(handlers)) {
throw "handlers is not a Array";
}
if (callback && typeof callback !== 'function') {
throw "callback is not a function";
}
let handlersArr = handlers.map((val) => {
if (typeof val !== 'function') {
return this.add(val.handler, val.args, val.callback);
}
return this.add(val);
});
let res;
try {
res = await Promise.all(handlersArr);
callback && callback(null, res);
} catch (error) {
callback && callback(error)
}
return res;
}
}
module.exports = Scheduler;