Method: Nitro::Render#stream

Defined in:
lib/nitro/cgi/stream.rb

#stream(io = nil) ⇒ Object

Enable streaming mode for the current HTTP Response. You can optionally provide an existing IO object for streaming. – This code is considered a hack fix. But it still is useful so for the moment it stays in the distribution. ++



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/nitro/cgi/stream.rb', line 15

def stream(io = nil)
  if io
    # Reuse an existing IO if it exists.
    @context.out = io
  else  
    r, w = IO.pipe
    
    @context.out = r
    @out = w
    r.sync = true    
    w.class.send(:define_method, :empty?) { false }

    Thread.new do 
      begin
        yield
      ensure
  w.close
      end
    end
  end
end