Permalink
Browse files

Refactor darwin handler

Also added specs
  • Loading branch information...
1 parent c220ae1 commit 0aaa732844bbf9cfc0e2eea3d26d7002034194dc mynyml committed May 28, 2010
Showing with 130 additions and 212 deletions.
  1. +2 −2 lib/watchr.rb
  2. +0 −5 lib/watchr/controller.rb
  3. +45 −81 lib/watchr/event_handlers/darwin.rb
  4. +76 −121 test/event_handlers/test_darwin.rb
  5. +4 −0 test/test_helper.rb
  6. +3 −3 test/test_watchr.rb
View
@@ -37,7 +37,7 @@ module EventHandler
autoload :Base, 'watchr/event_handlers/base'
autoload :Portable, 'watchr/event_handlers/portable'
autoload :Unix, 'watchr/event_handlers/unix' if ::Watchr::HAVE_REV
- autoload :FSE, 'watchr/event_handlers/darwin' if ::Watchr::HAVE_FSE
+ autoload :Darwin, 'watchr/event_handlers/darwin' if ::Watchr::HAVE_FSE
end
class << self
@@ -111,7 +111,7 @@ def handler
case ENV['HANDLER'] || Config::CONFIG['host_os']
when /darwin|mach|osx|fsevents?/i
if Watchr::HAVE_FSE
- Watchr::EventHandler::FSE
+ Watchr::EventHandler::Darwin
else
Watchr.debug "fsevent not found. `gem install ruby-fsevent` to get evented handler"
Watchr::EventHandler::Portable
View
@@ -31,7 +31,6 @@ class Controller
def initialize(script, handler)
@script, @handler = script, handler
@handler.add_observer(self)
- @handler.controller = self
Watchr.debug "using %s handler" % handler.class.name
end
@@ -67,10 +66,6 @@ def update(path, event_type = nil)
end
end
- def update_monitored_paths
- @handler.refresh(monitored_paths)
- end
-
# List of paths the script is monitoring.
#
# Basically this means all paths below current directoly recursivelly that
@@ -1,105 +1,69 @@
module Watchr
module EventHandler
- class FSEWatcher < ::FSEvent
- attr_reader :handler
-
- def initialize(handler)
- super()
- @handler = handler
- self.latency = 0.2
- end
-
- def on_change(dirs)
- handler.on_change(dirs)
- end
- end
-
- class FSE
+ class Darwin < FSEvent
include Base
- attr_reader :watcher, :path_stats, :monitored_paths
- attr_accessor :controller
-
def initialize
- @watcher = FSEWatcher.new(self)
- @path_stats = {}
- end
-
- def on_change(dirs)
- update_monitored_paths
- watch_monitored_paths
- dirs.each do |dir|
- #Watchr.debug "change in #{dir}"
- changed_pathname = Pathname(dir)
- monitored_paths.each do |pathname|
- if pathname.dirname.basename == changed_pathname.basename
- type = detect_change(pathname)
- if type
- #Watchr.debug type
- notify(pathname, type)
- update_path_stats(pathname) unless type == :deleted
- end
- end
- end
- end
+ super
+ self.latency = 0.2
end
def listen(monitored_paths)
- @monitored_paths = monitored_paths
- watch_monitored_paths
- @watcher.start
+ register_paths(monitored_paths)
+ start
end
def refresh(monitored_paths)
- @monitored_paths = monitored_paths
- watch_monitored_paths
+ register_paths(monitored_paths)
+ restart
end
- protected
-
- def watch_monitored_paths
- init_path_stats
- paths = monitored_paths.map {|p| p.dirname.to_s}.uniq
- @watcher.watch_directories(paths)
- end
+ private
+ def on_change(dirs)
+ dirs.each {|dir| notify(*detect_change_in(dir)) }
+ end
- def init_path_stats
- now = Time.now
- monitored_paths.each do |pathname|
- unless path_stats[pathname]
- path_stats[pathname] = {:mtime => now, :atime => now, :ctime => now}
+ def detect_change_in(dir)
+ paths = monitored_paths_for(dir)
+ paths.each do |path|
+ type = event_type(path)
+ return [path, type] if type
end
end
- end
-
- def detect_change(pathname)
- return :deleted if !pathname.exist?
- return :modified if pathname.mtime > mtime(pathname)
- return :accessed if pathname.atime > atime(pathname)
- return :changed if pathname.ctime > ctime(pathname)
- end
- def update_monitored_paths
- @monitored_paths = controller.monitored_paths
- end
+ def event_type(path)
+ return :deleted if !path.exist?
+ return :modified if path.mtime > @reference_times[path][:mtime]
+ return :accessed if path.atime > @reference_times[path][:atime]
+ return :changed if path.ctime > @reference_times[path][:ctime]
+ nil
+ end
- def update_path_stats(pathname)
- path_stats[pathname][:mtime] = pathname.mtime
- path_stats[pathname][:atime] = pathname.atime
- path_stats[pathname][:ctime] = pathname.ctime
- end
+ def monitored_paths_for(dir)
+ dir = Pathname(dir).expand_path
+ @paths.select {|path| path.dirname.expand_path == dir }
+ end
- def mtime(pathname)
- path_stats[pathname][:mtime]
- end
+ def register_paths(paths)
+ @paths = paths
+ watch_directories(dirs_for(@paths))
+ update_reference_times
+ end
- def atime(pathname)
- path_stats[pathname][:atime]
- end
+ def dirs_for(paths)
+ paths.map {|path| path.dirname.to_s }.uniq
+ end
- def ctime(pathname)
- path_stats[pathname][:ctime]
- end
+ def update_reference_times
+ @reference_times = {}
+ now = Time.now
+ @paths.each do |path|
+ @reference_times[path] = {}
+ @reference_times[path][:atime] = now
+ @reference_times[path][:mtime] = now
+ @reference_times[path][:ctime] = now
+ end
+ end
end
end
end
Oops, something went wrong.

0 comments on commit 0aaa732

Please sign in to comment.