Class: OBS::WebSocket::ResponseDispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/obs/websocket.rb

Instance Method Summary collapse

Constructor Details

#initializeResponseDispatcher

Returns a new instance of ResponseDispatcher.



164
165
166
# File 'lib/obs/websocket.rb', line 164

def initialize
  @ongoing_requests = {}
end

Instance Method Details

#dispatch(message_id, payload) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/obs/websocket.rb', line 180

def dispatch(message_id, payload)
  if h = @ongoing_requests.delete(message_id)
    request = h[:request]
    future = h[:future]

    case payload['status']
    when 'ok'
      response_class = request.class.const_get(:Response)
      future.fulfill(response_class.new(payload))
    when 'error'
      future.reject(RequestError.new(payload['error']))
    else
      fail 'Unknown status'
    end
  end
end

#register(executor, request) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
# File 'lib/obs/websocket.rb', line 168

def register(executor, request)
  message_id = new_message_id
  future = Concurrent::Promises.resolvable_future_on(executor)

  @ongoing_requests[message_id] = {
    request: request,
    future: future,
  }

  [message_id, future.with_hidden_resolvable]
end