doc: rdoc cleanups and fixes
[rainbows.git] / lib / rainbows / event_machine / try_defer.rb
blob97e384d7163d9ee97ad6fbb14cd7a37413c5dce9
1 # -*- encoding: binary -*-
2 # :enddoc:
4 # Middleware that will run the app dispatch in a separate thread.
5 # This middleware is automatically loaded by Rainbows! when using
6 # EventMachine and if the app responds to the +deferred?+ method.
7 class Rainbows::EventMachine::TryDefer < Struct.new(:app)
8   # shortcuts
9   ASYNC_CALLBACK = Rainbows::EvCore::ASYNC_CALLBACK
11   def initialize(app)
12     # the entire app becomes multithreaded, even the root (non-deferred)
13     # thread since any thread can share processes with others
14     Rainbows::Const::RACK_DEFAULTS['rack.multithread'] = true
15     super
16   end
18   def call(env)
19     if app.deferred?(env)
20       EM.defer(proc { catch(:async) { app.call(env) } }, env[ASYNC_CALLBACK])
21       # all of the async/deferred stuff breaks Rack::Lint :<
22       nil
23     else
24       app.call(env)
25     end
26   end
27 end