Newer
Older
CVSS_3.0_GUI / node_modules / nwjs-builder-phoenix / node_modules / dir-compare / fileDescriptorQueue.js
root on 7 May 2019 1 KB Initial commit
  1. 'use strict';
  2.  
  3. var fs = require('fs');
  4.  
  5. /**
  6. * Limits the number of concurrent file handlers.
  7. * Use it as a wrapper over fs.open() and fs.close().
  8. * Example:
  9. * var fdQueue = new FileDescriptorQueue(8);
  10. * fdQueue.open(path, flags, (err, fd) =>{
  11. * ...
  12. * fdQueue.close(fd, (err) =>{
  13. * ...
  14. * });
  15. * });
  16. * As of node v7, calling fd.close without a callback is deprecated.
  17. */
  18. var FileDescriptorQueue = function(maxFilesNo) {
  19. var pendingJobs = [];
  20. var activeCount = 0;
  21.  
  22. var open = function(path, flags, callback) {
  23. pendingJobs.push({
  24. path : path,
  25. flags : flags,
  26. callback : callback
  27. });
  28. process();
  29. }
  30.  
  31. var process = function() {
  32. if (pendingJobs.length > 0 && activeCount < maxFilesNo) {
  33. var job = pendingJobs.shift();
  34. activeCount++;
  35. fs.open(job.path, job.flags, function(err, fd) {
  36. job.callback(err, fd);
  37. });
  38. }
  39. }
  40.  
  41. var close = function(fd, callback) {
  42. activeCount--;
  43. fs.close(fd, callback);
  44. process();
  45. }
  46.  
  47. return {
  48. open : open,
  49. close : close
  50. };
  51. }
  52.  
  53. module.exports = FileDescriptorQueue;
Buy Me A Coffee