class Roda::RodaPlugins::Streaming::Stream
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.
Public Class Methods
new(opts={}, &back)
click to toggle source
Handle streaming options, see Streaming
for details.
# File lib/roda/plugins/streaming.rb, line 88 def initialize(opts={}, &back) @scheduler = opts[:scheduler] || Scheduler.new(self) @back = back.to_proc @keep_open = opts[:keep_open] @callbacks = [] @closed = false if opts[:callback] callback(&opts[:callback]) end end
Public Instance Methods
<<(data)
click to toggle source
Add output to the streaming response body.
# File lib/roda/plugins/streaming.rb, line 101 def <<(data) @scheduler.schedule{@front.call(data.to_s)} self end
callback() { || ... }
click to toggle source
Add the given block as a callback to call when the block closes.
# File lib/roda/plugins/streaming.rb, line 107 def callback(&block) return yield if closed? @callbacks << block end
Also aliased as: errback
close()
click to toggle source
If not already closed, close the connection, and call any callbacks.
# File lib/roda/plugins/streaming.rb, line 117 def close return if closed? @closed = true @scheduler.schedule{@callbacks.each{|c| c.call}} end
closed?()
click to toggle source
Whether the connection has already been closed.
# File lib/roda/plugins/streaming.rb, line 124 def closed? @closed end
each(&front)
click to toggle source
Yield values to the block as they are passed in via <<
.
# File lib/roda/plugins/streaming.rb, line 129 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