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) if request = @requests.delete(id) request.result.value = result else raise "no request with id '#{id.inspect}'" end end def wait_for_results_through(iteration) @wait = iteration.respond_to?(:call) ? iteration : proc{ @hub.handle_message } end end end