Skip to content

Commit 193fd1b

Browse files
DRY helper functions
1 parent d975912 commit 193fd1b

File tree

1 file changed

+39
-29
lines changed

1 file changed

+39
-29
lines changed

lib/react_on_rails/helper.rb

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -124,40 +124,15 @@ def react_component(component_name, options = {})
124124
# @option options [Boolean] :raise_on_prerender_error Set to true to raise exceptions during server-side rendering
125125
# Any other options are passed to the content tag, including the id.
126126
def stream_react_component(component_name, options = {})
127-
unless ReactOnRails::Utils.react_on_rails_pro?
128-
raise ReactOnRails::Error,
129-
"You must use React on Rails Pro to use the stream_react_component method."
130-
end
131-
132-
if @rorp_rendering_fibers.nil?
133-
raise ReactOnRails::Error,
134-
"You must call stream_view_containing_react_components to render the view containing the react component"
135-
end
136-
137-
rendering_fiber = Fiber.new do
138-
stream = internal_stream_react_component(component_name, options)
139-
stream.each_chunk do |chunk|
140-
Fiber.yield chunk
141-
end
127+
run_stream_inside_fiber do
128+
internal_stream_react_component(component_name, options)
142129
end
143-
144-
@rorp_rendering_fibers << rendering_fiber
145-
146-
# return the first chunk of the fiber
147-
# It contains the initial html of the component
148-
# all updates will be appended to the stream sent to browser
149-
rendering_fiber.resume
150130
end
151131

152132
def rsc_react_component(component_name, options = {})
153-
rendering_fiber = Fiber.new do
154-
res = internal_rsc_react_component(component_name, options)
155-
res.each_chunk do |chunk|
156-
Fiber.yield chunk
157-
end
158-
Fiber.yield nil
133+
run_stream_inside_fiber do
134+
internal_rsc_react_component(component_name, options)
159135
end
160-
rendering_fiber
161136
end
162137

163138
# react_component_hash is used to return multiple HTML strings for server rendering, such as for
@@ -400,6 +375,32 @@ def load_pack_for_generated_component(react_component_name, render_options)
400375

401376
private
402377

378+
def run_stream_inside_fiber
379+
unless ReactOnRails::Utils.react_on_rails_pro?
380+
raise ReactOnRails::Error,
381+
"You must use React on Rails Pro to use the stream_react_component method."
382+
end
383+
384+
if @rorp_rendering_fibers.nil?
385+
raise ReactOnRails::Error,
386+
"You must call stream_view_containing_react_components to render the view containing the react component"
387+
end
388+
389+
rendering_fiber = Fiber.new do
390+
stream = yield
391+
stream.each_chunk do |chunk|
392+
Fiber.yield chunk
393+
end
394+
end
395+
396+
@rorp_rendering_fibers << rendering_fiber
397+
398+
# return the first chunk of the fiber
399+
# It contains the initial html of the component
400+
# all updates will be appended to the stream sent to browser
401+
rendering_fiber.resume
402+
end
403+
403404
def internal_stream_react_component(component_name, options = {})
404405
options = options.merge(stream?: true)
405406
result = internal_react_component(component_name, options)
@@ -410,6 +411,15 @@ def internal_stream_react_component(component_name, options = {})
410411
)
411412
end
412413

414+
def internal_rsc_react_component(react_component_name, options = {})
415+
options = options.merge(rsc?: true)
416+
render_options = create_render_options(react_component_name, options)
417+
json_stream = server_rendered_react_component(render_options)
418+
json_stream.transform do |chunk|
419+
chunk[:html].html_safe
420+
end
421+
end
422+
413423
def generated_components_pack_path(component_name)
414424
"#{ReactOnRails::PackerUtils.packer_source_entry_path}/generated/#{component_name}.js"
415425
end

0 commit comments

Comments
 (0)