1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
# frozen_string_literal: true
module Seahorse
module Client
class AsyncResponse
def initialize(options = {})
@response = Response.new(context: options[:context])
@stream = options[:stream]
@stream_mutex = options[:stream_mutex]
@close_condition = options[:close_condition]
@sync_queue = options[:sync_queue]
end
# @return [RequestContext]
def context
@response.context
end
# @return [StandardError, nil]
def error
@response.error
end
# @overload on(status_code, &block)
# @param [Integer] status_code The block will be
# triggered only for responses with the given status code.
#
# @overload on(status_code_range, &block)
# @param [Range<Integer>] status_code_range The block will be
# triggered only for responses with a status code that falls
# witin the given range.
#
# @return [self]
def on(range, &block)
@response.on(range, &block)
self
end
# @api private
def on_complete(&block)
@response.on_complete(&block)
self
end
# @return [Boolean] Returns `true` if the response is complete with
# no error.
def successful?
@response.error.nil?
end
def wait
if error && context.config.raise_response_errors
raise error
elsif @stream
# have a sync signal that #signal can be blocked on
# else, if #signal is called before #wait
# will be waiting for a signal never arrives
@sync_queue << "sync_signal"
# now #signal is unlocked for
# signaling close condition when ready
@stream_mutex.synchronize {
@close_condition.wait(@stream_mutex)
}
@response
end
end
def join!
if error && context.config.raise_response_errors
raise error
elsif @stream
# close callback is waiting
# for the "sync_signal"
@sync_queue << "sync_signal"
@stream.close
@response
end
end
end
end
end
|