Class: Thin::AsyncResponse

Inherits:
Object
  • Object
show all
Includes:
Rack::Response::Helpers
Defined in:
lib/message_bus/rack/thin_ext.rb

Overview

Response which body is sent asynchronously.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, status = 200, headers = {}) ⇒ AsyncResponse

Returns a new instance of AsyncResponse.



49
50
51
52
53
54
55
# File 'lib/message_bus/rack/thin_ext.rb', line 49

def initialize(env, status = 200, headers = {})
  @callback = env['async.callback']
  @body = DeferrableBody.new
  @status = status
  @headers = headers
  @headers_sent = false
end

Instance Attribute Details

#callbackObject (readonly)

Returns the value of attribute callback.



46
47
48
# File 'lib/message_bus/rack/thin_ext.rb', line 46

def callback
  @callback
end

#closedObject (readonly)

Returns the value of attribute closed.



46
47
48
# File 'lib/message_bus/rack/thin_ext.rb', line 46

def closed
  @closed
end

#headersObject (readonly)

Returns the value of attribute headers.



46
47
48
# File 'lib/message_bus/rack/thin_ext.rb', line 46

def headers
  @headers
end

#statusObject

Returns the value of attribute status.



47
48
49
# File 'lib/message_bus/rack/thin_ext.rb', line 47

def status
  @status
end

Instance Method Details

#doneObject

Tell Thin the response is complete and the connection can be closed.



71
72
73
74
75
# File 'lib/message_bus/rack/thin_ext.rb', line 71

def done
  @closed = true
  send_headers
  ::EM.next_tick { @body.succeed }
end

#send_headersObject



57
58
59
60
61
62
# File 'lib/message_bus/rack/thin_ext.rb', line 57

def send_headers
  return if @headers_sent

  @callback.call [@status, @headers, @body]
  @headers_sent = true
end

#write(body) ⇒ Object Also known as: <<



64
65
66
67
# File 'lib/message_bus/rack/thin_ext.rb', line 64

def write(body)
  send_headers
  @body.call(body.respond_to?(:each) ? body : [body])
end