-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
62 lines (51 loc) · 1.04 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
const fastq = require('fastq');
const {callbackify} = require('util');
module.exports = (cursor, iteratee, {
concurrency = 100,
batch = false,
batchSize = 10
} = {}) => new Promise((resolve, reject) => {
const queue = fastq(callbackify(iteratee), concurrency);
const stream = cursor.stream();
let docs;
function done(err) {
if (err) {
stream.close();
queue.kill();
return reject(err);
}
stream.resume();
}
stream.on('error', (err) => {
queue.kill();
reject(err);
});
if (batch) {
cursor.batchSize(concurrency * batchSize);
docs = [];
} else {
cursor.batchSize(concurrency);
}
queue.saturated = () => {
stream.pause();
};
stream.on('data', (doc) => {
if (batch && docs.length < batchSize) {
return docs.push(doc);
}
queue.push(batch ? docs : doc, done);
if (batch && docs.length === batchSize) {
docs = [doc];
}
});
stream.on('end', () => {
if (batch && docs.length > 0) {
queue.push(docs, done);
}
if (queue.idle()) {
resolve();
} else {
queue.drain = resolve;
}
});
});