Class: LiveCable::RenderContext
- Inherits:
-
Object
- Object
- LiveCable::RenderContext
- Defined in:
- lib/live_cable/render_context.rb
Instance Attribute Summary collapse
- #children ⇒ Array<LiveCable::Component> readonly
- #children_by_part ⇒ Hash<Integer, Array<LiveCable::Component>> readonly
- #component ⇒ LiveCable::Component readonly
- #render_results ⇒ Hash<String, RenderResult> readonly
Instance Method Summary collapse
- #<<(result) ⇒ Object
- #add_component(child) ⇒ Object
- #clear ⇒ Object
- #get_component(live_id) ⇒ LiveCable::Component?
-
#initialize(component, root: nil) ⇒ RenderContext
constructor
A new instance of RenderContext.
- #live_connection ⇒ LiveCable::Connection
-
#preserved_children_from(previous_context) ⇒ Array<LiveCable::Component>
Returns children from the previous context that came from parts which were skipped (not rendered) in this render cycle.
- #render_part(index) ⇒ Object
- #root? ⇒ Boolean
Constructor Details
#initialize(component, root: nil) ⇒ RenderContext
Returns a new instance of RenderContext.
5 6 7 8 9 10 11 12 |
# File 'lib/live_cable/render_context.rb', line 5 def initialize(component, root: nil) @component = component @children = [] @root = root @render_results = {} @children_by_part = {} @current_part = nil end |
Instance Attribute Details
#children ⇒ Array<LiveCable::Component> (readonly)
15 16 17 |
# File 'lib/live_cable/render_context.rb', line 15 def children @children end |
#children_by_part ⇒ Hash<Integer, Array<LiveCable::Component>> (readonly)
24 25 26 |
# File 'lib/live_cable/render_context.rb', line 24 def children_by_part @children_by_part end |
#component ⇒ LiveCable::Component (readonly)
18 19 20 |
# File 'lib/live_cable/render_context.rb', line 18 def component @component end |
#render_results ⇒ Hash<String, RenderResult> (readonly)
21 22 23 |
# File 'lib/live_cable/render_context.rb', line 21 def render_results @render_results end |
Instance Method Details
#<<(result) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/live_cable/render_context.rb', line 55 def <<(result) if root? render_results[result.live_id] = result else root << result end end |
#add_component(child) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/live_cable/render_context.rb', line 63 def add_component(child) return unless live_connection live_connection.add_component(child) children << child (@children_by_part[@current_part] ||= []) << child end |
#clear ⇒ Object
49 50 51 52 |
# File 'lib/live_cable/render_context.rb', line 49 def clear @children = nil @component = nil end |
#get_component(live_id) ⇒ LiveCable::Component?
72 73 74 |
# File 'lib/live_cable/render_context.rb', line 72 def get_component(live_id) live_connection&.get_component(live_id) end |
#live_connection ⇒ LiveCable::Connection
77 78 79 |
# File 'lib/live_cable/render_context.rb', line 77 def live_connection component.live_connection end |
#preserved_children_from(previous_context) ⇒ Array<LiveCable::Component>
Returns children from the previous context that came from parts which were skipped (not rendered) in this render cycle. These children should not be destroyed — their part simply didn't re-evaluate.
39 40 41 42 43 |
# File 'lib/live_cable/render_context.rb', line 39 def preserved_children_from(previous_context) previous_context.children_by_part.each_with_object([]) do |(part, part_children), preserved| preserved.concat(part_children) unless @children_by_part.key?(part) end end |
#render_part(index) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/live_cable/render_context.rb', line 26 def render_part(index) @current_part = index result = yield @children_by_part[index] ||= [] unless result.nil? result end |
#root? ⇒ Boolean
45 46 47 |
# File 'lib/live_cable/render_context.rb', line 45 def root? root.nil? end |