Class: Sinatra::Helpers::Stream

Inherits:
Object
  • Object
show all
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 blog 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 (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Stream) initialize(scheduler = self.class, keep_open = false, &back)

A new instance of Stream



299
300
301
302
# File 'lib/sinatra/base.rb', line 299

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

Class Method Details

+ (Object) defer



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

def self.defer(*)    yield end

+ (Object) schedule



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

def self.schedule(*) yield end

Instance Method Details

- (Object) <<(data)



322
323
324
325
# File 'lib/sinatra/base.rb', line 322

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

- (Object) callback(&block) Also known as: errback



327
328
329
330
# File 'lib/sinatra/base.rb', line 327

def callback(&block)
  return yield if @closed
  @callbacks << block
end

- (Object) close



304
305
306
307
308
# File 'lib/sinatra/base.rb', line 304

def close
  return if @closed
  @closed = true
  @scheduler.schedule { @callbacks.each { |c| c.call }}
end

- (Object) each(&front)



310
311
312
313
314
315
316
317
318
319
320
# File 'lib/sinatra/base.rb', line 310

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