Class: Gin::Stream
- Inherits:
-
Object
- Object
- Gin::Stream
- Defined in:
- lib/gin/stream.rb
Overview
Taken from Sinatra.
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.
18 19 20 21 |
# File 'lib/gin/stream.rb', line 18 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
.defer ⇒ Object
15 |
# File 'lib/gin/stream.rb', line 15 def self.defer(*) yield end |
.schedule ⇒ Object
14 |
# File 'lib/gin/stream.rb', line 14 def self.schedule(*) yield end |
Instance Method Details
#<<(data) ⇒ Object
41 42 43 44 |
# File 'lib/gin/stream.rb', line 41 def <<(data) @scheduler.schedule { @front.call(data.to_s) } self end |
#callback(&block) ⇒ Object Also known as: errback
46 47 48 49 |
# File 'lib/gin/stream.rb', line 46 def callback(&block) return yield if @closed @callbacks << block end |
#close ⇒ Object
23 24 25 26 27 |
# File 'lib/gin/stream.rb', line 23 def close return if @closed @closed = true @scheduler.schedule { @callbacks.each { |c| c.call }} end |
#closed? ⇒ Boolean
53 54 55 |
# File 'lib/gin/stream.rb', line 53 def closed? @closed end |
#each(&front) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/gin/stream.rb', line 29 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 |