module Isomorfeus
module ReactViewHelper
def mount_component(component_name, props = {}, asset = 'application_ssr.js')
@ssr_response_status = nil
thread_id_asset = "#{Thread.current.object_id}#{asset}"
render_result = "
10
sleep 0.01
first_pass_finished = Isomorfeus.ssr_contexts[thread_id_asset].exec('return global.FirstPassFinished')
end
end
# wait for transport requests to finish
if first_pass_finished == 'transport'
transport_busy = Isomorfeus.ssr_contexts[thread_id_asset].exec('return global.Opal.Isomorfeus.Transport["$busy?"]()')
if transport_busy
start_time = Time.now
while transport_busy
break if (Time.now - start_time) > 10
sleep 0.01
transport_busy = Isomorfeus.ssr_contexts[thread_id_asset].exec('return global.Opal.Isomorfeus.Transport["$busy?"]()')
end
end
end
end
# build javascript for second render pass
javascript = <<~JAVASCRIPT
var rendered_tree = global.Opal.Isomorfeus.TopLevel.$render_component_to_string('#{component_name}', #{Oj.dump(props, mode: :strict)})
var application_state = global.Opal.Isomorfeus.store.native.getState();
if (typeof global.Opal.Isomorfeus.Transport !== 'undefined') { global.Opal.Isomorfeus.Transport.$disconnect(); }
return [rendered_tree, application_state, global.Opal.Isomorfeus['$ssr_response_status']()];
JAVASCRIPT
# execute second render pass
rendered_tree, application_state, @ssr_response_status = Isomorfeus.ssr_contexts[thread_id_asset].exec(javascript)
# build result
render_result << " data-iso-state='#{Oj.dump(application_state, mode: :strict)}'>"
render_result << rendered_tree
else
render_result << '>'
end
render_result << '
'
render_result
end
def ssr_response_status
@ssr_response_status || 200
end
end
end