class Bixby::WebSocket::AsyncResponse

Asynchronously receive a response via some channel

Attributes

id[R]

Public Class Methods

new(id, &block) click to toggle source

Create a new AsyncResponse. Optionally pass a callback block which will be fired when the response is set.

@param [String] id

@yieldparam [JsonResponse] response

@return [AsyncResponse]

# File lib/bixby-common/websocket/async_response.rb, line 20
def initialize(id, &block)
  @id = id
  @block = block
  @mutex = Mutex.new
  @cond = ConditionVariable.new
  @response = nil
  @completed = false
end

Public Instance Methods

completed?() click to toggle source

Has the request completed?

@return [Boolean] true if completed

# File lib/bixby-common/websocket/async_response.rb, line 48
def completed?
  @completed
end
response() click to toggle source

Retrieve the response, blocking until it is available

@return [Object] response data

# File lib/bixby-common/websocket/async_response.rb, line 55
def response
  @mutex.synchronize {
    if !@completed then
      @cond.wait(@mutex)
    end
  }
  return @response
end
response=(obj) click to toggle source

Set the response and signal any blocking threads. Triggers callback, if one was set.

@param [Object] obj result of request, usually a JsonResponse

# File lib/bixby-common/websocket/async_response.rb, line 33
def response=(obj)
  @mutex.synchronize {
    @response = obj
    @completed = true
    @cond.broadcast
  }

  if not @block.nil? then
    @block.call(@response)
  end
end