|
2 | 2 |
|
3 | 3 | module ViewComponent
|
4 | 4 | # 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 |
37 | 10 |
|
38 |
| - def respond_to_missing?(symbol, include_all) |
39 |
| - @_buffer.respond_to?(symbol, include_all) || super |
| 11 | + def subscribers |
| 12 | + @subscribers |
| 13 | + end |
40 | 14 | end
|
41 | 15 |
|
42 | 16 | module Patch
|
43 | 17 | 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 |
51 | 19 | def with_output_buffer(buf = nil) # :nodoc:
|
52 |
| - $count ||= 0 |
53 |
| - $count += 1 |
54 | 20 | unless buf
|
55 | 21 | buf = ActionView::OutputBuffer.new
|
56 | 22 | if output_buffer && output_buffer.respond_to?(:encoding)
|
57 | 23 | buf.force_encoding(output_buffer.encoding)
|
58 | 24 | end
|
59 | 25 | 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} |
63 | 28 | yield
|
64 |
| - @output_buffer.__buffer__ |
| 29 | + output_buffer |
65 | 30 | 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 } |
67 | 44 | end
|
68 | 45 | end
|
69 | 46 |
|
70 | 47 | included do
|
| 48 | + attr_accessor :global_buffer_coordinator |
| 49 | + |
71 | 50 | alias_method(:original_output_buffer=, :output_buffer=)
|
72 |
| - prepend SeparateGlobalModuleForHAMLCompat |
| 51 | + prepend Compatibility |
73 | 52 |
|
74 | 53 | alias_method(:original_with_output_buffer, :with_output_buffer)
|
75 | 54 | end
|
|
0 commit comments