Class: Spring::Watcher::Listen
- Inherits:
-
Abstract
- Object
- Abstract
- Spring::Watcher::Listen
- Defined in:
- lib/spring/watcher/listen.rb
Instance Attribute Summary collapse
-
#listener ⇒ Object
readonly
Returns the value of attribute listener.
Instance Method Summary collapse
- #base_directories ⇒ Object
- #changed(modified, added, removed) ⇒ Object
-
#initialize ⇒ Listen
constructor
A new instance of Listen.
- #mark_stale ⇒ Object
- #running? ⇒ Boolean
- #start ⇒ Object
- #stop ⇒ Object
- #subjects_changed ⇒ Object
- #watching?(file) ⇒ Boolean
Constructor Details
#initialize ⇒ Listen
Returns a new instance of Listen.
26 27 28 29 |
# File 'lib/spring/watcher/listen.rb', line 26 def initialize(*) super @listener = nil end |
Instance Attribute Details
#listener ⇒ Object (readonly)
Returns the value of attribute listener.
24 25 26 |
# File 'lib/spring/watcher/listen.rb', line 24 def listener @listener end |
Instance Method Details
#base_directories ⇒ Object
76 77 78 79 80 81 |
# File 'lib/spring/watcher/listen.rb', line 76 def base_directories ([root] + files.keys.reject { |f| f.start_with? "#{root}/" }.map { |f| File.("#{f}/..") } + directories.keys.reject { |d| d.start_with? "#{root}/" } ).uniq.map { |path| Pathname.new(path) } end |
#changed(modified, added, removed) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/spring/watcher/listen.rb', line 60 def changed(modified, added, removed) synchronize do if (modified + added + removed).any? { |f| watching? f } mark_stale end end end |
#mark_stale ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/spring/watcher/listen.rb', line 68 def mark_stale super # May be called from listen thread which won't be happy # about stopping itself, so stop from another thread. Thread.new { stop }.join end |
#running? ⇒ Boolean
45 46 47 |
# File 'lib/spring/watcher/listen.rb', line 45 def running? @listener && @listener.processing? end |
#start ⇒ Object
31 32 33 34 35 36 |
# File 'lib/spring/watcher/listen.rb', line 31 def start return if @listener @listener = ::Listen.to(*base_directories, latency: latency, &method(:changed)) @listener.start end |
#stop ⇒ Object
38 39 40 41 42 43 |
# File 'lib/spring/watcher/listen.rb', line 38 def stop if @listener @listener.stop @listener = nil end end |
#subjects_changed ⇒ Object
49 50 51 52 53 54 |
# File 'lib/spring/watcher/listen.rb', line 49 def subjects_changed return unless @listener return unless @listener.respond_to?(:directories) return unless @listener.directories.sort != base_directories.sort restart end |
#watching?(file) ⇒ Boolean
56 57 58 |
# File 'lib/spring/watcher/listen.rb', line 56 def watching?(file) files.include?(file) || file.start_with?(*directories.keys) end |