module PipeRpc class Hub::Requests def initialize(hub) @hub = hub @requests ={} @uid = 0 wait_for_results_through nil end attr_reader :requests def evaluate(request) @uid += 1 request.id = @uid @requests[@uid] = request @hub.socket.write request @wait.call until request.result.received? request.result.value end def [](id) @requests[id] end def inject_result_for(id, result) request = @requests.delete(id) raise NoRequestForResponseError.new("no request with id '#{id.inspect}'") unless request request.result.value = result end def wait_for_results_through(iteration) @wait = iteration.respond_to?(:call) ? iteration : proc{ @hub.handle_message } end end end