|
| 1 | +'use strict'; |
| 2 | + |
1 | 3 | const { EventEmitter } = require('events'); |
2 | 4 | const globStream = require('glob-stream'); |
3 | 5 | const workerFarm = require('worker-farm'); |
4 | 6 |
|
5 | 7 | class FileProcessor extends EventEmitter { |
6 | | - constructor(globPattern, worker, options) { |
7 | | - super(); |
8 | | - options = options || {}; |
9 | | - const glob = (this.glob = globStream(globPattern)); |
10 | | - const workers = (this.workers = workerFarm(options.worker || {}, worker)); |
11 | | - |
12 | | - let allQueued = false; |
13 | | - let errorHappened = false; |
14 | | - let queuedCount = 0; |
15 | | - let processedCount = 0; |
16 | | - |
17 | | - const checkForEnd = () => { |
18 | | - if (errorHappened || (allQueued && queuedCount === processedCount)) { |
19 | | - if (!options.keepAlive) { |
20 | | - workerFarm.end(workers); |
21 | | - } |
22 | | - if (!errorHappened) this.emit('end'); |
23 | | - } |
24 | | - }; |
25 | | - |
26 | | - glob.on('data', ({ path }) => { |
27 | | - queuedCount++; |
28 | | - this.emit('queued', path); |
29 | | - this.process(path, (err, result) => { |
30 | | - processedCount++; |
31 | | - if (err) { |
32 | | - errorHappened = true; |
33 | | - this.emit('error', err); |
34 | | - } else { |
35 | | - this.emit('processed', path, result); |
36 | | - } |
37 | | - |
38 | | - checkForEnd(); |
39 | | - }); |
40 | | - }); |
41 | | - |
42 | | - glob.on('end', () => { |
43 | | - allQueued = true; |
44 | | - this.emit('allQueued', { queuedCount, processedCount }); |
45 | | - checkForEnd(); |
46 | | - }); |
47 | | - } |
48 | | - |
49 | | - process(path, callback) { |
50 | | - this.workers(path, callback); |
51 | | - } |
52 | | - |
53 | | - destroy(callback) { |
54 | | - this.glob.destroy(); |
55 | | - workerFarm.end(this.workers, callback); |
56 | | - } |
| 8 | + constructor(globPattern, worker, options) { |
| 9 | + super(); |
| 10 | + options = options || {}; |
| 11 | + const glob = (this.glob = globStream(globPattern)); |
| 12 | + const workers = (this.workers = workerFarm(options.worker || {}, worker)); |
| 13 | + |
| 14 | + let allQueued = false; |
| 15 | + let errorHappened = false; |
| 16 | + let queuedCount = 0; |
| 17 | + let processedCount = 0; |
| 18 | + |
| 19 | + const checkForEnd = () => { |
| 20 | + if (errorHappened || (allQueued && queuedCount === processedCount)) { |
| 21 | + if (!options.keepAlive) { |
| 22 | + workerFarm.end(workers); |
| 23 | + } |
| 24 | + if (!errorHappened) this.emit('end'); |
| 25 | + } |
| 26 | + }; |
| 27 | + |
| 28 | + glob.on('data', ({ path }) => { |
| 29 | + queuedCount++; |
| 30 | + this.emit('queued', path); |
| 31 | + this.process(path, (err, result) => { |
| 32 | + processedCount++; |
| 33 | + if (err) { |
| 34 | + errorHappened = true; |
| 35 | + this.emit('error', err); |
| 36 | + } else { |
| 37 | + this.emit('processed', path, result); |
| 38 | + } |
| 39 | + |
| 40 | + checkForEnd(); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + glob.on('end', () => { |
| 45 | + allQueued = true; |
| 46 | + this.emit('allQueued', { queuedCount, processedCount }); |
| 47 | + checkForEnd(); |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + process(path, callback) { |
| 52 | + this.workers(path, callback); |
| 53 | + } |
| 54 | + |
| 55 | + destroy(callback) { |
| 56 | + this.glob.destroy(); |
| 57 | + workerFarm.end(this.workers, callback); |
| 58 | + } |
57 | 59 | } |
58 | 60 |
|
59 | 61 | module.exports = FileProcessor; |
0 commit comments