123456789_123456789_123456789_123456789_123456789_

Class: Sinatra::Helpers::Stream

Relationships & Source Files
Inherits: Object
Defined in: lib/sinatra/base.rb

Overview

Class of the response body in case you use #stream.

Three things really matter: The front and back block (back being the block generating content, front the one sending it to the client) and the scheduler, integrating with whatever concurrency feature the ::Rack handler is using.

Scheduler has to respond to defer and schedule.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(scheduler = self.class, keep_open = false, &back) ⇒ Stream

[ GitHub ]

  
# File 'lib/sinatra/base.rb', line 461

def initialize(scheduler = self.class, keep_open = false, &back)
  @back = back.to_proc
  @scheduler = scheduler
  @keep_open = keep_open
  @callbacks = []
  @closed = false
end

Class Method Details

.defer

[ GitHub ]

  
# File 'lib/sinatra/base.rb', line 459

def self.defer(*)    yield end

.schedule

[ GitHub ]

  
# File 'lib/sinatra/base.rb', line 458

def self.schedule(*) yield end

Instance Attribute Details

#closed?Boolean (readonly)

[ GitHub ]

  
# File 'lib/sinatra/base.rb', line 502

def closed?
  @closed
end

Instance Method Details

#<<(data)

[ GitHub ]

  
# File 'lib/sinatra/base.rb', line 489

def <<(data)
  @scheduler.schedule { @front.call(data.to_s) }
  self
end

#callback(&block) Also known as: #errback

[ GitHub ]

  
# File 'lib/sinatra/base.rb', line 494

def callback(&block)
  return yield if closed?

  @callbacks << block
end

#close

[ GitHub ]

  
# File 'lib/sinatra/base.rb', line 469

def close
  return if closed?

  @closed = true
  @scheduler.schedule { @callbacks.each { |c| c.call } }
end

#each(&front)

[ GitHub ]

  
# File 'lib/sinatra/base.rb', line 476

def each(&front)
  @front = front
  @scheduler.defer do
    begin
      @back.call(self)
    rescue Exception => e
      @scheduler.schedule { raise e }
    ensure
      close unless @keep_open
    end
  end
end

#errback(&block)

Alias for #callback.

[ GitHub ]

  
# File 'lib/sinatra/base.rb', line 500

alias errback callback