forked from gliviu/dir-compare
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileDescriptorQueue.js
More file actions
53 lines (46 loc) · 1.08 KB
/
Copy pathfileDescriptorQueue.js
File metadata and controls
53 lines (46 loc) · 1.08 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
46
47
48
49
50
51
52
53
'use strict';
var fs = require('fs');
/**
* Limits the number of concurrent file handlers.
* Use it as a wrapper over fs.open() and fs.close().
* Example:
* var fdQueue = new FileDescriptorQueue(8);
* fdQueue.open(path, flags, (err, fd) =>{
* ...
* fdQueue.close(fd, (err) =>{
* ...
* });
* });
* As of node v7, calling fd.close without a callback is deprecated.
*/
var FileDescriptorQueue = function(maxFilesNo) {
var pendingJobs = [];
var activeCount = 0;
var open = function(path, flags, callback) {
pendingJobs.push({
path : path,
flags : flags,
callback : callback
});
process();
}
var process = function() {
if (pendingJobs.length > 0 && activeCount < maxFilesNo) {
var job = pendingJobs.shift();
activeCount++;
fs.open(job.path, job.flags, function(err, fd) {
job.callback(err, fd);
});
}
}
var close = function(fd, callback) {
activeCount--;
fs.close(fd, callback);
process();
}
return {
open : open,
close : close
};
}
module.exports = FileDescriptorQueue;