Skip to content

Commit 4c3561e

Browse files
committed
first commit
0 parents  commit 4c3561e

File tree

7 files changed

+220
-0
lines changed

7 files changed

+220
-0
lines changed

.editorconfig

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# editorconfig.org
2+
root = true
3+
4+
[*]
5+
end_of_line = lf
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true
9+
indent_style = space
10+
indent_size = 4

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
node_modules

.jscsrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"validateIndentation": 4
3+
}

.jshintrc

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
// Enforcing options
3+
"eqeqeq": false,
4+
"forin": true,
5+
"indent": 4,
6+
"noarg": true,
7+
"undef": true,
8+
"unused": true,
9+
"trailing": true,
10+
"evil": true,
11+
"laxcomma": true,
12+
13+
// Relaxing options
14+
"onevar": false,
15+
"asi": false,
16+
"eqnull": true,
17+
"expr": false,
18+
"loopfunc": true,
19+
"sub": true,
20+
"browser": true,
21+
"node": true,
22+
"globals": {
23+
"self": true,
24+
"define": true,
25+
"describe": true,
26+
"context": true,
27+
"it": true
28+
}
29+
}

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# async.util.queue
2+
3+
![Last version](https://img.shields.io/github/tag/async-js/queue.svg?style=flat-square)
4+
[![Build Status](http://img.shields.io/travis/async-js/queue/master.svg?style=flat-square)](https://travis-ci.org/async-js/queue)
5+
[![Dependency status](http://img.shields.io/david/async-js/queue.svg?style=flat-square)](https://david-dm.org/async-js/queue)
6+
[![Dev Dependencies Status](http://img.shields.io/david/dev/async-js/queue.svg?style=flat-square)](https://david-dm.org/async-js/queue#info=devDependencies)
7+
[![NPM Status](http://img.shields.io/npm/dm/queue.svg?style=flat-square)](https://www.npmjs.org/package/queue)
8+
[![Donate](https://img.shields.io/badge/donate-paypal-blue.svg?style=flat-square)](https://paypal.me/kikobeats)
9+
10+
> async queue helper method as module.
11+
12+
This module is used internally by [async](https://github.com/async-js/async).
13+
14+
## License
15+
16+
MIT © [async-js](https://github.com/async-js)

index.js

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
'use strict';
2+
3+
var map = require('async.util.map');
4+
var noop = require('async.util.noop');
5+
var isArray = require('async.util.isarray');
6+
var onlyOnce = require('async.util.onlyonce');
7+
var arrayEach = require('async.util.arrayeach');
8+
var setImmediate = require('async.util.setimmediate');
9+
10+
module.exports = function queue(worker, concurrency, payload) {
11+
if (concurrency == null)
12+
concurrency = 1;
13+
else if (concurrency === 0) {
14+
throw new Error('Concurrency must not be zero');
15+
}
16+
17+
function _insert(q, data, pos, callback) {
18+
if (callback != null && typeof callback !== "function") {
19+
throw new Error("task callback must be a function");
20+
}
21+
q.started = true;
22+
if (!isArray(data)) {
23+
data = [data];
24+
}
25+
if (data.length === 0 && q.idle()) {
26+
// call drain immediately if there are no tasks
27+
return setImmediate(function() {
28+
q.drain();
29+
});
30+
}
31+
arrayEach(data, function(task) {
32+
var item = {
33+
data: task,
34+
callback: callback || noop
35+
};
36+
37+
if (pos) {
38+
q.tasks.unshift(item);
39+
} else {
40+
q.tasks.push(item);
41+
}
42+
43+
if (q.tasks.length === q.concurrency) {
44+
q.saturated();
45+
}
46+
});
47+
setImmediate(q.process);
48+
}
49+
50+
function _next(q, tasks) {
51+
return function() {
52+
workers -= 1;
53+
54+
var removed = false;
55+
var args = arguments;
56+
arrayEach(tasks, function(task) {
57+
arrayEach(workersList, function(worker, index) {
58+
if (worker === task && !removed) {
59+
workersList.splice(index, 1);
60+
removed = true;
61+
}
62+
});
63+
64+
task.callback.apply(task, args);
65+
});
66+
if (q.tasks.length + workers === 0) {
67+
q.drain();
68+
}
69+
q.process();
70+
};
71+
}
72+
73+
var workers = 0;
74+
var workersList = [];
75+
var q = {
76+
tasks: [],
77+
concurrency: concurrency,
78+
payload: payload,
79+
saturated: noop,
80+
empty: noop,
81+
drain: noop,
82+
started: false,
83+
paused: false,
84+
push: function(data, callback) {
85+
_insert(q, data, false, callback);
86+
},
87+
kill: function() {
88+
q.drain = noop;
89+
q.tasks = [];
90+
},
91+
unshift: function(data, callback) {
92+
_insert(q, data, true, callback);
93+
},
94+
process: function() {
95+
while (!q.paused && workers < q.concurrency && q.tasks.length) {
96+
97+
var tasks = q.payload ?
98+
q.tasks.splice(0, q.payload) :
99+
q.tasks.splice(0, q.tasks.length);
100+
101+
var data = map(tasks, function(task) {
102+
return task.data;
103+
});
104+
105+
if (q.tasks.length === 0) {
106+
q.empty();
107+
}
108+
workers += 1;
109+
workersList.push(tasks[0]);
110+
var cb = onlyOnce(_next(q, tasks));
111+
worker(data, cb);
112+
}
113+
},
114+
length: function() {
115+
return q.tasks.length;
116+
},
117+
running: function() {
118+
return workers;
119+
},
120+
workersList: function() {
121+
return workersList;
122+
},
123+
idle: function() {
124+
return q.tasks.length + workers === 0;
125+
},
126+
pause: function() {
127+
q.paused = true;
128+
},
129+
resume: function() {
130+
if (q.paused === false) {
131+
return;
132+
}
133+
q.paused = false;
134+
var resumeCount = Math.min(q.concurrency, q.tasks.length);
135+
// Need to call q.process once per concurrent
136+
// worker to preserve full concurrency after pause
137+
for (var w = 1; w <= resumeCount; w++) {
138+
setImmediate(q.process);
139+
}
140+
}
141+
};
142+
return q;
143+
};

package.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "async.util.queue",
3+
"description": "async queuehelper method as module.",
4+
"main": "./index.js",
5+
"repository": "async-js/async.util",
6+
"author": {
7+
"email": "josefrancisco.verdu@gmail.com",
8+
"name": "Kiko Beats",
9+
"url": "https://github.com/Kikobeats"
10+
},
11+
"version": "0.3.0",
12+
"license": "MIT",
13+
"keywords": [
14+
"async.util",
15+
"queue"
16+
]
17+
}

0 commit comments

Comments
 (0)