lib/phlex/sgml/state.rb

Lines

100%

103 of 103 covered

Branches

100%

32 of 32 covered

Methods

100%

12 of 12 covered

1# frozen_string_literal: true
2
31class Phlex::SGML::State
41 def initialize(user_context: {}, output_buffer:, fragments:)
52445 @buffer = +""
62445 @flushed_bytesize = 0
72445 @capturing = false
82445 @user_context = user_context
92445 @fragments = fragments
102445 @fragment_depth = 0
112445 @should_render = !fragments
122445 @cache_stack = []
132445 @halt_signal = nil
142445 @output_buffer = output_buffer
152445 @attribute_cache = Phlex.__attribute_cache__
16 end
17
181 attr_accessor :capturing, :user_context
19
201 attr_reader :fragments, :fragment_depth, :output_buffer, :buffer, :should_render, :attribute_cache
21
22 # Kept as a stored boolean so the check is a plain attribute read.
231 alias_method :should_render?, :should_render
24
251 def around_render(component)
262530 if !@fragments || @halt_signal
272376 yield
28 else
29154 catch do |signal|
30154 @halt_signal = signal
31154 yield
32 end
33 end
34 end
35
361 def begin_fragment(id)
371401 if @fragments&.include?(id)
3840 @fragment_depth += 1
3940 @should_render = true
40 end
41
421401 if caching?
431294 current_byte_offset = 0 # Start tracking the byte offset of this fragment from the start of the cache buffer
441294 cache_stack.reverse_each do |(cache_buffer, fragment_map, open_fragments)| # We'll iterate deepest to shallowest
451346 current_byte_offset += cache_buffer.bytesize # Add the length of the cache buffer to the current byte offset
461346 fragment_map[id] = [current_byte_offset, nil, []] # Record the byte offset, length, and store a list of the nested fragments
47
48 # Visit only open ancestors; allocate descendant sets on their first child.
491346 open_fragments.delete(id)
501412 open_fragments.each_key { |name| (open_fragments[name] ||= Set.new) << id }
511346 open_fragments[id] = nil
52 end
53 end
54 end
55
561 def end_fragment(id, halt: true)
571401 if caching?
581294 byte_length = nil
591294 cache_stack.reverse_each do |(cache_buffer, fragment_map, open_fragments)| # We'll iterate deepest to shallowest
601346 byte_length ||= cache_buffer.bytesize - fragment_map[id][0] # The byte length is the difference between the current byte offset and the byte offset of the fragment
611346 fragment_map[id][1] = byte_length # All cache contexts use the same byte length
621346 if (descendants = open_fragments.delete(id))
6357 fragment_map[id][2] = descendants.to_a
64 end
65 end
66 end
67
681401 return unless @fragments&.include?(id)
69
7040 @fragments.delete(id)
7140 @fragment_depth -= 1
7240 @should_render = @fragment_depth > 0
73 # Don't replace an exception or nonlocal exit with the render halt signal.
7440 throw @halt_signal if halt && @fragments.length == 0
75 end
76
771 def record_fragment(id, offset, length, nested_fragments)
782261 return unless caching?
79
801154 cache_stack.reverse_each do |(cache_buffer, fragment_map, open_fragments)|
811154 offset += cache_buffer.bytesize
821154 fragment_map[id] = [offset, length, nested_fragments]
83
841154 open_fragments.delete(id)
852225 open_fragments.each_key { |name| (open_fragments[name] ||= Set.new) << id }
86 end
87 end
88
891 def caching(&)
90110 result = nil
91
92110 capture(cache: true) do
93110 @cache_stack.push([buffer, {}, {}].freeze)
94 begin
95110 yield
96108 result = @cache_stack.last.first(2)
97 ensure
98110 @cache_stack.pop
99 end
100 end
101
102108 result
103 end
104
1051 def caching?
1066258 !(cache_stack = @cache_stack) || cache_stack.length > 0
107 end
108
109 # A capture has no cache stack until it needs to track a fragment.
110 # While it has none, `buffer` is the buffer being captured into.
1111 private def cache_stack
1123852 @cache_stack ||= [[buffer, {}, {}]]
113 end
114
1151 def capture(cache: false)
1161351 new_buffer = +""
1171351 original_buffer = @buffer
1181351 original_capturing = @capturing
1191351 original_fragments = @fragments
1201351 original_should_render = @should_render
1211351 original_cache_stack = cache ? cache_stack : @cache_stack
1221351 captured_cache_stack = nil
123
124 begin
1251351 @buffer = new_buffer
1261351 @capturing = true
1271351 @fragments = nil
1281351 @should_render = true
129 # Captured output may be inserted later, elsewhere, or not at all.
130 # Track its fragments locally until the original string is inserted.
1311351 @cache_stack = nil unless cache
1321351 yield
133 ensure
1341351 captured_cache_stack = @cache_stack unless cache
1351351 @buffer = original_buffer
1361351 @capturing = original_capturing
1371351 @fragments = original_fragments
1381351 @should_render = original_should_render
1391351 @cache_stack = original_cache_stack
140 end
141
1421333 if captured_cache_stack && !(fragment_map = captured_cache_stack[0][1]).empty?
143 # Let the string own its metadata so discarded captures can be collected
144 # without losing metadata for strings that will be inserted again.
1451201 new_buffer.instance_variable_set(:@__phlex_fragments, [new_buffer.dup.freeze, fragment_map])
146 end
147
1481333 new_buffer
149 end
150
151 # Flushing moves bytes out of the buffer without changing how much was written.
152 # Read through `buffer` so overrides that redirect output (e.g. phlex-rails) are measured too.
1531 def output_bytesize
15410824 @flushed_bytesize + buffer.bytesize
155 end
156
1571 def append(content)
158 # Changed strings no longer have reliable fragment offsets.
1591195 if caching? && (captured = content.instance_variable_get(:@__phlex_fragments)) && captured[0] == content
1601086 captured[1].each do |id, (offset, length, nested_fragments)|
1611091 record_fragment(id, offset, length, nested_fragments)
162 end
163 end
164
1651195 buffer << content
166 end
167
1681 def flush
16990 return if capturing
170
17188 buffer = @buffer
17288 @output_buffer << buffer.dup
173
17488 @flushed_bytesize += buffer.bytesize
17588 buffer.clear
17688 nil
177 end
178end