Module: ParallelRender::Helper

Defined in:
lib/parallel_render/helper.rb

Constant Summary collapse

PLACEHOLDER_PREFIX =
"<!--ASYNC-PLACEHOLDER:".freeze
PLACEHOLDER_SUFFIX =
"-->".freeze

Instance Method Summary collapse

Instance Method Details

#parallel_render(partial, locals = {}, context: controller.view_context) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/parallel_render/helper.rb', line 6

def parallel_render(partial, locals = {}, context: controller.view_context)
  token       = SecureRandom.hex(10)
  placeholder = "#{PLACEHOLDER_PREFIX}#{token}#{PLACEHOLDER_SUFFIX}".html_safe
  futures     = ParallelRender::Current.async_futures ||= {}

  futures[token] = Concurrent::Future.execute(executor: ASYNC_RENDER_EXECUTOR) do
    Rails.application.executor.wrap do
      begin
        # ApplicationController.renderer.render(partial:, locals:)
        context.render(partial:, locals:)
      rescue => e
        Rails.logger.error "Error rendering #{partial}: #{e.message}"
        e.backtrace.each { |line| Rails.logger.error "  #{line}" }
        if Rails.env.development?
          "<p style='background-color: red; color: white'>" +
          "Error rendering #{partial}: #{e.message}, #{e.backtrace.join("<br>")}" +
          "</p>"
        else
          raise(e)
        end
      end
    end
  end

  placeholder
end