1 # based on async_examples/async_app.ru by James Tucker
2 class DeferrableChunkBody
3 include EventMachine::Deferrable
7 @body_callback.call("#{chunk.size.to_s(16)}\r\n")
8 @body_callback.call(chunk)
9 @body_callback.call("\r\n")
14 @body_callback = block
18 @body_callback.call("0\r\n\r\n")
20 end if defined?(EventMachine)
25 'Content-Type' => 'text/plain',
26 'Transfer-Encoding' => 'chunked',
28 delay = env["HTTP_X_DELAY"].to_i
30 case env["rainbows.model"]
31 when :EventMachine, :NeverBlock
32 body = DeferrableChunkBody.new
33 body.callback { body.finish }
35 env['async.callback'].call([ 200, headers, body ])
40 body.call "World #{env['PATH_INFO']}\n"
45 delay == 0 ? EM.next_tick(&task) : EM.add_timer(delay, &task)
47 # Cool.io only does one-shot responses due to the lack of the
48 # equivalent of EM::Deferrables
49 body = [ "Hello ", "World #{env['PATH_INFO']}\n", '' ].map do |chunk|
50 "#{chunk.size.to_s(16)}\r\n#{chunk}\r\n"
53 next_tick = Coolio::TimerWatcher.new(delay, false)
54 next_tick.on_timer { env['async.callback'].call([ 200, headers, body ]) }
55 next_tick.attach(Coolio::Loop.default)
57 raise "Not supported: #{env['rainbows.model']}"