lib/isomorfeus/transport.rb in isomorfeus-transport-1.0.0.zeta11 vs lib/isomorfeus/transport.rb in isomorfeus-transport-1.0.0.zeta12

- old
+ new

@@ -3,16 +3,17 @@ class << self if RUBY_ENGINE == 'opal' attr_accessor :socket def delay(ms = 1000, &block) - `setTimeout(#{block.to_n}, ms)` + `setTimeout(#{block.to_n}, #{ms})` end def init @requests_in_progress = { requests: {}, agent_ids: {} } @socket = nil + @initialized = false promise_connect if Isomorfeus.on_browser? true end def promise_connect @@ -29,28 +30,35 @@ ws_url = Isomorfeus::TopLevel.transport_ws_url end @socket = Isomorfeus::Transport::Websocket.new(ws_url) @socket.on_error do @socket.close - delay do + delay 1000 do Isomorfeus::Transport.promise_connect end end @socket.on_message do |event| json_hash = `Opal.Hash.$new(JSON.parse(event.data))` Isomorfeus::Transport::ClientProcessor.process(json_hash) end @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 + if @initialized + requests_in_progress[:requests].each_key do |request| + agent = get_agent_for_request_in_progress(request) + promise_send_request(request) if agent && !agent.sent + end promise.resolve(true) + else + @initialized = true + 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) } + end end end promise end @@ -73,24 +81,34 @@ end Isomorfeus::Transport.promise_send_request(request, &block) end def promise_send_request(request, &block) - if request_in_progress?(request) - agent = get_agent_for_request_in_progress(request) - else - agent = Isomorfeus::Transport::RequestAgent.new(request) + agent = if request_in_progress?(request) + get_agent_for_request_in_progress(request) + else + Isomorfeus::Transport::RequestAgent.new(request) + end + unless agent.sent if block_given? agent.promise.then do |response| block.call(response) end end register_request_in_progress(request, agent.id) raise 'No socket!' unless @socket - @socket.send(`JSON.stringify(#{{request: { agent_ids: { agent.id => request }}}.to_n})`) - delay(Isomorfeus.on_ssr? ? 8000 : 20000) do - unless agent.promise.realized? - agent.promise.reject({agent_response: { error: 'Request timeout!' }, full_response: {}}) + begin + @socket.send(`JSON.stringify(#{{request: { agent_ids: { agent.id => request }}}.to_n})`) + agent.sent = true + delay(Isomorfeus.on_ssr? ? 8000 : 20000) do + unless agent.promise.realized? + agent.promise.reject({agent_response: { error: 'Request timeout!' }, full_response: {}}) + end + end + rescue + @socket.close + delay 5000 do + Isomorfeus::Transport.promise_connect end end end agent.promise end