Skip to content

Commit a11de73

Browse files
committed
refactor(Controller hooks) use around-actions
1 parent d2545c1 commit a11de73

File tree

3 files changed

+16
-22
lines changed

3 files changed

+16
-22
lines changed

lib/react/rails/component_mount.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ def setup(controller)
1818
end
1919

2020
def teardown(controller)
21-
if controller.__prerenderer
22-
React::ServerRendering.checkin_renderer(controller.__prerenderer)
23-
end
2421
end
2522

2623
# Render a UJS-type HTML tag annotated with data attributes, which
@@ -60,12 +57,15 @@ module ControllerHelpers
6057
attr_accessor :__prerenderer
6158
end
6259

63-
# If you want a per-request renderer, add this method as a before-action
60+
# If you want a per-request renderer, add this method as an around-action
6461
#
6562
# @example Having one renderer instance for each controller action
66-
# before_action :checkout_renderer
67-
def checkout_prerenderer
68-
self.__prerenderer = React::ServerRendering.checkout_renderer
63+
# around_action :per_request_react_prerenderer
64+
def per_request_react_prerenderer
65+
React::ServerRendering.with_renderer do |renderer|
66+
self.__prerenderer = renderer
67+
yield
68+
end
6969
end
7070
end
7171

lib/react/rails/controller_lifecycle.rb

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,19 @@ module ControllerLifecycle
77

88
included do
99
# use both names to support Rails 3..5
10-
before_action_with_fallback = respond_to?(:before_action) ? :before_action : :before_filter
11-
after_action_with_fallback = respond_to?(:after_action) ? :after_action : :after_filter
12-
public_send(before_action_with_fallback, :setup_react_component_helper)
13-
public_send(after_action_with_fallback, :teardown_react_component_helper)
10+
around_action_with_fallback = respond_to?(:around_action) ? :around_action : :around_filter
11+
public_send(around_action_with_fallback, :use_react_component_helper)
1412
attr_reader :__react_component_helper
1513
end
1614

1715
# Instantiate the ViewHelper implementation and call its #setup method
18-
def setup_react_component_helper
16+
# then let the controller action run,
17+
# then call the ViewHelper implementation's #teardown method
18+
def use_react_component_helper
1919
new_helper = React::Rails::ViewHelper.helper_implementation_class.new
2020
new_helper.setup(self)
2121
@__react_component_helper = new_helper
22-
end
23-
24-
# Call the ViewHelper implementation's #teardown method
25-
def teardown_react_component_helper
22+
yield
2623
@__react_component_helper.teardown(self)
2724
end
2825
end

lib/react/server_rendering.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@ def self.render(component_name, props, prerender_options)
2121
end
2222
end
2323

24-
def self.checkout_renderer
25-
@@pool.checkout
26-
end
27-
28-
def self.checkin_renderer(renderer)
29-
@@pool.checkin
24+
# Yield a renderer for an arbitrary block
25+
def self.with_renderer
26+
@@pool.with { |renderer| yield(renderer) }
3027
end
3128

3229
class PrerenderError < RuntimeError

0 commit comments

Comments
 (0)