lib/vendor/darwin/lib/rb-fsevent/fsevent.rb in guard-1.0.1 vs lib/vendor/darwin/lib/rb-fsevent/fsevent.rb in guard-1.0.2
- old
+ new
@@ -12,10 +12,14 @@
END
end
attr_reader :paths, :callback
+ def initialize args = nil, &block
+ watch(args, &block) unless args.nil?
+ end
+
def watch(watch_paths, options=nil, &block)
@paths = watch_paths.kind_of?(Array) ? watch_paths : [watch_paths]
@callback = block
if options.kind_of?(Hash)
@@ -26,37 +30,39 @@
@options = []
end
end
def run
+ @pipe = open_pipe
@running = true
+
# please note the use of IO::select() here, as it is used specifically to
# preserve correct signal handling behavior in ruby 1.8.
- while @running && IO::select([pipe], nil, nil, nil)
- if line = pipe.readline
- modified_dir_paths = line.split(":").select { |dir| dir != "\n" }
+ while @running && IO::select([@pipe], nil, nil, nil)
+ if line = @pipe.readline
+ modified_dir_paths = line.split(':').select { |dir| dir != "\n" }
callback.call(modified_dir_paths)
end
end
rescue Interrupt, IOError
ensure
stop
end
def stop
- if pipe
- Process.kill("KILL", pipe.pid)
- pipe.close
+ unless @pipe.nil?
+ Process.kill('KILL', @pipe.pid)
+ @pipe.close
end
rescue IOError
ensure
- @pipe = @running = nil
+ @running = false
end
if RUBY_VERSION < '1.9'
- def pipe
- @pipe ||= IO.popen("#{self.class.watcher_path} #{options_string} #{shellescaped_paths}")
+ def open_pipe
+ IO.popen("#{self.class.watcher_path} #{options_string} #{shellescaped_paths}")
end
private
def options_string
@@ -83,11 +89,11 @@
str.gsub!(/\n/, "'\n'")
return str
end
else
- def pipe
- @pipe ||= IO.popen([self.class.watcher_path] + @options + @paths)
+ def open_pipe
+ IO.popen([self.class.watcher_path] + @options + @paths)
end
end
private