Class: Jsus::Util::Watcher
- Inherits:
-
Object
- Object
- Jsus::Util::Watcher
- Defined in:
- lib/jsus/util/watcher.rb
Class Method Summary collapse
-
.watch(input_dirs, ignored_dirs = []) {|filename| ... } ⇒ FSSM::Monitor
Watches input directories and their subdirectories for changes in js source files and package metadata files.
Instance Method Summary collapse
-
#initialize(input_dirs, ignored_dirs = [], &callback) ⇒ Watcher
constructor
Instantiates a FSSM monitor and starts watching.
- #run ⇒ Object
- #running? ⇒ Boolean
-
#watch_callback(base, match) ⇒ Object
Default callback for the FSSM watcher.
Constructor Details
#initialize(input_dirs, ignored_dirs = [], &callback) ⇒ Watcher
Instantiates a FSSM monitor and starts watching. Consider using class method Jsus::Util::Watcher.watch instead.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/jsus/util/watcher.rb', line 21 def initialize(input_dirs, ignored_dirs = [], &callback) require 'fssm' @callback = callback input_dirs = Array(input_dirs).compact @semaphore = Mutex.new watcher = self @ignored_dirs = Array(ignored_dirs).map {|dir| File.(dir) } FSSM.monitor do input_dirs.each do |dir| dir = File.(dir) path(dir) do glob ["**/*.js", "**/package.yml", "**/package.json"] create &watcher.method(:watch_callback) update &watcher.method(:watch_callback) delete &watcher.method(:watch_callback) end end end rescue LoadError => e Jsus.logger.error "You need to install fssm gem for --watch option." Jsus.logger.error "You may also want to install rb-fsevent for OS X" if RUBY_PLATFORM =~ /darwin/ raise e end |
Class Method Details
.watch(input_dirs, ignored_dirs = []) {|filename| ... } ⇒ FSSM::Monitor
Watches input directories and their subdirectories for changes in js source files and package metadata files.
12 13 14 |
# File 'lib/jsus/util/watcher.rb', line 12 def watch(input_dirs, ignored_dirs = [], &callback) new(input_dirs, ignored_dirs, &callback) end |
Instance Method Details
#run ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/jsus/util/watcher.rb', line 62 def run if @semaphore.try_lock begin yield rescue Exception => e Jsus.logger.error "Exception happened during watching: #{e}, #{e.inspect}" Jsus.logger.error "\t#{e.backtrace.join("\n\t")}" if Jsus.verbose? Jsus.logger.error "Compilation FAILED." ensure @semaphore.unlock end end end |
#running? ⇒ Boolean
78 79 80 |
# File 'lib/jsus/util/watcher.rb', line 78 def running? @semaphore.locked? end |
#watch_callback(base, match) ⇒ Object
Note:
Defers the processing to a separate thread and ignores all the incoming events received during the processing.
Default callback for the FSSM watcher.
52 53 54 55 56 57 58 59 |
# File 'lib/jsus/util/watcher.rb', line 52 def watch_callback(base, match) Thread.new do full_path = File.join(base, match) unless @ignored_dirs.any? {|dir| full_path.index(dir) == 0 } run { @callback.call(full_path) } end end end |