class Pantry::Communication::WaitList
The WaitList
manages futures for asynchronously waiting for responses from either the Client
or the Server
. Given an identity and a message, WaitList
returns a Future that will be filled when the handler in question receives a message of the same Message
type from that identity.
Constants
- FutureResultWrapper
Internal to Celluloid::Future, using signal ends up in a Result object in which calling value then calls value on the saved data which in our case is
Message
. We just want theMessage
so wrap up our messages in this object to work around this oddity.github.com/celluloid/celluloid/blob/master/lib/celluloid/future.rb#L89
Public Class Methods
# File lib/pantry/communication/wait_list.rb, line 10 def initialize @futures = Hash.new {|hash, key| hash[key] = []} end
Public Instance Methods
# File lib/pantry/communication/wait_list.rb, line 36 def received(message) if future = @futures[ message.uuid ].shift future.signal(FutureResultWrapper.new(message)) end end
Given a message
being sent, create a Future for a response to this message. This keys off of the Message’s UUID, which must be kept in tact as it passes through the system.
# File lib/pantry/communication/wait_list.rb, line 17 def wait_for(message) future = Celluloid::Future.new @futures[ message.uuid ] << future future end
Is there a future waiting for this response message?
# File lib/pantry/communication/wait_list.rb, line 24 def waiting_for?(message) !@futures[ message.uuid ].empty? end