watch-memory-system.js 3.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var path_1 = require("path");
  4. var events_1 = require("../util/events");
  5. var logger_1 = require("../logger/logger");
  6. var WatchMemorySystem = (function () {
  7. function WatchMemorySystem(fileCache, srcDir) {
  8. this.fileCache = fileCache;
  9. this.srcDir = srcDir;
  10. this.lastWatchEventTimestamp = Date.now();
  11. }
  12. WatchMemorySystem.prototype.close = function () {
  13. this.isListening = false;
  14. };
  15. WatchMemorySystem.prototype.pause = function () {
  16. this.isListening = false;
  17. };
  18. WatchMemorySystem.prototype.watch = function (filePathsBeingWatched, dirPaths, missing, startTime, options, aggregatedCallback, immediateCallback) {
  19. this.filePathsBeingWatched = filePathsBeingWatched;
  20. this.dirPaths = dirPaths;
  21. this.missing = missing;
  22. this.startTime = startTime;
  23. this.options = options;
  24. this.immediateCallback = immediateCallback;
  25. this.aggregatedCallback = aggregatedCallback;
  26. if (!this.isListening) {
  27. this.startListening();
  28. }
  29. return {
  30. pause: this.pause,
  31. close: this.close
  32. };
  33. };
  34. WatchMemorySystem.prototype.startListening = function () {
  35. var _this = this;
  36. this.isListening = true;
  37. events_1.on(events_1.EventType.WebpackFilesChanged, function () {
  38. _this.changes = new Set();
  39. var filePaths = _this.fileCache.getAll().filter(function (file) { return file.timestamp >= _this.lastWatchEventTimestamp && file.path.startsWith(_this.srcDir) && path_1.extname(file.path) === '.ts'; }).map(function (file) { return file.path; });
  40. logger_1.Logger.debug('filePaths: ', filePaths);
  41. _this.lastWatchEventTimestamp = Date.now();
  42. _this.processChanges(filePaths);
  43. });
  44. };
  45. WatchMemorySystem.prototype.processChanges = function (filePaths) {
  46. this.immediateCallback(filePaths[0], Date.now());
  47. for (var _i = 0, filePaths_1 = filePaths; _i < filePaths_1.length; _i++) {
  48. var path = filePaths_1[_i];
  49. this.changes.add(path);
  50. }
  51. // don't bother waiting around, just call doneAggregating right away.
  52. // keep it as a function in case we need to wait via setTimeout a bit in the future
  53. this.doneAggregating(this.changes);
  54. };
  55. WatchMemorySystem.prototype.doneAggregating = function (changes) {
  56. var _this = this;
  57. this.isAggregating = false;
  58. // process the changes
  59. var filePaths = Array.from(changes);
  60. var files = filePaths.filter(function (filePath) { return _this.filePathsBeingWatched.indexOf(filePath) >= 0; }).sort();
  61. var dirs = filePaths.filter(function (filePath) { return _this.dirPaths.indexOf(filePath) >= 0; }).sort();
  62. var missing = filePaths.filter(function (filePath) { return _this.missing.indexOf(filePath) >= 0; }).sort();
  63. var times = this.getTimes(this.filePathsBeingWatched, this.startTime, this.fileCache);
  64. this.aggregatedCallback(null, files, dirs, missing, times, times);
  65. };
  66. WatchMemorySystem.prototype.getTimes = function (allFiles, startTime, fileCache) {
  67. var times = {};
  68. for (var _i = 0, allFiles_1 = allFiles; _i < allFiles_1.length; _i++) {
  69. var filePath = allFiles_1[_i];
  70. var file = fileCache.get(filePath);
  71. if (file) {
  72. times[filePath] = file.timestamp;
  73. }
  74. else {
  75. times[filePath] = startTime;
  76. }
  77. }
  78. return times;
  79. };
  80. return WatchMemorySystem;
  81. }());
  82. exports.WatchMemorySystem = WatchMemorySystem;