Class: Async::HTTP::Body::Writable
- Inherits:
-
Readable
- Object
- Readable
- Async::HTTP::Body::Writable
- Defined in:
- lib/async/http/body/writable.rb
Overview
A dynamic body which you can write to and read from.
Direct Known Subclasses
Defined Under Namespace
Classes: Closed
Instance Method Summary collapse
-
#close(error = nil) ⇒ Object
Stop generating output; cause the next call to write to fail with the given error.
- #closed? ⇒ Boolean
-
#empty? ⇒ Boolean
Has the producer called #finish and has the reader consumed the nil token?.
-
#initialize(length = nil, queue: Async::Queue.new) ⇒ Writable
constructor
A new instance of Writable.
- #inspect ⇒ Object
- #length ⇒ Object
-
#read ⇒ Object
Read the next available chunk.
- #ready? ⇒ Boolean
-
#write(chunk) ⇒ Object
(also: #<<)
Write a single chunk to the body.
Constructor Details
#initialize(length = nil, queue: Async::Queue.new) ⇒ Writable
Returns a new instance of Writable.
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/async/http/body/writable.rb', line 21 def initialize(length = nil, queue: Async::Queue.new) @queue = queue @length = length @count = 0 @finished = false @closed = false @error = nil end |
Instance Method Details
#close(error = nil) ⇒ Object
Stop generating output; cause the next call to write to fail with the given error.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/async/http/body/writable.rb', line 39 def close(error = nil) unless @closed @queue.enqueue(nil) @closed = true @error = error end super end |
#closed? ⇒ Boolean
50 51 52 |
# File 'lib/async/http/body/writable.rb', line 50 def closed? @closed end |
#empty? ⇒ Boolean
Has the producer called #finish and has the reader consumed the nil token?
59 60 61 |
# File 'lib/async/http/body/writable.rb', line 59 def empty? @finished end |
#inspect ⇒ Object
88 89 90 |
# File 'lib/async/http/body/writable.rb', line 88 def inspect "\#<#{self.class} #{@count} chunks written, #{status}>" end |
#length ⇒ Object
34 35 36 |
# File 'lib/async/http/body/writable.rb', line 34 def length @length end |
#read ⇒ Object
Read the next available chunk.
64 65 66 67 68 69 70 71 72 |
# File 'lib/async/http/body/writable.rb', line 64 def read return if @finished unless chunk = @queue.dequeue @finished = true end return chunk end |
#ready? ⇒ Boolean
54 55 56 |
# File 'lib/async/http/body/writable.rb', line 54 def ready? !@queue.empty? end |
#write(chunk) ⇒ Object Also known as: <<
Write a single chunk to the body. Signal completion by calling ‘#finish`.
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/async/http/body/writable.rb', line 75 def write(chunk) # If the reader breaks, the writer will break. # The inverse of this is less obvious (*) if @closed raise(@error || Closed) end @count += 1 @queue.enqueue(chunk) end |