Class: Klenod::Build::Watcher

Inherits:
Object
  • Object
show all
Defined in:
lib/klenod/build/watcher.rb

Defined Under Namespace

Classes: PendingChanges

Instance Method Summary collapse

Constructor Details

#initialize(source_dir:, context:, debounce_interval: 0.1) ⇒ Watcher

Returns a new instance of Watcher.



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/klenod/build/watcher.rb', line 56

def initialize(source_dir:, context:, debounce_interval: 0.1)
  @source_dir = source_dir
  @context = context
  @debounce_interval = debounce_interval
  @graph_version = 0
  @pending_changes = PendingChanges.new
  @pending_mutex = Mutex.new
  @pending_condition = ConditionVariable.new
  @last_change_at = nil
  @stopping = false
end

Instance Method Details

#startObject



68
69
70
71
72
73
74
75
# File 'lib/klenod/build/watcher.rb', line 68

def start
  @worker_thread = Thread.new { process_pending_updates }
  @listener =
    Listen.to(@source_dir) do |modified, added, removed|
      enqueue_update(modified + added, removed)
    end
  @listener.start
end

#stopObject



77
78
79
80
81
82
83
84
# File 'lib/klenod/build/watcher.rb', line 77

def stop
  @listener&.stop
  @pending_mutex.synchronize do
    @stopping = true
    @pending_condition.broadcast
  end
  @worker_thread&.join
end