a zip code crypto-currency system good for red ONLY

NodeWatchFileSystem.js 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Watchpack = require("watchpack");
  7. class NodeWatchFileSystem {
  8. constructor(inputFileSystem) {
  9. this.inputFileSystem = inputFileSystem;
  10. this.watcherOptions = {
  11. aggregateTimeout: 0
  12. };
  13. this.watcher = new Watchpack(this.watcherOptions);
  14. }
  15. watch(files, dirs, missing, startTime, options, callback, callbackUndelayed) {
  16. if(!Array.isArray(files))
  17. throw new Error("Invalid arguments: 'files'");
  18. if(!Array.isArray(dirs))
  19. throw new Error("Invalid arguments: 'dirs'");
  20. if(!Array.isArray(missing))
  21. throw new Error("Invalid arguments: 'missing'");
  22. if(typeof callback !== "function")
  23. throw new Error("Invalid arguments: 'callback'");
  24. if(typeof startTime !== "number" && startTime)
  25. throw new Error("Invalid arguments: 'startTime'");
  26. if(typeof options !== "object")
  27. throw new Error("Invalid arguments: 'options'");
  28. if(typeof callbackUndelayed !== "function" && callbackUndelayed)
  29. throw new Error("Invalid arguments: 'callbackUndelayed'");
  30. const oldWatcher = this.watcher;
  31. this.watcher = new Watchpack(options);
  32. if(callbackUndelayed)
  33. this.watcher.once("change", callbackUndelayed);
  34. this.watcher.once("aggregated", (changes, removals) => {
  35. changes = changes.concat(removals);
  36. if(this.inputFileSystem && this.inputFileSystem.purge) {
  37. this.inputFileSystem.purge(changes);
  38. }
  39. const times = this.watcher.getTimes();
  40. callback(null,
  41. changes.filter(file => files.indexOf(file) >= 0).sort(),
  42. changes.filter(file => dirs.indexOf(file) >= 0).sort(),
  43. changes.filter(file => missing.indexOf(file) >= 0).sort(), times, times);
  44. });
  45. this.watcher.watch(files.concat(missing), dirs.concat(missing), startTime);
  46. if(oldWatcher) {
  47. oldWatcher.close();
  48. }
  49. return {
  50. close: () => {
  51. if(this.watcher) {
  52. this.watcher.close();
  53. this.watcher = null;
  54. }
  55. },
  56. pause: () => {
  57. if(this.watcher) {
  58. this.watcher.pause();
  59. }
  60. }
  61. };
  62. }
  63. }
  64. module.exports = NodeWatchFileSystem;