Skip to content

Commit

Permalink
add defer stack for handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
joshbuddy committed Sep 4, 2011
1 parent c8008ef commit 740b601
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/goliath/connection.rb
Expand Up @@ -24,7 +24,7 @@ def post_init
@parser = Http::Parser.new
@parser.on_headers_complete = proc do |h|

env = Goliath::Env.new
env = Thread.current[GOLIATH_ENV] = Goliath::Env.new
env[SERVER_PORT] = port.to_s
env[RACK_LOGGER] = logger
env[OPTIONS] = options
Expand Down
4 changes: 4 additions & 0 deletions lib/goliath/env.rb
Expand Up @@ -121,6 +121,10 @@ def respond_to?(name)
super
end

def defer_stack
@defer_stack ||= []
end

# The Goliath::Env will provide any of it's keys as a method. It will also provide
# any of the keys in the config object as methods. The methods will return
# the value of the key. If the key doesn't exist in either hash this will
Expand Down
38 changes: 38 additions & 0 deletions lib/goliath/rack/builder.rb
Expand Up @@ -4,8 +4,44 @@ module Goliath
module Rack
class Builder < ::Rack::Builder
attr_accessor :params
attr_reader :inner_app
include Params::Parser

alias_method :original_run, :run
def run(app)
@inner_app = app
original_run(app)
end

module MappingHandlers
include Constants
attr_reader :last_app

def defer
defer = EM::DefaultDeferrable.new
Thread.current[GOLIATH_ENV].defer_stack << defer
defer
end

def on_body(env, body)
defer.callback do |api|
api.on_body(env, body) if api.respond_to?(:on_body)
end
end

def on_headers(env, headers)
defer.callback do |api|
api.on_headers(env, headers) if api.respond_to?(:on_headers)
end
end

def on_close(env)
defer.callback do |api|
api.on_close(env) if api.respond_to?(:on_close)
end
end
end

# Builds the rack middleware chain for the given API
#
# @param klass [Class] The API class to build the middlewares for
Expand All @@ -17,6 +53,7 @@ def self.build(klass, api)
use(mw_klass, *args, &blk)
end
if klass.maps?
klass.instance_eval "include MappingHandlers", __FILE__, __LINE__
klass.maps.each do |path, route_klass, opts, blk|
blk ||= Proc.new {
run Builder.build(route_klass, route_klass.new)
Expand All @@ -27,6 +64,7 @@ def self.build(klass, api)
env['params'].merge!(env['router.params']) if env['router.params']
builder.params = builder.retrieve_params(env)
builder.instance_eval(&blk)
env.defer_stack.each{ |defer| defer.succeed(builder.inner_app)}
builder.to_app.call(env)
}
end
Expand Down

0 comments on commit 740b601

Please sign in to comment.