class Spring::Watcher::Abstract

A user of a watcher can use IO.select to wait for changes:

watcher = MyWatcher.new(root, latency)
IO.select([watcher]) # watcher is running in background
watcher.stale? # => true

Attributes

directories[R]
files[R]
latency[R]
root[R]

Public Class Methods

new(root, latency) click to toggle source
Calls superclass method
# File lib/spring/watcher/abstract.rb, line 16
def initialize(root, latency)
  super()

  @root        = File.realpath(root)
  @latency     = latency
  @files       = {}
  @directories = {}
  @stale       = false
  @listeners   = []

  @on_debug    = nil
end

Public Instance Methods

add(*items) click to toggle source
# File lib/spring/watcher/abstract.rb, line 37
def add(*items)
  debug { "watcher: add: #{items.inspect}" }

  items = items.flatten.map do |item|
    item = Pathname.new(item)

    if item.relative?
      Pathname.new("#{root}/#{item}")
    else
      item
    end
  end

  items = items.select do |item|
    if item.symlink?
      item.readlink.exist?.tap do |exists|
        if !exists
          debug { "add: ignoring dangling symlink: #{item.inspect} -> #{item.readlink.inspect}" }
        end
      end
    else
      item.exist?
    end
  end

  synchronize {
    items.each do |item|
      if item.directory?
        directories[item.realpath.to_s] = true
      else
        begin
          files[item.realpath.to_s] = true
        rescue Errno::ENOENT
          # Race condition. Ignore symlinks whose target was removed
          # since the check above, or are deeply chained.
          debug { "add: ignoring now-dangling symlink: #{item.inspect} -> #{item.readlink.inspect}" }
        end
      end
    end

    subjects_changed
  }
end
debug() { || ... } click to toggle source
# File lib/spring/watcher/abstract.rb, line 33
def debug
  @on_debug.call(yield) if @on_debug
end
mark_stale() click to toggle source
# File lib/spring/watcher/abstract.rb, line 90
def mark_stale
  return if stale?
  @stale = true
  debug { "marked stale, calling listeners: listeners=#{@listeners.inspect}" }
  @listeners.each(&:call)
end
on_debug(&block) click to toggle source
# File lib/spring/watcher/abstract.rb, line 29
def on_debug(&block)
  @on_debug = block
end
on_stale(&block) click to toggle source
# File lib/spring/watcher/abstract.rb, line 85
def on_stale(&block)
  debug { "added listener: #{block.inspect}" }
  @listeners << block
end
restart() click to toggle source
# File lib/spring/watcher/abstract.rb, line 97
def restart
  debug { "restarting" }
  stop
  start
end
stale?() click to toggle source
# File lib/spring/watcher/abstract.rb, line 81
def stale?
  @stale
end
start() click to toggle source
# File lib/spring/watcher/abstract.rb, line 103
def start
  raise NotImplementedError
end
stop() click to toggle source
# File lib/spring/watcher/abstract.rb, line 107
def stop
  raise NotImplementedError
end
subjects_changed() click to toggle source
# File lib/spring/watcher/abstract.rb, line 111
def subjects_changed
  raise NotImplementedError
end