Class: Sinatra::Helpers::Stream

Inherits:
Object
  • Object
show all
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/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 collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Stream.



455
456
457
458
459
460
461
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/lib/sinatra/base.rb', line 455

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

.deferObject



453
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/lib/sinatra/base.rb', line 453

def self.defer(*)    yield end

.scheduleObject



452
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/lib/sinatra/base.rb', line 452

def self.schedule(*) yield end

Instance Method Details

#<<(data) ⇒ Object



482
483
484
485
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/lib/sinatra/base.rb', line 482

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

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



487
488
489
490
491
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/lib/sinatra/base.rb', line 487

def callback(&block)
  return yield if closed?

  @callbacks << block
end

#closeObject



463
464
465
466
467
468
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/lib/sinatra/base.rb', line 463

def close
  return if closed?

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

#closed?Boolean

Returns:

  • (Boolean)


495
496
497
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/lib/sinatra/base.rb', line 495

def closed?
  @closed
end

#each(&front) ⇒ Object



470
471
472
473
474
475
476
477
478
479
480
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.5/lib/sinatra/base.rb', line 470

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