Class: Spring::Watcher::Listen

Inherits:
Abstract
  • Object
show all
Defined in:
lib/spring/watcher/listen.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeListen

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

#listenerObject (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_directoriesObject



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.expand_path("#{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_staleObject



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

Returns:

  • (Boolean)


45
46
47
# File 'lib/spring/watcher/listen.rb', line 45

def running?
  @listener && @listener.processing?
end

#startObject



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

#stopObject



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_changedObject



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

Returns:

  • (Boolean)


56
57
58
# File 'lib/spring/watcher/listen.rb', line 56

def watching?(file)
  files.include?(file) || file.start_with?(*directories.keys)
end