Skip to content

Commit 00c9552

Browse files
add helper functions to reduce boiler-plate code
1 parent 04a263b commit 00c9552

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

lib/react_on_rails/helper.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,26 @@ def stream_react_component(component_name, options = {})
101101
)
102102
end
103103

104+
def stream_react_component_async(component_name, options = {})
105+
rendering_fiber = Fiber.new do
106+
stream = stream_react_component(component_name, options)
107+
stream.each_chunk do |chunk|
108+
Fiber.yield chunk
109+
end
110+
Fiber.yield nil
111+
end
112+
113+
if @rorp_rendering_fibers.nil?
114+
raise ReactOnRails::Error, "You must call stream_view_containing_react_components to render the view containing the react component"
115+
end
116+
@rorp_rendering_fibers << rendering_fiber
117+
118+
# return the first chunk of the fiber
119+
# It contains the initial html of the component
120+
# all updates will be appended to the stream sent to browser
121+
rendering_fiber.resume
122+
end
123+
104124
# react_component_hash is used to return multiple HTML strings for server rendering, such as for
105125
# adding meta-tags to a page.
106126
# It is exactly like react_component except for the following:

0 commit comments

Comments
 (0)