Skip to content

Commit 02bb7af

Browse files
committed
Revert to subscriber model
1 parent 7b0164b commit 02bb7af

File tree

3 files changed

+42
-60
lines changed

3 files changed

+42
-60
lines changed

lib/view_component/base.rb

+14-10
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,16 @@ def render_in(view_context, &block)
7373
# TODO flag with initializer
7474
if !self.__vc_original_view_context
7575
view_context.original_output_buffer = ActionView::OutputBuffer.new if !view_context.output_buffer
76-
77-
if !view_context.output_buffer.is_a?(GlobalBuffer)
78-
view_context.original_output_buffer = GlobalBuffer.new(view_context.output_buffer)
76+
self.__vc_original_view_context ||= view_context
77+
if self.__vc_original_view_context.global_buffer_coordinator.nil?
78+
coordinator = self.__vc_original_view_context.global_buffer_coordinator = GlobalBuffer::Coordinator.new
79+
coordinator.subscribers.push(self.__vc_original_view_context)
7980
end
8081
end
8182

82-
self.__vc_original_view_context ||= view_context
8383
@output_buffer = self.__vc_original_view_context.output_buffer
84+
self.global_buffer_coordinator = self.__vc_original_view_context.global_buffer_coordinator
85+
self.__vc_original_view_context.global_buffer_coordinator.subscribers.push(self)
8486

8587
@lookup_context ||= view_context.lookup_context
8688

@@ -112,14 +114,16 @@ def render_in(view_context, &block)
112114
@__vc_content_evaluated = false
113115
@__vc_render_in_block = block
114116

115-
before_render
117+
self.__vc_original_view_context.capture do
118+
before_render
116119

117-
if render?
118-
capture do
119-
render_template_for(@__vc_variant).to_s + _output_postamble
120+
if render?
121+
capture do
122+
render_template_for(@__vc_variant).to_s + _output_postamble
123+
end
124+
else
125+
""
120126
end
121-
else
122-
""
123127
end
124128
ensure
125129
@current_template = old_current_template

lib/view_component/global_buffer.rb

+28-49
Original file line numberDiff line numberDiff line change
@@ -2,74 +2,53 @@
22

33
module ViewComponent
44
# TODO if things break, log each time an unsafe string method is called
5-
class GlobalBuffer
6-
def initialize(buffer)
7-
@_buffer = buffer
8-
end
9-
10-
def __swap_buffer__(buffer)
11-
# TODO remove once this is debugged
12-
raise 'oh no' if buffer.kind_of?(GlobalBuffer)
13-
@_buffer = buffer
14-
end
15-
16-
def __buffer__
17-
@_buffer
18-
end
19-
20-
def to_s
21-
@_buffer.to_s
22-
end
23-
24-
def html_safe?
25-
@_buffer.html_safe?
26-
end
27-
28-
# Necessary for cases like `output_buffer = output_buffer.class.new(output_buffer)` (yes, this happens)
29-
def class
30-
@_buffer.class
31-
end
32-
33-
def method_missing(symbol, *args, **kwargs)
34-
@_buffer.send(symbol, *args, **kwargs)
35-
end
36-
ruby2_keywords(:method_missing) if respond_to?(:ruby2_keywords, true)
5+
module GlobalBuffer
6+
class Coordinator
7+
def initialize
8+
@subscribers = []
9+
end
3710

38-
def respond_to_missing?(symbol, include_all)
39-
@_buffer.respond_to?(symbol, include_all) || super
11+
def subscribers
12+
@subscribers
13+
end
4014
end
4115

4216
module Patch
4317
extend ActiveSupport::Concern
44-
45-
module SeparateGlobalModuleForHAMLCompat
46-
def output_buffer=(new_buf)
47-
# TODO make HAML work by falling back to super
48-
@output_buffer.__swap_buffer__(new_buf)
49-
end
50-
18+
module Compatibility
5119
def with_output_buffer(buf = nil) # :nodoc:
52-
$count ||= 0
53-
$count += 1
5420
unless buf
5521
buf = ActionView::OutputBuffer.new
5622
if output_buffer && output_buffer.respond_to?(:encoding)
5723
buf.force_encoding(output_buffer.encoding)
5824
end
5925
end
60-
61-
old_buffer = output_buffer.__buffer__
62-
@output_buffer.__swap_buffer__(buf)
26+
self.output_buffer, old_buffer = buf, output_buffer
27+
global_buffer_coordinator&.subscribers&.each { |s| s.output_buffer = buf}
6328
yield
64-
@output_buffer.__buffer__
29+
output_buffer
6530
ensure
66-
@output_buffer.__swap_buffer__(old_buffer)
31+
self.output_buffer = old_buffer
32+
global_buffer_coordinator&.subscribers&.each { |s| s.output_buffer = old_buffer }
33+
end
34+
35+
def _run(method, template, locals, buffer, add_to_stack: true, &block)
36+
_old_output_buffer, _old_virtual_path, _old_template = @output_buffer, @virtual_path, @current_template
37+
@current_template = template if add_to_stack
38+
@output_buffer = buffer
39+
global_buffer_coordinator&.subscribers&.each { |s| s.output_buffer = buffer }
40+
public_send(method, locals, buffer, &block)
41+
ensure
42+
@output_buffer, @virtual_path, @current_template = _old_output_buffer, _old_virtual_path, _old_template
43+
global_buffer_coordinator&.subscribers&.each { |s| s.output_buffer = _old_output_buffer }
6744
end
6845
end
6946

7047
included do
48+
attr_accessor :global_buffer_coordinator
49+
7150
alias_method(:original_output_buffer=, :output_buffer=)
72-
prepend SeparateGlobalModuleForHAMLCompat
51+
prepend Compatibility
7352

7453
alias_method(:original_with_output_buffer, :with_output_buffer)
7554
end
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
- binding.pry
21
.haml-div
32
= content
43
= @message

0 commit comments

Comments
 (0)