lib/isomorfeus/transport.rb in isomorfeus-transport-1.0.0.delta10 vs lib/isomorfeus/transport.rb in isomorfeus-transport-1.0.0.delta11
- old
+ new
@@ -6,18 +6,23 @@
def delay(ms = 1000, &block)
`setTimeout(#{block.to_n}, ms)`
end
- def init!
+ def init
@requests_in_progress = { requests: {}, agent_ids: {} }
@socket = nil
- connect if Isomorfeus.on_browser?
+ promise_connect if Isomorfeus.on_browser?
+ true
end
- def connect
- return if @socket && @socket.ready_state < 2
+ def promise_connect
+ promise = Promise.new
+ if @socket && @socket.ready_state < 2
+ promise.resolve(true)
+ return promise
+ end
if Isomorfeus.on_browser?
window_protocol = `window.location.protocol`
ws_protocol = window_protocol == 'https:' ? 'wss:' : 'ws:'
ws_url = "#{ws_protocol}#{`window.location.host`}#{Isomorfeus.api_websocket_path}"
else
@@ -32,21 +37,40 @@
end
@socket.on_message do |event|
json_hash = `Opal.Hash.$new(JSON.parse(event.data))`
Isomorfeus::Transport::ClientProcessor.process(json_hash)
end
- true
+ @socket.on_open do |event|
+ init_promises = []
+ Isomorfeus.transport_init_class_names.each do |constant|
+ result = constant.constantize.send(:init)
+ init_promises << result if result.class == Promise
+ end
+ if init_promises.size > 0
+ Promise.when(*init_promises).then { promise.resolve(true) }
+ else
+ promise.resolve(true)
+ end
+ end
+ promise
end
def disconnect
@socket.close if @socket
@socket = nil
end
def promise_send_path(*path, &block)
request = {}
- path.inject(request) do |memo, key|
- memo[key] = {}
+ inject_path = path[0...-1]
+ last_inject_path_el = inject_path.last
+ last_path_el = path.last
+ inject_path.inject(request) do |memo, key|
+ if key == last_inject_path_el
+ memo[key] = last_path_el
+ else
+ memo[key] = {}
+ end
end
Isomorfeus::Transport.promise_send_request(request, &block)
end
def promise_send_request(request, &block)
\ No newline at end of file