Skip to content

Commit

Permalink
added catch async and throw async
Browse files Browse the repository at this point in the history
  • Loading branch information
rkh committed Mar 12, 2010
1 parent 817b7e1 commit 654edbb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/async_rack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ def autoload(class_name, path)
module ExtensionMixin
::AsyncRack.extend self
def autoload(class_name, path)
super
if Rack.autoload? class_name then Rack.autoload(class_name, path)
elsif Rack.const_defined? class_name then require path
if Rack.const_defined? class_name
require path
else
Rack.autoload class_name, path
super
end
end
end

# New middleware
autoload :CatchAsync, "async_rack/catch_async"
autoload :ThrowAsync, "async_rack/throw_async"

# Wrapped rack middleware
autoload :Chunked, "async_rack/chunked"
autoload :CommonLogger, "async_rack/commonlogger"
Expand Down
16 changes: 16 additions & 0 deletions lib/async_rack/catch_async.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'rack'
module AsyncRack
class CatchAsync
Rack::CatchAsync = self unless defined? Rack::CatchAsync

def initialize(app, async_response = [-1, {}, []])
@app, @async_response = app, async_response
end

def call(env)
response = @async_response
catch(:async) { response = @app.call env }
response
end
end
end
17 changes: 17 additions & 0 deletions lib/async_rack/throw_async.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'rack'

module AsyncRack
class ThrowAsync
Rack::ThrowAsync = self unless defined? Rack::ThrowAsync

def initialize(app, throw_on = [-1, 0])
@app, @throw_on = app, throw_on
end

def call(env)
response = @app.call env
throw :async if @throw_on.include? response.first
response
end
end
end

0 comments on commit 654edbb

Please sign in to comment.