Sha256: db49036a1f884d6cafadc2379c915ac8dcf19cda8081c6120491b40d342e9634

Contents?: true

Size: 1.16 KB

Versions: 368

Compression:

Stored size: 1.16 KB

Contents

module Files
  class SizableIO < IO
    def self.pipe
      r, w = super

      w.instance_variable_set(:@read_io, r)

      [ r, w ]
    end

    def size
      read_io.content_length_promise.wait.value
    end

    def wait!(timeout = nil)
      read_io.ready_promise.wait(timeout)
      error!
      self
    end

    def fulfill_content_length(length)
      read_io.content_length = length
      read_io.content_length_promise.execute
    end

    def ready!
      read_io.ready_promise.execute
    end

    def close
      raise @with_error if @with_error

      super
      read_io.content_length_promise.try_set(nil)
      read_io.ready_promise.try_set(true)
    end

    def error!
      raise read_io.with_error if read_io.with_error
    end

    def do_set_error(e)
      read_io.with_error = e
    end

    protected

    attr_accessor :content_length, :with_error

    def content_length_promise
      @content_length_promise ||= Concurrent::Promise.new { content_length }
    end

    def ready_promise
      @ready_promise ||= Concurrent::Promise.new { true }
    end

    def read_io
      @read_io || self
    end

    def read_io?
      read_io == self
    end
  end
end

Version data entries

368 entries across 368 versions & 1 rubygems

Version Path
files.com-1.1.180 lib/files.com/sizable_io.rb
files.com-1.1.179 lib/files.com/sizable_io.rb
files.com-1.1.178 lib/files.com/sizable_io.rb
files.com-1.1.177 lib/files.com/sizable_io.rb
files.com-1.1.176 lib/files.com/sizable_io.rb
files.com-1.1.175 lib/files.com/sizable_io.rb
files.com-1.1.174 lib/files.com/sizable_io.rb
files.com-1.1.173 lib/files.com/sizable_io.rb
files.com-1.1.172 lib/files.com/sizable_io.rb
files.com-1.1.171 lib/files.com/sizable_io.rb
files.com-1.1.170 lib/files.com/sizable_io.rb
files.com-1.1.169 lib/files.com/sizable_io.rb
files.com-1.1.168 lib/files.com/sizable_io.rb
files.com-1.1.167 lib/files.com/sizable_io.rb
files.com-1.1.166 lib/files.com/sizable_io.rb
files.com-1.1.165 lib/files.com/sizable_io.rb
files.com-1.1.164 lib/files.com/sizable_io.rb
files.com-1.1.163 lib/files.com/sizable_io.rb
files.com-1.1.162 lib/files.com/sizable_io.rb
files.com-1.1.161 lib/files.com/sizable_io.rb