Some Rack handlers (Thin, Rainbows!) implement an extended body object protocol, however, some middleware (namely Rack::Lint) will break it by not mirroring the methods in question. This middleware will detect an extended body object and will make sure it reaches the handler directly. We do this here, so our middleware and middleware set up by the app will still be able to run.
# File lib/sinatra/base.rb, line 123 def call(env) result, callback = app.call(env), env['async.callback'] return result unless callback and async?(*result) after_response { callback.call result } setup_close(env, *result) throw :async end
# File lib/sinatra/base.rb, line 139 def after_response(&block) raise NotImplementedError, "only supports EventMachine at the moment" unless defined? EventMachine EventMachine.next_tick(&block) end
# File lib/sinatra/base.rb, line 144 def async?(status, headers, body) return true if status == -1 body.respond_to? :callback and body.respond_to? :errback end
# File lib/sinatra/base.rb, line 133 def setup_close(env, status, header, body) return unless body.respond_to? :close and env.include? 'async.close' env['async.close'].callback { body.close } env['async.close'].errback { body.close } end