Sha256: 5bcdd5bd9c0346111153fe2b75a8f6bbd9b2a211a631ca5c56e1e98be774c3d3

Contents?: true

Size: 1.45 KB

Versions: 54

Compression:

Stored size: 1.45 KB

Contents

# Copyright (c) 2007, 2008, 2009 Blake Mizerany
# Copyright (c) 2010, 2011, 2012, 2013, 2014, 2015, 2016 Konstantin Haase
#
# 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 Rack::App::Streamer
  require "rack/app/streamer/scheduler"

  def initialize(env, options={}, &back)
    @serializer = env[::Rack::App::Constants::ENV::SERIALIZER]
    @extname = env[::Rack::App::Constants::ENV::EXTNAME]
    @scheduler = options[:scheduler] || Rack::App::Streamer::Scheduler.by_env(env)
    @keep_open = options[:keep_open] || false
    @back = back.to_proc
    @callbacks, @closed = [], false
  end

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

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

  def <<(data)
    @scheduler.schedule { @front.call(@serializer.serialize(@extname, data)) }
    self
  end

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

  alias errback callback

  def closed?
    @closed
  end
end

Version data entries

54 entries across 54 versions & 2 rubygems

Version Path
rack-app-11.0.2 lib/rack/app/streamer.rb
rack-app-11.0.1 lib/rack/app/streamer.rb
rack-app-11.0.0 lib/rack/app/streamer.rb
rack-app-10.0.0 lib/rack/app/streamer.rb
rack-app-9.0.0 lib/rack/app/streamer.rb
rack-app-8.1.0 lib/rack/app/streamer.rb
rack-app-8.0.1 lib/rack/app/streamer.rb
rack-app-8.0.0 lib/rack/app/streamer.rb
mountapi-0.11.1 vendor/bundle/ruby/2.7.0/gems/rack-app-7.7.0/lib/rack/app/streamer.rb
rack-app-7.7.0 lib/rack/app/streamer.rb
rack-app-7.6.5 lib/rack/app/streamer.rb
rack-app-7.6.4 lib/rack/app/streamer.rb
rack-app-7.6.3 lib/rack/app/streamer.rb
rack-app-7.6.2 lib/rack/app/streamer.rb
rack-app-7.6.1 lib/rack/app/streamer.rb
rack-app-7.6.0 lib/rack/app/streamer.rb
rack-app-7.5.2 lib/rack/app/streamer.rb
rack-app-7.5.1 lib/rack/app/streamer.rb
rack-app-7.5.0 lib/rack/app/streamer.rb
rack-app-7.4.0 lib/rack/app/streamer.rb