Created
November 26, 2011 02:13
-
-
Save clc3123/1394840 to your computer and use it in GitHub Desktop.
thin_async_app_example.ru
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
require "eventmachine" | |
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 | |
class AsyncApp | |
def call(env) | |
body = DeferrableBody.new | |
EventMachine::next_tick { | |
env['async.callback'].call [200, {'Content-Type' => 'text/plain'}, body] | |
body.call ["Hey!\n"] | |
} | |
EventMachine::add_timer(2) { | |
body.call ["Woah, async!\n"] | |
EventMachine::add_timer(3) { | |
body.call ["Cheers then!"] | |
body.succeed | |
} | |
} | |
throw :async # Still works for supporting non-async frameworks... | |
end | |
end | |
run AsyncApp.new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment