Class: Termplot::MessageBroker
- Inherits:
-
Object
- Object
- Termplot::MessageBroker
- 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
- #close ⇒ Object
- #closed? ⇒ Boolean
- #flush_queue ⇒ Object
-
#initialize(sender:, receiver:) ⇒ MessageBroker
constructor
A new instance of MessageBroker.
- #on_message(&block) ⇒ Object
- #pending_message_count ⇒ Object
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
#close ⇒ Object
90 91 92 |
# File 'lib/termplot/message_broker.rb', line 90 def close queue.close end |
#closed? ⇒ Boolean
94 95 96 |
# File 'lib/termplot/message_broker.rb', line 94 def closed? queue.closed? end |
#flush_queue ⇒ Object
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 (&block) .push(block) end |
#pending_message_count ⇒ Object
79 80 81 |
# File 'lib/termplot/message_broker.rb', line 79 def queue.size end |