Class: ActiveSupport::EventedFileUpdateChecker::Core
- Defined in:
- lib/active_support/evented_file_update_checker.rb
Instance Attribute Summary collapse
-
#files ⇒ Object
readonly
Returns the value of attribute files.
-
#updated ⇒ Object
readonly
Returns the value of attribute updated.
Instance Method Summary collapse
- #changed(modified, added, removed) ⇒ Object
- #common_path(paths) ⇒ Object
- #directories_to_watch ⇒ Object
- #finalizer ⇒ Object
-
#initialize(files, dirs) ⇒ Core
constructor
A new instance of Core.
- #normalize_dirs! ⇒ Object
- #restart ⇒ Object
- #restart? ⇒ Boolean
- #start ⇒ Object
- #stop ⇒ Object
- #thread_safely ⇒ Object
- #watching?(file) ⇒ Boolean
Constructor Details
#initialize(files, dirs) ⇒ Core
Returns a new instance of Core.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/active_support/evented_file_update_checker.rb', line 77 def initialize(files, dirs) @files = files.map { |file| Pathname(file). }.to_set @dirs = dirs.each_with_object({}) do |(dir, exts), hash| hash[Pathname(dir).] = Array(exts).map { |ext| ext.to_s.sub(/\A\.?/, ".") }.to_set end @common_path = common_path(@dirs.keys) @dtw = directories_to_watch @missing = [] @updated = Concurrent::AtomicBoolean.new(false) @mutex = Mutex.new start # inotify / FSEvents file descriptors are inherited on fork, so # we need to reopen them otherwise only the parent or the child # will be notified. # FIXME: this callback is keeping a reference on the instance @after_fork = ActiveSupport::ForkTracker.after_fork { start } end |
Instance Attribute Details
#files ⇒ Object (readonly)
Returns the value of attribute files.
75 76 77 |
# File 'lib/active_support/evented_file_update_checker.rb', line 75 def files @files end |
#updated ⇒ Object (readonly)
Returns the value of attribute updated.
75 76 77 |
# File 'lib/active_support/evented_file_update_checker.rb', line 75 def updated @updated end |
Instance Method Details
#changed(modified, added, removed) ⇒ Object
144 145 146 147 148 |
# File 'lib/active_support/evented_file_update_checker.rb', line 144 def changed(modified, added, removed) unless @updated.true? @updated.make_true if (modified + added + removed).any? { |f| watching?(f) } end end |
#common_path(paths) ⇒ Object
178 179 180 |
# File 'lib/active_support/evented_file_update_checker.rb', line 178 def common_path(paths) paths.map { |path| path.ascend.to_a }.reduce(&:&)&.first end |
#directories_to_watch ⇒ Object
172 173 174 175 176 |
# File 'lib/active_support/evented_file_update_checker.rb', line 172 def directories_to_watch dtw = @dirs.keys | @files.map(&:dirname) accounted_for = dtw.to_set + Gem.path.map { |path| Pathname(path) } dtw.reject { |dir| dir.ascend.drop(1).any? { |parent| accounted_for.include?(parent) } } end |
#finalizer ⇒ Object
100 101 102 103 104 105 |
# File 'lib/active_support/evented_file_update_checker.rb', line 100 def finalizer proc do stop ActiveSupport::ForkTracker.unregister(@after_fork) end end |
#normalize_dirs! ⇒ Object
138 139 140 141 142 |
# File 'lib/active_support/evented_file_update_checker.rb', line 138 def normalize_dirs! @dirs.transform_keys! do |dir| dir.exist? ? dir.realpath : dir end end |
#restart ⇒ Object
129 130 131 132 |
# File 'lib/active_support/evented_file_update_checker.rb', line 129 def restart stop start end |
#restart? ⇒ Boolean
134 135 136 |
# File 'lib/active_support/evented_file_update_checker.rb', line 134 def restart? @missing.any?(&:exist?) end |
#start ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/active_support/evented_file_update_checker.rb', line 113 def start normalize_dirs! @dtw, @missing = [*@dtw, *@missing].partition(&:exist?) @listener = @dtw.any? ? Listen.to(*@dtw, &method(:changed)) : nil @listener&.start # Wait for the listener to be ready to avoid race conditions # Unfortunately this isn't quite enough on macOS because the Darwin backend # has an extra private thread we can't wait on. @listener&.wait_for_state(:processing_events) end |
#stop ⇒ Object
125 126 127 |
# File 'lib/active_support/evented_file_update_checker.rb', line 125 def stop @listener&.stop end |
#thread_safely ⇒ Object
107 108 109 110 111 |
# File 'lib/active_support/evented_file_update_checker.rb', line 107 def thread_safely @mutex.synchronize do yield self end end |
#watching?(file) ⇒ Boolean
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/active_support/evented_file_update_checker.rb', line 150 def watching?(file) file = Pathname(file) if @files.member?(file) true elsif file.directory? false else ext = file.extname file.dirname.ascend do |dir| matching = @dirs[dir] if matching && (matching.empty? || matching.include?(ext)) break true elsif dir == @common_path || dir.root? break false end end end end |