-
-
Save raggi/26404 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env thin -e production -R config.ru -V start | |
# config.ru | |
# N.B. Rack::Lint doesn't yet support the api, use production mode rack. | |
require 'eventmachine' | |
class AsyncApp | |
class DeferrableBody | |
include EventMachine::Deferrable | |
def call(body) | |
body.each do |chunk| | |
@body_callback.call(chunk) | |
end | |
end | |
def each &blk | |
@body_callback = blk | |
end | |
end | |
# This is a template async response. N.B. Can't use string for body on 1.9 | |
AsyncResponse = [-1, {}, []].freeze | |
def call(env) | |
body = DeferrableBody.new | |
EventMachine::next_tick { env['async.callback'].call [200, {'Content-Type' => 'text/plain'}, body] } | |
EventMachine::add_periodic_timer(3) do | |
body.call ["Cheers then!\n"] | |
# body.succeed | |
end | |
AsyncResponse | |
end | |
end | |
run AsyncApp.new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment