Class: Sinatra::Helpers::Stream
- Inherits:
-
Object
- Object
- Sinatra::Helpers::Stream
- 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 collapse
Instance Method Summary collapse
- #<<(data) ⇒ Object
- #callback(&block) ⇒ Object (also: #errback)
- #close ⇒ Object
- #closed? ⇒ Boolean
- #each(&front) ⇒ Object
-
#initialize(scheduler = self.class, keep_open = false, &back) ⇒ Stream
constructor
A new instance of Stream.
Constructor Details
#initialize(scheduler = self.class, keep_open = false, &back) ⇒ Stream
Returns a new instance of Stream.
457 458 459 460 461 462 463 |
# File 'lib/sinatra/base.rb', line 457 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 ⇒ Object
455 |
# File 'lib/sinatra/base.rb', line 455 def self.defer(*) yield end |
.schedule ⇒ Object
454 |
# File 'lib/sinatra/base.rb', line 454 def self.schedule(*) yield end |
Instance Method Details
#<<(data) ⇒ Object
485 486 487 488 |
# File 'lib/sinatra/base.rb', line 485 def <<(data) @scheduler.schedule { @front.call(data.to_s) } self end |
#callback(&block) ⇒ Object Also known as: errback
490 491 492 493 494 |
# File 'lib/sinatra/base.rb', line 490 def callback(&block) return yield if closed? @callbacks << block end |
#close ⇒ Object
465 466 467 468 469 470 |
# File 'lib/sinatra/base.rb', line 465 def close return if closed? @closed = true @scheduler.schedule { @callbacks.each { |c| c.call } } end |
#closed? ⇒ Boolean
498 499 500 |
# File 'lib/sinatra/base.rb', line 498 def closed? @closed end |
#each(&front) ⇒ Object
472 473 474 475 476 477 478 479 480 481 482 483 |
# File 'lib/sinatra/base.rb', line 472 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 |