Skip to content

Commit

Permalink
simpler solution for event dispatching. no more defer stack
Browse files Browse the repository at this point in the history
  • Loading branch information
joshbuddy committed Sep 4, 2011
1 parent 294d9cf commit 92166b2
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 44 deletions.
20 changes: 19 additions & 1 deletion lib/goliath/api.rb
Expand Up @@ -5,6 +5,10 @@
require 'goliath/rack'
require 'goliath/validation'

class HttpRouter::Route
attr_accessor :event_handler
end

module Goliath
# All Goliath APIs subclass Goliath::API. All subclasses _must_ override the
# {#response} method.
Expand Down Expand Up @@ -183,6 +187,21 @@ def env
Thread.current[GOLIATH_ENV]
end

# Accessor for the event handler for on_{body, close, header}
#
# @return [Goliath::Api, nil] The current handler for events

def event_handler
@event_handler ||= begin
if self.class.maps?
response = self.class.router.recognize(env)
response.path.route.event_handler
else
self
end
end
end

# The API will proxy missing calls to the env object if possible.
#
# The two entries in this example are equivalent as long as you are not
Expand Down Expand Up @@ -216,7 +235,6 @@ def respond_to_missing?(name, *)
# @return [Goliath::Connection::AsyncResponse] An async response.
def call(env)
begin
Thread.current[GOLIATH_ENV] = env
status, headers, body = response(env)

if status
Expand Down
9 changes: 4 additions & 5 deletions lib/goliath/connection.rb
Expand Up @@ -32,13 +32,12 @@ def post_init
env[CONFIG] = config
env[REMOTE_ADDR] = remote_address

env[ASYNC_HEADERS] = @api.method(:on_headers) if @api.respond_to? :on_headers
env[ASYNC_BODY] = @api.method(:on_body) if @api.respond_to? :on_body
env[ASYNC_CLOSE] = @api.method(:on_close) if @api.respond_to? :on_close

r = Goliath::Request.new(@app, self, env)
r.parse_header(h, @parser)

env[ASYNC_HEADERS] = @api.event_handler.method(:on_headers) if @api.event_handler.respond_to? :on_headers
env[ASYNC_BODY] = @api.event_handler.method(:on_body) if @api.event_handler.respond_to? :on_body
env[ASYNC_CLOSE] = @api.event_handler.method(:on_close) if @api.event_handler.respond_to? :on_close
r.dispatch_header(h)
@requests.push(r)
end

Expand Down
4 changes: 0 additions & 4 deletions lib/goliath/env.rb
Expand Up @@ -121,10 +121,6 @@ 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
39 changes: 5 additions & 34 deletions lib/goliath/rack/builder.rb
Expand Up @@ -13,35 +13,6 @@ def run(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 @@ -53,18 +24,18 @@ 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)
}
klass.router.add(path, opts.dup).to {|env|
builder = Builder.new
builder = Builder.new
builder.instance_eval(&blk)
route = klass.router.add(path, opts.dup)
route.event_handler = builder.inner_app
route.to {|env|
env['params'] ||= {}
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
2 changes: 2 additions & 0 deletions lib/goliath/request.rb
Expand Up @@ -70,7 +70,9 @@ def parse_header(h, parser)
@env[REQUEST_PATH] = parser.request_path
@env[PATH_INFO] = parser.request_path
@env[FRAGMENT] = parser.fragment
end

def dispatch_header(h)
begin
@env[ASYNC_HEADERS].call(@env, h) if @env[ASYNC_HEADERS]
rescue Exception => e
Expand Down

2 comments on commit 92166b2

@igrigorik
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, this makes a lot more sense now - thanks Josh. Now for the finale.. we just need to wrap this with an integration test, or something. ;-)

@joshbuddy
Copy link
Member Author

@joshbuddy joshbuddy commented on 92166b2 Sep 4, 2011 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.