Class: React::Router::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/react/router/renderer.rb

Defined Under Namespace

Classes: PrerenderError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.react_props(args = {}) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/react/router/renderer.rb', line 31

def self.react_props(args={})
  if args.is_a? String
    args
  else
    args.to_json
  end
end

.render(routes, location, args = {}) ⇒ Object



25
26
27
28
29
# File 'lib/react/router/renderer.rb', line 25

def self.render(routes, location, args={})
  @@pool.with do |renderer|
    renderer.render(routes, location, args)
  end
end

.setup!(react_js, react_router_js, routes_js, args = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/react/router/renderer.rb', line 15

def self.setup!(react_js, react_router_js, routes_js, args={})
  @@react_js = react_js
  @@react_router_js = react_router_js
  @@routes_js = routes_js
  @@pool.shutdown {} if @@pool
  reset_combined_js!
  default_pool_options = {:size => 10, :timeout => 20}
  @@pool = ConnectionPool.new(default_pool_options.merge(args)) { self.new }
end

Instance Method Details

#contextObject



39
40
41
# File 'lib/react/router/renderer.rb', line 39

def context
  @context ||= ExecJS.compile(self.class.combined_js)
end

#render(routes, location, args = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/react/router/renderer.rb', line 43

def render(routes, location, args={})
  react_props = React::Router::Renderer.react_props(args)
  jscode = <<-JS
    function() {
      var str = '';
      ReactRouter.run(#{routes}, #{location.to_json}, function (Handler) {
        str = React.renderToString(React.createElement(Handler, #{react_props}));
      });
      return str;
    }()
  JS
  context.eval(jscode).html_safe
rescue ExecJS::ProgramError => e
  raise PrerenderError.new(routes, location, react_props, e)
end