Class: Termplot::Consumers::BaseConsumer

Inherits:
Object
  • Object
show all
Defined in:
lib/termplot/consumers/base_consumer.rb

Direct Known Subclasses

MultiSourceConsumer, SingleSourceConsumer

Defined Under Namespace

Classes: ProducerPool, RendererThread

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ BaseConsumer

Returns a new instance of BaseConsumer.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/termplot/consumers/base_consumer.rb', line 10

def initialize(options)
  @options = options
  @broker_pool = MessageBrokerPool.new
  @producer_pool = ProducerPool.new
  @renderer = Renderer.new(
    cols: options.cols,
    rows: options.rows,
    widgets: positioned_widgets,
    debug: options.debug
  )

  @renderer_thread = RendererThread.new(
    renderer: renderer,
    broker_pool: broker_pool
  )
end

Instance Method Details

#runObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/termplot/consumers/base_consumer.rb', line 27

def run
  register_producers_and_brokers
  broker_pool.on_message { renderer_thread.continue }

  Shell.init(clear: options.full_screen)
  renderer_thread.start

  # Blocks main thread to produce values from the producer pool
  producer_pool.start_and_block

  # At this point producer threads have all exited, tell renderer to
  # consume all messages left on the queue
  renderer_thread.continue while !broker_pool.empty?

  # Close queues
  broker_pool.shutdown

  # Shutdown renderer
  renderer_thread.join
end