Class: Async::HTTP::Body::Writable
- Defined in:
- lib/async/http/body/writable.rb
Overview
A dynamic body which you can write to and read from.
Instance Method Summary collapse
-
#empty? ⇒ Boolean
Has the producer called #finish and has the reader consumed the nil token?.
-
#finish ⇒ Object
Signal that output has finished.
-
#initialize ⇒ Writable
constructor
A new instance of Writable.
- #inspect ⇒ Object
-
#read ⇒ Object
Read the next available chunk.
-
#stop(error) ⇒ Object
Stop generating output; cause the next call to write to fail with the given error.
-
#write(chunk) ⇒ Object
(also: #<<)
Write a single chunk to the body.
Methods inherited from Readable
Constructor Details
Instance Method Details
#empty? ⇒ Boolean
Has the producer called #finish and has the reader consumed the nil token?
40 41 42 |
# File 'lib/async/http/body/writable.rb', line 40 def empty? @finished end |
#finish ⇒ Object
Signal that output has finished. This must be called at least once.
82 83 84 |
# File 'lib/async/http/body/writable.rb', line 82 def finish @queue.enqueue(nil) end |
#inspect ⇒ Object
86 87 88 |
# File 'lib/async/http/body/writable.rb', line 86 def inspect "\#<#{self.class} #{@count} chunks written#{@finished ? ', finished' : ''}>" end |
#read ⇒ Object
Read the next available chunk.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/async/http/body/writable.rb', line 45 def read # I'm not sure if this is a good idea. # if @stopped # raise @stopped # end return if @finished unless chunk = @queue.dequeue @finished = true end return chunk end |
#stop(error) ⇒ Object
Stop generating output; cause the next call to write to fail with the given error.
61 62 63 |
# File 'lib/async/http/body/writable.rb', line 61 def stop(error) @stopped ||= error end |
#write(chunk) ⇒ Object Also known as: <<
Write a single chunk to the body. Signal completion by calling ‘#finish`.
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/async/http/body/writable.rb', line 66 def write(chunk) # If the reader breaks, the writer will break. # The inverse of this is less obvious (*) if @stopped raise @stopped end # TODO should this yield if the queue is full? @count += 1 @queue.enqueue(chunk) end |