lib/async_partial.rb in async_partial-0.6.0 vs lib/async_partial.rb in async_partial-0.7.0

- old
+ new

@@ -1,22 +1,51 @@ # frozen_string_literal: true require_relative 'async_partial/railtie' module AsyncPartial - module Renderer - def render(context, options, block) - if (options.delete(:async) || (options[:locals]&.delete(:async))) + module PartialRenderer + private + + def render_partial + if @locals.delete :async AsyncResult.new(Thread.new { super }) else super end end + + def collection_with_template + super.map do |v| + v.value if AsyncPartial::AsyncResult === v + end + end end + module CollectionPartialTemplateRenderer + def render(view, locals, buffer = nil, &block) + locals = locals.dup + if locals.delete :async + AsyncResult.new(Thread.new { super }) + else + super + end + end + end + + module PerThreadBufferStack + def render(view, locals, buffer = nil, &block) + buffer ||= ActionView::OutputBuffer.new + (Thread.current[:output_buffers] ||= []).push buffer + result = super + Thread.current[:output_buffers].pop + result + end + end + module CaptureHelper def capture(*args, &block) - buf = block.binding.local_variable_get :output_buffer + buf = Thread.current[:output_buffers].last value = nil buffer = with_output_buffer(buf) { value = block.call(*args) } if (string = buffer.presence || value) && string.is_a?(String) ERB::Util.html_escape string end