class Async::HTTP::Body::Writable
A dynamic body which you can write to and read from.
Public Class Methods
new(length = nil, queue: Async::Queue.new)
click to toggle source
@param [Integer] length The length of the response body if known. @param [Async::Queue] queue Specify a different queue implementation, e.g. `Async::LimitedQueue.new(8)` to enable back-pressure streaming.
# File lib/async/http/body/writable.rb, line 38 def initialize(length = nil, queue: Async::Queue.new) @queue = queue @length = length @count = 0 @finished = false @closed = false @error = nil end
Public Instance Methods
close(error = nil)
click to toggle source
Stop generating output; cause the next call to write to fail with the given error.
Calls superclass method
# File lib/async/http/body/writable.rb, line 56 def close(error = nil) unless @closed @queue.enqueue(nil) @closed = true @error = error end super end
closed?()
click to toggle source
# File lib/async/http/body/writable.rb, line 67 def closed? @closed end
empty?()
click to toggle source
Has the producer called finish and has the reader consumed the nil token?
# File lib/async/http/body/writable.rb, line 76 def empty? @finished end
inspect()
click to toggle source
# File lib/async/http/body/writable.rb, line 105 def inspect "\#<#{self.class} #{@count} chunks written, #{status}>" end
length()
click to toggle source
# File lib/async/http/body/writable.rb, line 51 def length @length end
read()
click to toggle source
Read the next available chunk.
# File lib/async/http/body/writable.rb, line 81 def read return if @finished unless chunk = @queue.dequeue @finished = true end return chunk end
ready?()
click to toggle source
# File lib/async/http/body/writable.rb, line 71 def ready? !@queue.empty? end
write(chunk)
click to toggle source
Write a single chunk to the body. Signal completion by calling `#finish`.
# File lib/async/http/body/writable.rb, line 92 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
Also aliased as: <<
Private Instance Methods
status()
click to toggle source
# File lib/async/http/body/writable.rb, line 111 def status if @finished 'finished' elsif @closed 'closing' else 'waiting' end end