Class: RSMP::Queue
- Inherits:
-
Object
show all
- Includes:
- Receiver
- Defined in:
- lib/rsmp/collect/queue.rb
Overview
Receives items from a Distributor and keeps them in a queue.
The client can wait for messages and will get them one by one.
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Receiver
#accept_message?, #initialize_receiver, #inspect, #receive, #receive_event, #reject_message?, #start_receiving, #stop_receiving
Constructor Details
#initialize(distributor, filter: nil) ⇒ Queue
10
11
12
13
14
|
# File 'lib/rsmp/collect/queue.rb', line 10
def initialize(distributor, filter: nil)
initialize_receiver distributor, filter: filter
@condition = Async::Notification.new
clear
end
|
Instance Attribute Details
#messages ⇒ Object
Returns the value of attribute messages.
8
9
10
|
# File 'lib/rsmp/collect/queue.rb', line 8
def messages
@messages
end
|
Instance Method Details
#clear ⇒ Object
16
17
18
|
# File 'lib/rsmp/collect/queue.rb', line 16
def clear
@messages = []
end
|
#handle_message(message) ⇒ Object
42
43
44
45
|
# File 'lib/rsmp/collect/queue.rb', line 42
def handle_message(message)
@messages << message
@condition.signal
end
|
#wait_for_message(timeout: nil) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/rsmp/collect/queue.rb', line 20
def wait_for_message(timeout: nil)
if @messages.empty?
if timeout
Async::Task.current.with_timeout(timeout) { @condition.wait }
else
@condition.wait
end
end
Result.success(@messages.shift)
rescue Async::TimeoutError => e
Result.failure(
:timeout,
message: "No message was received within #{timeout}s",
source: :timeout,
cause: e
)
end
|
#wait_for_message! ⇒ Object
38
39
40
|
# File 'lib/rsmp/collect/queue.rb', line 38
def wait_for_message!(...)
wait_for_message(...).value!
end
|