Sha256: eaa7daafee963d9f585e999f1c1f5113853fa2b12c28a35b80d2f639fe716b22

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

##
# Midori Extension of File reading and writing
module MidoriContrib
  class File
    # Init File object
    # @param [Array] args same args like File.new
    def initialize(*args)
      @file = ::File.new(*args)
    end

    # read file
    # @return [String] string readed
    def read
      await(Promise.new do |resolve|
        data = ''
        EventLoop.register(@file, :r) do
          if @file.eof?
            EventLoop.deregister(@file)
            resolve.call(data)
          else
            data << @file.read_nonblock(16384)
          end
        end
      end)
    end

    # write file
    # @param [String] data string to be written
    def write(data)
      await(Promise.new do |resolve|
        written = 0
        EventLoop.register(@file, :w) do
          written += @file.write_nonblock(data[written..-1])
          if written == data.size
            EventLoop.deregister(@file)
            resolve.call(written)
          end
        end
      end)
    end

    # raw file object
    # @return [File] file
    def raw
      @file
    end

    # Close the file
    def close
      @file.close
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
midori-contrib-0.0.2 lib/midori-contrib/file.rb