Class: Adsf::Live::Watcher

Inherits:
Object
  • Object
show all
Defined in:
lib/adsf/live/watcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(root_dir:) ⇒ Watcher

Returns a new instance of Watcher.



6
7
8
9
10
11
12
# File 'lib/adsf/live/watcher.rb', line 6

def initialize(root_dir:)
  unless Pathname.new(root_dir).absolute?
    raise ArgumentError, 'Watcher#initialize: The root_path argument must be an absolute path'
  end

  @root_dir = root_dir
end

Instance Method Details

#handle_changes(server, chs) ⇒ Object



45
46
47
48
49
# File 'lib/adsf/live/watcher.rb', line 45

def handle_changes(server, chs)
  prefix_length = @root_dir.length
  paths = chs.map { |pa| pa[prefix_length..-1] }
  server.reload(paths)
end

#startObject



14
15
16
17
# File 'lib/adsf/live/watcher.rb', line 14

def start
  @server = start_server
  @listener = start_listener(@server)
end

#start_listener(server) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/adsf/live/watcher.rb', line 31

def start_listener(server)
  options = {
    latency: 0.0,
    wait_for_delay: 0.0,
  }

  listener =
    Listen.to(@root_dir, options) do |ch_mod, ch_add, ch_del|
      handle_changes(server, [ch_mod, ch_add, ch_del].flatten)
    end
  listener.start
  listener
end

#start_serverObject



24
25
26
27
28
29
# File 'lib/adsf/live/watcher.rb', line 24

def start_server
  ::Adsf::Live::WebSocketServer.new(
    host: '0.0.0.0',
    port: '35729',
  )
end

#stopObject



19
20
21
22
# File 'lib/adsf/live/watcher.rb', line 19

def stop
  @listener&.stop
  @server&.stop
end