Class: Bixby::WebSocket::AsyncResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/bixby-common/websocket/async_response.rb

Overview

Asynchronously receive a response via WebSocket channel

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ AsyncResponse

Returns a new instance of AsyncResponse.



12
13
14
15
16
17
18
# File 'lib/bixby-common/websocket/async_response.rb', line 12

def initialize(id)
  @id = id
  @mutex = Mutex.new
  @cond = ConditionVariable.new
  @response = nil
  @completed = false
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



10
11
12
# File 'lib/bixby-common/websocket/async_response.rb', line 10

def id
  @id
end

Instance Method Details

#completed?Boolean

Has the request completed?

Returns:

  • (Boolean)

    true if completed



34
35
36
# File 'lib/bixby-common/websocket/async_response.rb', line 34

def completed?
  @completed
end

#responseObject

Retrieve the response, blocking until it is available

Returns:

  • (Object)

    response data



41
42
43
44
45
# File 'lib/bixby-common/websocket/async_response.rb', line 41

def response
  return @response if @completed
  @mutex.synchronize { @cond.wait(@mutex) }
  return @response
end

#response=(obj) ⇒ Object

Set the response and signal any blocking threads

Parameters:

  • obj (Object)

    result of request, usually a JsonResponse



23
24
25
26
27
28
29
# File 'lib/bixby-common/websocket/async_response.rb', line 23

def response=(obj)
  @mutex.synchronize {
    @completed = true
    @response = obj
    @cond.signal
  }
end