Class: MightyTest::Watcher::EventQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/mighty_test/watcher/event_queue.rb

Instance Method Summary collapse

Constructor Details

#initialize(console: Console.new, file_system: FileSystem.new) ⇒ EventQueue

Returns a new instance of EventQueue.



6
7
8
9
10
# File 'lib/mighty_test/watcher/event_queue.rb', line 6

def initialize(console: Console.new, file_system: FileSystem.new)
  @console = console
  @file_system = file_system
  @file_system_queue = Thread::Queue.new
end

Instance Method Details

#popObject



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mighty_test/watcher/event_queue.rb', line 12

def pop
  console.with_raw_input do
    until stopped?
      if (key = console.read_keypress_nonblock)
        return [:keypress, key]
      end
      if (paths = pop_files_changed)
        return [:file_system_changed, paths]
      end
    end
  end
end

#restartObject



35
36
37
38
# File 'lib/mighty_test/watcher/event_queue.rb', line 35

def restart
  stop
  start
end

#startObject



25
26
27
28
29
30
31
32
33
# File 'lib/mighty_test/watcher/event_queue.rb', line 25

def start
  raise "Already started" unless stopped?

  @file_system_listener = file_system.listen do |modified, added, _removed|
    paths = [*modified, *added].uniq
    file_system_queue.push(paths) unless paths.empty?
  end
  true
end

#stopObject



40
41
42
43
# File 'lib/mighty_test/watcher/event_queue.rb', line 40

def stop
  file_system_listener&.stop
  @file_system_listener = nil
end

#stopped?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/mighty_test/watcher/event_queue.rb', line 45

def stopped?
  !file_system_listener
end