Class: ReactOnRailsPro::StreamRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/react_on_rails_pro/stream_request.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&request_block) ⇒ StreamRequest

Returns a new instance of StreamRequest.



89
90
91
# File 'lib/react_on_rails_pro/stream_request.rb', line 89

def initialize(&request_block)
  @request_executor = request_block
end

Class Method Details

.create(&request_block) ⇒ Object

Method to start the decoration



127
128
129
# File 'lib/react_on_rails_pro/stream_request.rb', line 127

def self.create(&request_block)
  StreamDecorator.new(new(&request_block))
end

Instance Method Details

#each_chunk(&block) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/react_on_rails_pro/stream_request.rb', line 95

def each_chunk(&block)
  return enum_for(:each_chunk) unless block

  Sync do
    barrier = Async::Barrier.new

    send_bundle = false
    error_body = +""
    loop do
      stream_response = @request_executor.call(send_bundle, barrier)

      # The Node renderer always emits the length-prefixed wire format
      # (`<metadata JSON>\t<content byte length hex>\n<raw content bytes>`)
      # for every response chunk — both the one-shot streaming path and the
      # incremental-rendering path. We check the status code inside the loop
      # block because calling `status` outside of it blocks until the full
      # response has been received. See the `status` spec in
      # `spec/react_on_rails_pro/stream_spec.rb` for more details.
      process_response_chunks(stream_response, error_body, &block)
      break
    rescue HTTPX::HTTPError => e
      send_bundle = handle_http_error(e, error_body, send_bundle)
    rescue HTTPX::ReadTimeoutError => e
      raise ReactOnRailsPro::Error, "Time out error while server side render streaming a component.\n" \
                                    "Original error:\n#{e}\n#{e.backtrace}"
    end

    barrier.wait
  end
end