From ed92489c0df5dfd5b97c64f69637f3ce7a0122c5 Mon Sep 17 00:00:00 2001 From: Abanoub Ghadban Date: Thu, 20 Jun 2024 19:50:32 +0300 Subject: [PATCH] tmp --- lib/react_on_rails/helper.rb | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/react_on_rails/helper.rb b/lib/react_on_rails/helper.rb index b16ac119d5..65d7061d13 100644 --- a/lib/react_on_rails/helper.rb +++ b/lib/react_on_rails/helper.rb @@ -101,6 +101,16 @@ def stream_react_component(component_name, options = {}) ) end + def stream_react_component_async(component_name, options = {}) + Fiber.new do + stream = stream_react_component(component_name, options) + stream.each_chunk do |chunk| + Fiber.yield chunk + end + Fiber.yield nil + end + end + # react_component_hash is used to return multiple HTML strings for server rendering, such as for # adding meta-tags to a page. # It is exactly like react_component except for the following: @@ -376,6 +386,7 @@ def build_react_component_result_for_server_streamed_content( component_specification_tag: required("component_specification_tag"), render_options: required("render_options") ) + content_tag_options_html_tag = render_options.html_options[:tag] || "div" # The component_specification_tag is appended to the first chunk # We need to pass it early with the first chunk because it's needed in hydration # We need to make sure that client can hydrate the app early even before all components are streamed @@ -383,18 +394,16 @@ def build_react_component_result_for_server_streamed_content( rendered_html_stream = rendered_html_stream.transform do |chunk| if is_first_chunk is_first_chunk = false - next "#{chunk}\n#{component_specification_tag}" + next <<-HTML + #{rails_context_if_not_already_rendered} + #{component_specification_tag} + <#{content_tag_options_html_tag} id="#{render_options.dom_id}">#{chunk} + HTML end chunk end - content_tag_options_html_tag = render_options.html_options[:tag] || "div" - rendered_html_stream = rendered_html_stream.prepend { rails_context_if_not_already_rendered } - .prepend { "<#{content_tag_options_html_tag} id=\"#{render_options.dom_id}\">" } - .transform(&:html_safe) - - rendered_html_stream.append { "" } - .append { component_specification_tag } + rendered_html_stream.transform(&:html_safe) # TODO: handle console logs end