Class: Opal::Rails::WatchRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/opal/rails/watch_runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(config:, resolver: nil, builder_runner: nil, manifest: nil, file_watcher_class: FileWatcher, output: $stdout, error_output: $stderr, kernel: Kernel) ⇒ WatchRunner

Returns a new instance of WatchRunner.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/opal/rails/watch_runner.rb', line 8

def initialize(config:, resolver: nil, builder_runner: nil, manifest: nil, file_watcher_class: FileWatcher,
               output: $stdout, error_output: $stderr, kernel: Kernel)
  @config = config
  @resolver = resolver
  @builder_runner = builder_runner
  @manifest = manifest
  @file_watcher_class = file_watcher_class
  @output = output
  @error_output = error_output
  @kernel = kernel
  @dependencies_by_entrypoint = {}
  @outputs_by_entrypoint = {}
  @reverse_dependencies = Hash.new { |hash, key| hash[key] = [] }
  @opal_dependencies = []
end

Instance Method Details

#process_changes(modified:, added:, removed:) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/opal/rails/watch_runner.rb', line 38

def process_changes(modified:, added:, removed:)
  modified = normalize_paths(modified)
  added = normalize_paths(added)
  removed = normalize_paths(removed)
  changed = (modified + added + removed).uniq.sort
  return if changed.empty?

  if full_rebuild_required?(changed: changed, modified: modified, added: added, removed: removed)
    error_output.puts "* Modified code: #{changed.join(', ')}; rebuilding all entrypoints"
    rebuild_all!
  else
    logical_names = modified.flat_map { |path| reverse_dependencies[path] }.uniq.sort
    return if logical_names.empty?

    error_output.puts "* Modified code: #{modified.join(', ')}; rebuilding #{logical_names.join(', ')}"
    rebuild_entrypoints!(logical_names)
  end
end

#start!Object



34
35
36
# File 'lib/opal/rails/watch_runner.rb', line 34

def start!
  rebuild_all!
end

#watchObject



24
25
26
27
28
29
30
31
32
# File 'lib/opal/rails/watch_runner.rb', line 24

def watch
  start!
  output.puts '* Opal watcher started'
  kernel.sleep
rescue Interrupt
  output.puts '* Stopping Opal watcher...'
ensure
  watcher&.stop
end