Class: Termplot::MessageBroker

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

Overview

Broker messages in a thread-safe way between a sender and a receiver.

Instance Method Summary collapse

Constructor Details

#initialize(sender:, receiver:) ⇒ MessageBroker

Returns a new instance of MessageBroker.



66
67
68
69
70
71
72
73
# File 'lib/termplot/message_broker.rb', line 66

def initialize(sender:, receiver:)
  @sender = sender
  @receiver = receiver
  @queue = Queue.new
  @on_message_callbacks = []

  register_callbacks
end

Instance Method Details

#closeObject



90
91
92
# File 'lib/termplot/message_broker.rb', line 90

def close
  queue.close
end

#closed?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/termplot/message_broker.rb', line 94

def closed?
  queue.closed?
end

#flush_queueObject



83
84
85
86
87
88
# File 'lib/termplot/message_broker.rb', line 83

def flush_queue
  num_samples = queue.size
  num_samples.times do
    receiver << queue.shift
  end
end

#on_message(&block) ⇒ Object



75
76
77
# File 'lib/termplot/message_broker.rb', line 75

def on_message(&block)
  on_message_callbacks.push(block)
end

#pending_message_countObject



79
80
81
# File 'lib/termplot/message_broker.rb', line 79

def pending_message_count
  queue.size
end