watcherManager.js 1.1 KB

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