Module: ReactManifest::Watcher

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.

Constant Summary collapse

DEBOUNCE_SECONDS =
0.3

Class Method Summary collapse

Class Method Details

.running?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/react_manifest/watcher.rb', line 50

def running?
  !@listener.nil?
end

.start(config = ReactManifest.configuration) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/react_manifest/watcher.rb', line 14

def start(config = ReactManifest.configuration)
  begin
    require "listen"
  rescue LoadError
    log "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 "ux_root does not exist (#{root}) — file watching disabled until directory is created."
    return
  end

  log "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 "File change detected: #{changed.join(', ')}"
    regenerate!(config)
  end

  @listener.start
end

.stopObject



45
46
47
48
# File 'lib/react_manifest/watcher.rb', line 45

def stop
  @listener&.stop
  @listener = nil
end