2012-05-04 20:45:47 +00:00
|
|
|
require 'eventmachine'
|
|
|
|
|
|
|
|
module Rack
|
|
|
|
class EMStream
|
|
|
|
include EventMachine::Deferrable
|
|
|
|
|
|
|
|
def initialize(app)
|
|
|
|
@app = app
|
|
|
|
end
|
|
|
|
|
|
|
|
def each(&b)
|
|
|
|
@callback = b
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
|
|
|
dup._call(env)
|
|
|
|
end
|
|
|
|
|
|
|
|
def _call(env)
|
|
|
|
result = @app.call(env)
|
|
|
|
|
|
|
|
EM.next_tick {
|
|
|
|
env['async.callback'].call [ result[0], result[1], self ]
|
|
|
|
|
2012-08-29 17:33:50 +00:00
|
|
|
result[2].close if result[2].respond_to?(:close)
|
|
|
|
|
2012-05-04 20:45:47 +00:00
|
|
|
result[2].each { |data| EM.next_tick { @callback.call(data) } }
|
|
|
|
EM.next_tick { succeed }
|
|
|
|
}
|
|
|
|
|
|
|
|
throw :async
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|