Class: CloudRailSi::ServiceCode::LimitedReadableStream

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudrail_si/helpers/LimitedReadableStream.rb

Instance Method Summary collapse

Constructor Details

#initialize(source, limit) ⇒ LimitedReadableStream

Returns a new instance of LimitedReadableStream.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cloudrail_si/helpers/LimitedReadableStream.rb', line 6

def initialize(source, limit)
    super()

    @source = source
    @limit = limit
    @should_push = false
    @sourceReadable = source.readable


    #TODO: translate this
=begin
    source.on("readable", function () {
        _this.sourceReadable = true;
        _this.process();
    })
    source.on("end", function () {
        _this.push(null);
    })
    source.on("error", function (err) {
        _this.emit("error", err);
    })
=end
end

Instance Method Details

#_processObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cloudrail_si/helpers/LimitedReadableStream.rb', line 35

def _process
    if (@should_push && this.source_readable)
        chunk = source.read(min(@limit, CHUNK_SIZE))
        while (!chunk.nil?)
            @limit -= chunk.length
            if (@limit === 0)
                @push.call(chunk)
                @push.call(null)
                return
            end
            if (!this.push(chunk))
                this.shouldPush = false
                return
            end
            chunk = source.read(min(@limit, CHUNK_SIZE))
        end
    end
end

#_readObject



30
31
32
33
# File 'lib/cloudrail_si/helpers/LimitedReadableStream.rb', line 30

def _read
    @shouldPush = true
    @process.call
end