@@ -18,7 +18,8 @@ def self.setup!(react_js, components_js, args={})
1818 @@components_js = components_js
1919 @@pool . shutdown { } if @@pool
2020 reset_combined_js!
21- @@pool = ConnectionPool . new ( :size => args [ :size ] ||10 , :timeout => args [ :timeout ] ||20 ) { self . new }
21+ default_pool_options = { :size => 10 , :timeout => 20 }
22+ @@pool = ConnectionPool . new ( default_pool_options . merge ( args ) ) { self . new }
2223 end
2324
2425 def self . render ( component , args = { } )
@@ -27,6 +28,33 @@ def self.render(component, args={})
2728 end
2829 end
2930
31+ def self . react_props ( args = { } )
32+ if args . is_a? String
33+ args
34+ else
35+ args . to_json
36+ end
37+ end
38+
39+ def context
40+ @context ||= ExecJS . compile ( self . class . combined_js )
41+ end
42+
43+ def render ( component , args = { } )
44+ react_props = React ::Renderer . react_props ( args )
45+ jscode = <<-JS
46+ function() {
47+ return React.renderToString(React.createElement(#{ component } , #{ react_props } ));
48+ }()
49+ JS
50+ context . eval ( jscode ) . html_safe
51+ rescue ExecJS ::ProgramError => e
52+ raise PrerenderError . new ( component , react_props , e )
53+ end
54+
55+
56+ private
57+
3058 def self . setup_combined_js
3159 <<-CODE
3260 var global = global || this;
@@ -54,28 +82,5 @@ def self.combined_js
5482 @@combined_js
5583 end
5684
57- def self . react_props ( args = { } )
58- if args . is_a? String
59- args
60- else
61- args . to_json
62- end
63- end
64-
65- def context
66- @context ||= ExecJS . compile ( self . class . combined_js )
67- end
68-
69- def render ( component , args = { } )
70- react_props = React ::Renderer . react_props ( args )
71- jscode = <<-JS
72- function() {
73- return React.renderToString(React.createElement(#{ component } , #{ react_props } ));
74- }()
75- JS
76- context . eval ( jscode ) . html_safe
77- rescue ExecJS ::ProgramError => e
78- raise PrerenderError . new ( component , react_props , e )
79- end
8085 end
8186end
0 commit comments