Class: Rack::Stream::DeferrableBody

Inherits:
Object
  • Object
show all
Includes:
EM::Deferrable
Defined in:
lib/rack/stream/deferrable_body.rb

Overview

From thin_async

Instance Method Summary collapse

Constructor Details

#initialize(chunks = []) ⇒ DeferrableBody

Returns a new instance of DeferrableBody.

Parameters:

  • chunks (defaults to: [])
    • object that responds to each. holds initial chunks of content


8
9
10
11
# File 'lib/rack/stream/deferrable_body.rb', line 8

def initialize(chunks = [])
  @queue = []
  chunks.each {|c| chunk(c)}
end

Instance Method Details

#chunk(*chunks) ⇒ Object

Enqueue a chunk of content to be flushed to stream at a later time



14
15
16
17
# File 'lib/rack/stream/deferrable_body.rb', line 14

def chunk(*chunks)
  @queue += chunks
  schedule_dequeue
end

#close!(flush = true) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/rack/stream/deferrable_body.rb', line 30

def close!(flush = true)
  EM.next_tick {
    if !flush || empty?
      succeed
    else
      schedule_dequeue
      close!(flush)
    end
  }
end

#each(&blk) ⇒ Object

When rack attempts to iterate over body, save the block, and execute at a later time when @queue has elements



21
22
23
24
# File 'lib/rack/stream/deferrable_body.rb', line 21

def each(&blk)
  @body_callback = blk
  schedule_dequeue
end

#empty?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/rack/stream/deferrable_body.rb', line 26

def empty?
  @queue.empty?
end