Class: Rack::Async::CallbackBody
- Inherits:
-
Object
- Object
- Rack::Async::CallbackBody
- Defined in:
- lib/rack/async.rb
Instance Method Summary collapse
- #<<(data) ⇒ Object
- #callback(&block) ⇒ Object
- #each(&block) ⇒ Object
- #errback(&block) ⇒ Object
- #fail ⇒ Object
-
#initialize(*args) ⇒ CallbackBody
constructor
A new instance of CallbackBody.
- #on_eof(&block) ⇒ Object
- #succeed ⇒ Object (also: #finish)
Constructor Details
#initialize(*args) ⇒ CallbackBody
Returns a new instance of CallbackBody.
69 70 71 72 73 |
# File 'lib/rack/async.rb', line 69 def initialize(*args) super @buffer = [] @lock = Mutex.new end |
Instance Method Details
#<<(data) ⇒ Object
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/rack/async.rb', line 75 def <<(data) @lock.synchronize do if @each_callback @each_callback.call(data) else @buffer << data end end self end |
#callback(&block) ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/rack/async.rb', line 105 def callback(&block) if !@callback && @finished block.call else @callback = block end end |
#each(&block) ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/rack/async.rb', line 86 def each(&block) @lock.synchronize do @buffer.each(&block) @buffer.clear @each_callback = block end end |
#errback(&block) ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/rack/async.rb', line 113 def errback(&block) if !@errback && @finished block.call else @errback = block end end |
#fail ⇒ Object
100 101 102 103 |
# File 'lib/rack/async.rb', line 100 def fail @errback.call if @errback @finished = true end |
#on_eof(&block) ⇒ Object
121 122 123 124 |
# File 'lib/rack/async.rb', line 121 def on_eof(&block) callback(&block) errback(&block) end |
#succeed ⇒ Object Also known as: finish
94 95 96 97 |
# File 'lib/rack/async.rb', line 94 def succeed @callback.call if @callback @finished = true end |