Class: Spring::Watcher::Abstract
- Inherits:
-
Object
- Object
- Spring::Watcher::Abstract
- Includes:
- Mutex_m
- Defined in:
- lib/spring/watcher/abstract.rb
Overview
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
Direct Known Subclasses
Instance Attribute Summary collapse
-
#directories ⇒ Object
readonly
Returns the value of attribute directories.
-
#files ⇒ Object
readonly
Returns the value of attribute files.
-
#latency ⇒ Object
readonly
Returns the value of attribute latency.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
- #add(*items) ⇒ Object
- #debug ⇒ Object
-
#initialize(root, latency) ⇒ Abstract
constructor
A new instance of Abstract.
- #mark_stale ⇒ Object
- #on_debug(&block) ⇒ Object
- #on_stale(&block) ⇒ Object
- #restart ⇒ Object
- #stale? ⇒ Boolean
- #start ⇒ Object
- #stop ⇒ Object
- #subjects_changed ⇒ Object
Constructor Details
#initialize(root, latency) ⇒ Abstract
Returns a new instance of Abstract.
16 17 18 19 20 21 22 23 24 25 26 27 |
# 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 |
Instance Attribute Details
#directories ⇒ Object (readonly)
Returns the value of attribute directories.
14 15 16 |
# File 'lib/spring/watcher/abstract.rb', line 14 def directories @directories end |
#files ⇒ Object (readonly)
Returns the value of attribute files.
14 15 16 |
# File 'lib/spring/watcher/abstract.rb', line 14 def files @files end |
#latency ⇒ Object (readonly)
Returns the value of attribute latency.
14 15 16 |
# File 'lib/spring/watcher/abstract.rb', line 14 def latency @latency end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
14 15 16 |
# File 'lib/spring/watcher/abstract.rb', line 14 def root @root end |
Instance Method Details
#add(*items) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# 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 ⇒ Object
33 34 35 |
# File 'lib/spring/watcher/abstract.rb', line 33 def debug @on_debug.call(yield) if @on_debug end |
#mark_stale ⇒ Object
90 91 92 93 94 95 |
# 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) ⇒ Object
29 30 31 |
# File 'lib/spring/watcher/abstract.rb', line 29 def on_debug(&block) @on_debug = block end |
#on_stale(&block) ⇒ Object
85 86 87 88 |
# File 'lib/spring/watcher/abstract.rb', line 85 def on_stale(&block) debug { "added listener: #{block.inspect}" } @listeners << block end |
#restart ⇒ Object
97 98 99 100 101 |
# File 'lib/spring/watcher/abstract.rb', line 97 def restart debug { "restarting" } stop start end |
#stale? ⇒ Boolean
81 82 83 |
# File 'lib/spring/watcher/abstract.rb', line 81 def stale? @stale end |
#start ⇒ Object
103 104 105 |
# File 'lib/spring/watcher/abstract.rb', line 103 def start raise NotImplementedError end |
#stop ⇒ Object
107 108 109 |
# File 'lib/spring/watcher/abstract.rb', line 107 def stop raise NotImplementedError end |
#subjects_changed ⇒ Object
111 112 113 |
# File 'lib/spring/watcher/abstract.rb', line 111 def subjects_changed raise NotImplementedError end |