class ActiveSupport::EventedFileUpdateChecker::Core

def changed(modified, added, removed)

def changed(modified, added, removed)
  unless @updated.true?
    @updated.make_true if (modified + added + removed).any? { |f| watching?(f) }
  end
end

def common_path(paths)

def common_path(paths)
  paths.map { |path| path.ascend.to_a }.reduce(&:&)&.first
end

def directories_to_watch

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

def finalizer

def finalizer
  proc do
    stop
    ActiveSupport::ForkTracker.unregister(@after_fork)
  end
end

def initialize(files, dirs)

def initialize(files, dirs)
  @files = files.map { |file| Pathname(file).expand_path }.to_set
  @dirs = dirs.each_with_object({}) do |(dir, exts), hash|
    hash[Pathname(dir).expand_path] = 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

def normalize_dirs!

def normalize_dirs!
  @dirs.transform_keys! do |dir|
    dir.exist? ? dir.realpath : dir
  end
end

def restart

def restart
  stop
  start
end

def restart?

def restart?
  @missing.any?(&:exist?)
end

def start

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

def stop

def stop
  @listener&.stop
end

def thread_safely

def thread_safely
  @mutex.synchronize do
    yield self
  end
end

def watching?(file)

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