-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
59 lines (52 loc) · 1.4 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
;
'use strict';
(function WorkQueueModuleSpace() {
var _innerNextTick;
if ('function' === typeof setImmediate) {
_innerNextTick = setImmediate;
} else {
_innerNextTick = function (cb) {
setTimtoue(cb, 0);
}
}
function WorkQueue (opt) {
var self = this;
opt = opt || {};
if (!!opt.workMax) self.setWorkMax(opt.workMax);
self._queueArray = [];
self._queueNowRun = 0;
};
WorkQueue.create = function (opt) {
return new WorkQueue(opt);
};
WorkQueue.prototype.setQueueMax = WorkQueue.prototype.setWorkMax = function (maxnum) {
this._workMax = parseInt(maxnum) || 1000;
};
WorkQueue.prototype.queue = function (workFn) {
var self = this;
if ('function' == typeof workFn) {
self._queueArray.push(workFn);
self._doQueue();
}
};
WorkQueue.prototype._doQueue = function () {
var self = this;
_innerNextTick(function () {
if (self._queueNowRun < self._workMax) {
self._queueNowRun++;
var workFn = self._queueArray.pop();
if (!!workFn) {
workFn(function () {
self._queueNowRun--;
self._doQueue();
});
} else {
self._queueNowRun--;
}
}
});
};
if ('object' === typeof module) module.exports = WorkQueue;
if ('object' === typeof window) window.Varbox = WorkQueue;
if ('object' === typeof self) self.Varbox = WorkQueue;
})();