Class: SorbetView::FileSystem::FileWatcher

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/sorbet_view/file_system/file_watcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(config:, &on_change) ⇒ FileWatcher

Returns a new instance of FileWatcher.



17
18
19
20
21
# File 'lib/sorbet_view/file_system/file_watcher.rb', line 17

def initialize(config:, &on_change)
  @config = config
  @on_change = on_change
  @listener = T.let(nil, T.untyped)
end

Instance Method Details

#startObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sorbet_view/file_system/file_watcher.rb', line 24

def start
  dirs = (@config.input_dirs + @config.component_dirs + @config.controller_dirs).uniq.select { |d| Dir.exist?(d) }
  return if dirs.empty?

  @listener = Listen.to(
    *dirs,
    only: /\.(erb|rb)$/,
    wait_for_delay: 0.1
  ) do |modified, added, removed|
    # Filter out excluded paths; for .rb files, only pass those containing erb_template
    modified = filter_paths(modified)
    added = filter_paths(added)
    removed = filter_paths(removed)

    next if modified.empty? && added.empty? && removed.empty?

    @on_change.call(modified, added, removed)
  end

  @listener.start
end

#stopObject



47
48
49
# File 'lib/sorbet_view/file_system/file_watcher.rb', line 47

def stop
  @listener&.stop
end