1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/actionview/lib/action_view/flows.rb

75 lines
1.7 KiB
Ruby
Raw Normal View History

require "active_support/core_ext/string/output_safety"
module ActionView
2011-04-16 05:42:02 -04:00
class OutputFlow #:nodoc:
attr_reader :content
def initialize
@content = Hash.new { |h, k| h[k] = ActiveSupport::SafeBuffer.new }
end
2011-04-16 05:42:02 -04:00
# Called by _layout_for to read stored values.
def get(key)
@content[key]
end
2011-04-16 05:42:02 -04:00
# Called by each renderer object to set the layout contents.
def set(key, value)
@content[key] = ActiveSupport::SafeBuffer.new(value)
end
2011-04-16 05:42:02 -04:00
# Called by content_for
def append(key, value)
@content[key] << value
end
2012-01-22 08:12:26 -05:00
alias_method :append!, :append
end
2011-04-16 05:42:02 -04:00
class StreamingFlow < OutputFlow #:nodoc:
def initialize(view, fiber)
@view = view
@parent = nil
@child = view.output_buffer
@content = view.view_flow.content
@fiber = fiber
@root = Fiber.current.object_id
end
2013-06-25 14:34:51 -04:00
# Try to get stored content. If the content
# is not available and we're inside the layout fiber,
# then it will begin waiting for the given key and yield.
def get(key)
return super if @content.key?(key)
if inside_fiber?
view = @view
begin
@waiting_for = key
view.output_buffer, @parent = @child, view.output_buffer
Fiber.yield
ensure
@waiting_for = nil
view.output_buffer, @child = @parent, view.output_buffer
end
end
super
end
2011-04-16 05:42:02 -04:00
# Appends the contents for the given key. This is called
# by providing and resuming back to the fiber,
# if that's the key it's waiting for.
2011-04-16 05:34:07 -04:00
def append!(key, value)
super
@fiber.resume if @waiting_for == key
end
private
def inside_fiber?
Fiber.current.object_id != @root
end
end
2013-06-25 14:34:51 -04:00
end