watcherManager.js 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. var path = require("path");
  6. function WatcherManager() {
  7. this.directoryWatchers = {};
  8. }
  9. WatcherManager.prototype.getDirectoryWatcher = function(directory, options) {
  10. var DirectoryWatcher = require("./DirectoryWatcher");
  11. options = options || {};
  12. var key = directory + " " + JSON.stringify(options);
  13. if(!this.directoryWatchers[key]) {
  14. this.directoryWatchers[key] = new DirectoryWatcher(directory, options);
  15. this.directoryWatchers[key].on("closed", function() {
  16. delete this.directoryWatchers[key];
  17. }.bind(this));
  18. }
  19. return this.directoryWatchers[key];
  20. };
  21. WatcherManager.prototype.watchFile = function watchFile(p, options, startTime) {
  22. var directory = path.dirname(p);
  23. return this.getDirectoryWatcher(directory, options).watch(p, startTime);
  24. };
  25. WatcherManager.prototype.watchDirectory = function watchDirectory(directory, options, startTime) {
  26. return this.getDirectoryWatcher(directory, options).watch(directory, startTime);
  27. };
  28. module.exports = new WatcherManager();