-
Notifications
You must be signed in to change notification settings - Fork 35
/
listen.rb
84 lines (67 loc) · 1.96 KB
/
listen.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
80
81
82
83
84
return if ENV['DISABLE_SPRING_WATCHER_LISTEN']
require "spring/watcher"
require "spring/watcher/abstract"
require "listen"
require "listen/version"
if defined?(Celluloid)
# fork() doesn't preserve threads, so a clean
# Celluloid shutdown isn't possible, but we can
# reduce the 10 second timeout
# There's a patch for Celluloid to avoid this (search for 'fork' in Celluloid
# issues)
Celluloid.shutdown_timeout = 2
end
module Spring
module Watcher
class Listen < Abstract
Spring.watch_method = self
attr_reader :listener
def initialize(*)
super
@listener = nil
end
def start
return if @listener
@listener = ::Listen.to(*base_directories, latency: latency, &method(:changed))
@listener.start
end
def stop
if @listener
@listener.stop
@listener = nil
end
end
def running?
@listener && @listener.processing?
end
def subjects_changed
return unless @listener
return unless @listener.respond_to?(:directories)
return unless @listener.directories.sort != base_directories.sort
restart
end
def watching?(file)
files.include?(file) || file.start_with?(*directories.keys)
end
def changed(modified, added, removed)
synchronize do
if (modified + added + removed).any? { |f| watching? f }
mark_stale
end
end
end
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
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
end
end
end