123456789_123456789_123456789_123456789_123456789_

Class: ActiveSupport::EventedFileUpdateChecker::Core

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: activesupport/lib/active_support/evented_file_update_checker.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(files, dirs) ⇒ Core

[ GitHub ]

  
# File 'activesupport/lib/active_support/evented_file_update_checker.rb', line 75

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

Instance Attribute Details

#files (readonly)

[ GitHub ]

  
# File 'activesupport/lib/active_support/evented_file_update_checker.rb', line 73

attr_reader :updated, :files

#restart (readonly)

[ GitHub ]

  
# File 'activesupport/lib/active_support/evented_file_update_checker.rb', line 127

def restart
  stop
  start
end

#restart?Boolean (readonly)

[ GitHub ]

  
# File 'activesupport/lib/active_support/evented_file_update_checker.rb', line 132

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

#updated (readonly)

[ GitHub ]

  
# File 'activesupport/lib/active_support/evented_file_update_checker.rb', line 73

attr_reader :updated, :files

Instance Method Details

#changed(modified, added, removed)

[ GitHub ]

  
# File 'activesupport/lib/active_support/evented_file_update_checker.rb', line 142

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

#common_path(paths)

[ GitHub ]

  
# File 'activesupport/lib/active_support/evented_file_update_checker.rb', line 176

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

#directories_to_watch

[ GitHub ]

  
# File 'activesupport/lib/active_support/evented_file_update_checker.rb', line 170

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

[ GitHub ]

  
# File 'activesupport/lib/active_support/evented_file_update_checker.rb', line 98

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

#normalize_dirs!

[ GitHub ]

  
# File 'activesupport/lib/active_support/evented_file_update_checker.rb', line 136

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

#start

[ GitHub ]

  
# File 'activesupport/lib/active_support/evented_file_update_checker.rb', line 111

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

[ GitHub ]

  
# File 'activesupport/lib/active_support/evented_file_update_checker.rb', line 123

def stop
  @listener&.stop
end

#thread_safely

[ GitHub ]

  
# File 'activesupport/lib/active_support/evented_file_update_checker.rb', line 105

def thread_safely
  @mutex.synchronize do
    yield self
  end
end

#watching?(file) ⇒ Boolean

[ GitHub ]

  
# File 'activesupport/lib/active_support/evented_file_update_checker.rb', line 148

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