Permalink
Browse files

set up async.close on thin and friends. fixes #446.

  • Loading branch information...
1 parent 301556a commit 9e54fa51467caf0729a8c8fb2043df4dc611430e @rkh rkh committed Jan 5, 2012
Showing with 27 additions and 1 deletion.
  1. +10 −1 lib/sinatra/base.rb
  2. +17 −0 test/streaming_test.rb
View
11 lib/sinatra/base.rb
@@ -275,6 +275,7 @@ def <<(data)
end
def callback(&block)
+ return yield if @closed
@callbacks << block
end
@@ -290,6 +291,7 @@ def callback(&block)
def stream(keep_open = false)
scheduler = env['async.callback'] ? EventMachine : Stream
current = @params.dup
+
block = proc do |out|
begin
original, @params = @params, current
@@ -299,7 +301,14 @@ def stream(keep_open = false)
end
end
- body Stream.new(scheduler, keep_open, &block)
+ out = Stream.new(scheduler, keep_open, &block)
+
+ if env['async.close']
+ env['async.close'].callback { out.close }
+ env['async.close'].errback { out.close }
+ end
+
+ body out
end
# Specify response freshness policy for HTTP caches (Cache-Control header).
View
17 test/streaming_test.rb
@@ -120,4 +120,21 @@ def defer!(*) @defer.pop.call until @defer.empty? end
get '/foo'
assert_body 'foo'
end
+
+ it 'sets up async.close if available' do
+ ran = false
+ mock_app do
+ get('/') do
+ close = Object.new
+ def close.callback; yield end
+ def close.errback; end
+ env['async.close'] = close
+ stream(:keep_open) do |out|
+ out.callback { ran = true }
+ end
+ end
+ end
+ get '/'
+ assert ran
+ end
end

0 comments on commit 9e54fa5

Please sign in to comment.