lib/isomorfeus/transport.rb in isomorfeus-transport-2.5.5 vs lib/isomorfeus/transport.rb in isomorfeus-transport-22.9.0.rc1

- old
+ new

@@ -6,43 +6,38 @@ attr_accessor :socket def init @socket = nil - promise_connect if Isomorfeus.on_browser? || Isomorfeus.on_mobile? + @actions = [] + @actions_promise = Promise.new + promise_connect true end def promise_connect(session_id = nil, promise = nil) promise = Promise.new unless promise if @socket && @socket.ready_state < 2 promise.resolve(true) return promise end - if 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 - ws_url = "#{Isomorfeus::TopLevel.transport_ws_url}" - end + + window_protocol = `window.location.protocol` + ws_protocol = window_protocol == 'https:' ? 'wss:' : 'ws:' + ws_url = "#{ws_protocol}//#{`window.location.host`}#{Isomorfeus.api_websocket_path}" + headers = (session_id.nil? || session_id.empty?) ? nil : { 'Cookie': "session=#{session_id}" } @socket = Isomorfeus::Transport::WebsocketClient.new(ws_url, nil, headers) @socket.on_error do |error| - if on_browser? - `console.warn('Isomorfeus::Transport: Will try again, but so far error connecting:', error)` - @socket.close - after 1000 do - Isomorfeus::Transport.promise_connect(session_id, promise) - end - else - Isomorfeus.raise_error(message: error.JS[:message], stack: error.JS[:stack]) - promise.reject + `console.warn('Isomorfeus::Transport: Will try again, but so far error connecting:', error)` + @socket.close + after 1000 do + Isomorfeus::Transport.promise_connect(session_id, promise) end end @socket.on_message do |event| - json_hash = `Opal.Hash.$new(JSON.parse(event.data))` + json_hash = 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| @@ -57,11 +52,11 @@ requests_in_progress[:requests].each_key do |request| agent = get_agent_for_request_in_progress(request) open_promise.then { promise_send_request(request) } if agent && !agent.sent end open_promise.then { promise.resolve(true) } - keep_session_alive if on_browser? + keep_session_alive end promise end def keep_session_alive @@ -74,55 +69,67 @@ def disconnect @socket.close if @socket @socket = nil end - def promise_send_path(*path, &block) + def promise_send_path(*path) request = {} 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 + if last_path_el.is_a?(Hash) + memo[key] = last_path_el + else + memo[key] = { last_path_el => nil } + end else memo[key] = {} end end - Isomorfeus::Transport.promise_send_request(request, &block) + self.promise_send_request(request) end - def promise_send_request(request, &block) + def promise_send_action(action) + @actions.push(action) + if @actions.length == 1 + after(0) do + acts = @actions + @actions = [] + pr = @actions_promise + @actions_promise = Promise.new + promise_send_request('Isomorfeus::Redux::Handler' => acts).then do |agnt| + pr.resolve(agnt) + end + end + end + @actions_promise + end + + def promise_send_request(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 + unless agent.queued || agent.sent + agent.queued = true register_request_in_progress(request, agent.id) - Isomorfeus.raise_error(message: 'No socket!') unless @socket begin - @socket.send(`JSON.stringify(#{{request: { agent_ids: { agent.id => request }}}.to_n})`) + Isomorfeus.raise_error(message: 'No socket!') unless @socket + @socket.send(JSON.dump({request: { agent_ids: { agent.id => request }}})) agent.sent = true - after(Isomorfeus.on_ssr? ? 5000 : 20000) do + after(20000) do unless agent.promise.realized? agent.promise.reject({agent_response: { error: 'Request timeout!' }, full_response: {}}) end end rescue @socket.close - after (Isomorfeus.on_ssr? ? 10 : 3000) do + after(3000) do @reconnect = true - if on_browser? - Isomorfeus::Transport.promise_connect - else - Isomorfeus.raise_error(message: 'Transport lost connection!') - end + Isomorfeus::Transport.promise_connect end end end agent.promise end