-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyncQueue.js
More file actions
45 lines (42 loc) · 1.05 KB
/
SyncQueue.js
File metadata and controls
45 lines (42 loc) · 1.05 KB
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
/**
* SyncQueue.js 0.1.0 Copyright (c) 2012 Kenneth Powers
* Released under the MIT License
*/
(function (name, global, definition) {
if (typeof module !== 'undefined') {
module.exports = definition();
} else if (typeof define !== 'undefined' && typeof define.amd === 'object') {
define(definition);
} else {
global[name] = definition();
}
})('SyncQueue', this, function () {
// SyncQueue constructor
var SyncQueue = function () {
if (!(this instanceof SyncQueue)) {
return new SyncQueue();
}
this._queue = [];
this._running = false;
};
// Function which adds a new job to the queue
SyncQueue.prototype.push = function (job) {
this._queue.push(job);
if (!this._running) {
this._running = true;
runNext(this);
}
};
// Function which runs the next job in the queue
function runNext(sq) {
if (sq._queue.length > 0) {
sq._queue.shift()(function () {
runNext(sq);
});
} else {
sq._running = false;
}
}
// Return constructor
return SyncQueue;
});