Class: Lilac::CLI::Watcher

Inherits:
Object
  • Object
show all
Defined in:
lib/lilac/cli/watcher.rb

Overview

Thin wrapper around listen that coalesces bursts of file events into a single on_change callback. Editors that "save" via write-then-rename or "mtime touch" frequently emit 2–5 events in rapid succession; without debouncing we would rebuild that many times.

The implementation timer-resets on each event: after the most recent event, we wait debounce seconds of quiet, then fire.

Constant Summary collapse

DEFAULT_DEBOUNCE =
0.15

Instance Method Summary collapse

Constructor Details

#initialize(paths, debounce: DEFAULT_DEBOUNCE, &on_change) ⇒ Watcher

Returns a new instance of Watcher.

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
27
# File 'lib/lilac/cli/watcher.rb', line 18

def initialize(paths, debounce: DEFAULT_DEBOUNCE, &on_change)
  raise ArgumentError, "block required" unless on_change

  @paths = paths
  @debounce = debounce
  @on_change = on_change
  @listener = nil
  @pending = nil
  @mutex = Mutex.new
end

Instance Method Details

#startObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/lilac/cli/watcher.rb', line 29

def start
  # No `only:` filter: callers pass `components/`, `pages/`, and
  # `public/`, and the public mirror needs to react to arbitrary
  # static assets (.js, .css, .wasm, images). Listen's default
  # ignore list already filters .git, swap files, OS metadata.
  @listener = Listen.to(*@paths) do |_modified, _added, _removed|
    schedule_change
  end
  @listener.start
end

#stopObject



40
41
42
43
# File 'lib/lilac/cli/watcher.rb', line 40

def stop
  @listener&.stop
  @mutex.synchronize { @pending&.kill }
end