Código en sinatra/base.rb: la clase Stream

The close parameter specifies whether Stream#close should be called after the block has been executed. This is only relevant for evented servers like Thin or Rainbows.

método stream

Fichero sinatra/base.rb:

  class Stream
    ...
    def stream(keep_open = false)
      # Soporta el servidor callbacks asincronas? (webrick no, thin si)
      scheduler = env['async.callback'] ? EventMachine : Stream
      current   = @params.dup
      body Stream.new(scheduler, keep_open) { |out| 
        with_params(current) { yield(out) } # llamamos al bloque 
      }
    end
    ...
  end
Si keep_open es true el navegador se queda esperando datos y no ciera la conexión.

The body method set or retrieve the response body.

When a block is given, evaluation is deferred until the body is read with each.

    def body(value = nil, &block)
      if block_given?
        def block.each; yield(call) end
        response.body = block
      elsif value
        headers.delete 'Content-Length' unless request.head? || value.is_a?(Rack::File) || value.is_a?(Stream)
        response.body = value
      else
        response.body
      end
    end

initialize

class Stream
      def self.schedule(*) yield end
      def self.defer(*)    yield end

      def initialize(scheduler = self.class, keep_open = false, &back)
        @back, @scheduler, @keep_open = 
         back.to_proc, scheduler, keep_open
        @callbacks, @closed = [], false
      end
  ...
end

with_params

  class Stream 
    ...
    def with_params(temp_params)
      original, @params = @params, temp_params # save params
      yield   # call the block
    ensure
      @params = original if original # recover params
    end
  end



Subsecciones
Casiano Rodriguez León 2015-01-07