Class: ParallelRender::Middleware
- Inherits:
-
Object
- Object
- ParallelRender::Middleware
- Defined in:
- lib/parallel_render/middleware.rb
Constant Summary collapse
- HTML_TYPE =
%r{\Atext/html}.freeze
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app) ⇒ Middleware
constructor
A new instance of Middleware.
Constructor Details
#initialize(app) ⇒ Middleware
Returns a new instance of Middleware.
16 17 18 |
# File 'lib/parallel_render/middleware.rb', line 16 def initialize(app) @app = app end |
Instance Method Details
#call(env) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/parallel_render/middleware.rb', line 20 def call(env) status, headers, body = @app.call(env) if html?(headers) html = +"" body.each { |part| html << part } (ParallelRender::Current.async_futures || {}).each do |token, future| html_fragment = wait_or_empty(future) placeholder = "<!--ASYNC-PLACEHOLDER:#{token}-->" html.gsub!(placeholder, html_fragment) end headers["Content-Length"] = html.bytesize.to_s if headers["Content-Length"] ParallelRender::Current.async_futures = {} [status, headers, [html]] else [status, headers, body] end end |