class Spring::Watcher::Polling

def add(*)

def add(*)
  check_stale if @poller
  super
end

def check_stale

def check_stale
  synchronize { mark_stale if mtime < compute_mtime }
end

def compute_mtime

def compute_mtime
  expanded_files.map { |f| File.mtime(f).to_f }.max || 0
rescue Errno::ENOENT
  # if a file does no longer exist, the watcher is always stale.
  Float::MAX
end

def expanded_files

def expanded_files
  files + Dir["{#{directories.map { |d| "#{d}/**/*" }.join(",")}}"]
end

def initialize(root, latency)

def initialize(root, latency)
  super
  @mtime  = 0
  @poller = nil
end

def start

def start
  unless @poller
    @poller = Thread.new {
      Thread.current.abort_on_exception = true
      loop do
        Kernel.sleep latency
        check_stale
      end
    }
  end
end

def stop

def stop
  if @poller
    @poller.kill
    @poller = nil
  end
end

def subjects_changed

def subjects_changed
  @mtime = compute_mtime
end