Skip to content

Commit

Permalink
Added callback hook for when the response is done.
Browse files Browse the repository at this point in the history
  • Loading branch information
troelskn committed Mar 3, 2015
1 parent d1898dc commit 1378c22
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/thin/async.rb
Expand Up @@ -49,6 +49,7 @@ def self.perform(*args, &block)

def initialize(env, status=200, headers={})
@callback = env['async.callback']
@close = env['async.close']
@body = DeferrableBody.new
@status = status
@headers = headers
Expand Down Expand Up @@ -85,6 +86,24 @@ def done?
@done
end

# Specify a block to be executed when the response is done
#
# Calling this method before the response has completed will cause the
# callback block to be stored on an internal list.
# If you call this method after the response is done, the block will
# be executed immediately.
#
def callback &block
@close.callback(&block)
self
end

# Cancels an outstanding callback to &block if any. Undoes the action of #callback.
#
def cancel_callback block
@close.cancel_callback(block)
end

# Tell Thin the response is gonna be sent asynchronously.
# The status code of -1 is the magic trick here.
def finish
Expand Down

0 comments on commit 1378c22

Please sign in to comment.