Module: ReactManifest::Watcher

Extended by:
Logging
Defined in:
lib/react_manifest/watcher.rb

Overview

Watches the ux/ directory tree for file changes and triggers manifest regeneration automatically.

Uses the ‘listen` gem (soft dependency — degrades gracefully if not available). Auto-started in development via the Railtie initializer.

Watches ux_root recursively so newly added controller directories are picked up without a server restart.

File-change callbacks are debounced by the listen gem and handled on a background thread. Rapid back-to-back changes are coalesced: if a regeneration is already in progress when a new change arrives, only one additional regeneration is queued (not one per file event).

Constant Summary collapse

DEBOUNCE_SECONDS =
0.3

Class Method Summary collapse

Methods included from Logging

log_debug, log_info, log_warn

Class Method Details

.reset_regen_state!Object

Kill the background regen thread and reset all regen state. Intended for use in tests only.



67
68
69
70
71
72
73
74
75
76
# File 'lib/react_manifest/watcher.rb', line 67

def reset_regen_state!
  thread = @regen_thread
  if thread&.alive?
    thread.kill
    thread.join(1)
  end
  @regen_thread  = nil
  @regen_pending = false
  @regen_mutex   = nil
end

.running?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/react_manifest/watcher.rb', line 61

def running?
  !@listener.nil?
end

.start(config = ReactManifest.configuration) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/react_manifest/watcher.rb', line 21

def start(config = ReactManifest.configuration)
  begin
    require "listen"
  rescue LoadError
    log_warn "listen gem not available — file watching disabled. " \
             "Add `gem 'listen'` to the development group in your Gemfile."
    return
  end

  root = config.abs_ux_root

  unless Dir.exist?(root)
    log_warn "ux_root does not exist (#{root}) — file watching disabled until directory is created."
    return
  end

  @regen_mutex = Mutex.new

  log_info "Watching #{root.sub("#{Rails.root}/", '')} for changes..."

  @listener = Listen.to(
    root,
    only: config.extensions_pattern,
    latency: DEBOUNCE_SECONDS
  ) do |modified, added, removed|
    changed = (modified + added + removed).map { |f| File.basename(f) }
    log_info "File change detected: #{changed.join(', ')}"
    handle_file_changes(modified, added, removed, config)
  end

  @listener.start
end

.stopObject



54
55
56
57
58
59
# File 'lib/react_manifest/watcher.rb', line 54

def stop
  @listener&.stop
  @listener = nil
  @regen_thread&.join(5)
  @regen_thread = nil
end