Class: Termplot::MessageBrokerPool
- Inherits:
-
Object
- Object
- Termplot::MessageBrokerPool
- Defined in:
- lib/termplot/message_broker.rb
Instance Method Summary collapse
- #broker(sender:, receiver:) ⇒ Object
- #closed? ⇒ Boolean
- #empty? ⇒ Boolean
- #flush_messages ⇒ Object
-
#initialize ⇒ MessageBrokerPool
constructor
A new instance of MessageBrokerPool.
- #on_message(&block) ⇒ Object
- #pending_message_count ⇒ Object
- #shutdown ⇒ Object
Constructor Details
#initialize ⇒ MessageBrokerPool
Returns a new instance of MessageBrokerPool.
5 6 7 8 9 |
# File 'lib/termplot/message_broker.rb', line 5 def initialize @brokers = [] @mutex = Mutex.new @on_message_callbacks = [] end |
Instance Method Details
#broker(sender:, receiver:) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/termplot/message_broker.rb', line 11 def broker(sender:, receiver:) mutex.synchronize do broker = MessageBroker.new(sender: sender, receiver: receiver) broker. do |v| .each do |block| block.call(v) end end brokers.push(broker) broker end end |
#closed? ⇒ Boolean
30 31 32 33 34 |
# File 'lib/termplot/message_broker.rb', line 30 def closed? mutex.synchronize do (brokers.count == 0) || brokers.all?(&:closed?) end end |
#empty? ⇒ Boolean
56 57 58 |
# File 'lib/termplot/message_broker.rb', line 56 def empty? == 0 end |
#flush_messages ⇒ Object
42 43 44 45 46 |
# File 'lib/termplot/message_broker.rb', line 42 def mutex.synchronize do brokers.each(&:flush_queue) end end |
#on_message(&block) ⇒ Object
24 25 26 27 28 |
# File 'lib/termplot/message_broker.rb', line 24 def (&block) mutex.synchronize do .push(block) end end |
#pending_message_count ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/termplot/message_broker.rb', line 48 def mutex.synchronize do brokers.inject(0) do |sum, broker| sum + broker. end end end |
#shutdown ⇒ Object
36 37 38 39 40 |
# File 'lib/termplot/message_broker.rb', line 36 def shutdown mutex.synchronize do brokers.each(&:close) end end |