Method: TemplateStreaming::Controller#render_with_template_streaming

Defined in:
lib/template_streaming.rb

#render_with_template_streaming(*args, &block) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/template_streaming.rb', line 40

def render_with_template_streaming(*args, &block)
  options = args.first { |a| a.is_a?(Hash) }
  if options && options.size == 1 && options.key?(:stream)
    # Need to set the default values, since the standard #render won't.
    options[:template] = default_template
    options[:layout] = true
  end
  push_render_stack_frame do |stack_height|
    if start_streaming_template?(stack_height, *args)
      @streaming_template = true
      @template.streaming_template = true
      @performed_render = true
      @streaming_body = StreamingBody.new(template_streaming_threshold) do
        cookies.freeze
        if self.class.session_store.sent_with_headers?
          session.freeze
          flash.freeze
        end

        @performed_render = false
        last_piece = render_without_template_streaming(*args, &block)
        # The original render will clobber our response.body, so
        # we must push the buffer ourselves.
        push last_piece
      end
      response.body = @streaming_body
      response.prepare!
      form_authenticity_token  # generate now

      # Normally, the flash is swept on first reference. This
      # means we need to ensure it's referenced before the session
      # is persisted. In the case of the cookie store, that's when
      # the headers are sent, so we force a reference now.
      #
      # But alas, that's not all. @_flash is removed after
      # #perform_action, which means calling #flash in the view
      # would cause the flash to be referenced again, sweeping the
      # flash a second time. To prevent this, we preserve the
      # flash in a separate ivar, and patch #flash to return this
      # if we're streaming.
      #
      flash  # ensure sweep
      @template_streaming_flash = @_flash
      request.env[STREAMING_KEY] = true

      run_callbacks :when_streaming_template
    else
      render_without_template_streaming(*args, &block)
    end
  end
end