watchFile.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.unwatchAllFiles = exports.unwatchFile = exports.watchFile = void 0;
  4. const CustomStatWatcher_1 = require("./watchFile/CustomStatWatcher");
  5. const statWatchersByFakeFS = new WeakMap();
  6. function watchFile(fakeFs, path, a, b) {
  7. let bigint;
  8. let persistent;
  9. let interval;
  10. let listener;
  11. switch (typeof a) {
  12. case `function`:
  13. {
  14. bigint = false;
  15. persistent = true;
  16. interval = 5007;
  17. listener = a;
  18. }
  19. break;
  20. default:
  21. {
  22. ({
  23. bigint = false,
  24. persistent = true,
  25. interval = 5007,
  26. } = a);
  27. listener = b;
  28. }
  29. break;
  30. }
  31. let statWatchers = statWatchersByFakeFS.get(fakeFs);
  32. if (typeof statWatchers === `undefined`)
  33. statWatchersByFakeFS.set(fakeFs, statWatchers = new Map());
  34. let statWatcher = statWatchers.get(path);
  35. if (typeof statWatcher === `undefined`) {
  36. statWatcher = CustomStatWatcher_1.CustomStatWatcher.create(fakeFs, path, { bigint });
  37. statWatchers.set(path, statWatcher);
  38. }
  39. statWatcher.registerChangeListener(listener, { persistent, interval });
  40. return statWatcher;
  41. }
  42. exports.watchFile = watchFile;
  43. function unwatchFile(fakeFs, path, cb) {
  44. const statWatchers = statWatchersByFakeFS.get(fakeFs);
  45. if (typeof statWatchers === `undefined`)
  46. return;
  47. const statWatcher = statWatchers.get(path);
  48. if (typeof statWatcher === `undefined`)
  49. return;
  50. if (typeof cb === `undefined`)
  51. statWatcher.unregisterAllChangeListeners();
  52. else
  53. statWatcher.unregisterChangeListener(cb);
  54. if (!statWatcher.hasChangeListeners()) {
  55. statWatcher.stop();
  56. statWatchers.delete(path);
  57. }
  58. }
  59. exports.unwatchFile = unwatchFile;
  60. function unwatchAllFiles(fakeFs) {
  61. const statWatchers = statWatchersByFakeFS.get(fakeFs);
  62. if (typeof statWatchers === `undefined`)
  63. return;
  64. for (const path of statWatchers.keys()) {
  65. unwatchFile(fakeFs, path);
  66. }
  67. }
  68. exports.unwatchAllFiles = unwatchAllFiles;