Class: LiveCable::RenderContext

Inherits:
Object
  • Object
show all
Defined in:
lib/live_cable/render_context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#childrenArray<LiveCable::Component> (readonly)

Returns:



15
16
17
# File 'lib/live_cable/render_context.rb', line 15

def children
  @children
end

#children_by_partHash<Integer, Array<LiveCable::Component>> (readonly)

Returns:



24
25
26
# File 'lib/live_cable/render_context.rb', line 24

def children_by_part
  @children_by_part
end

#componentLiveCable::Component (readonly)



18
19
20
# File 'lib/live_cable/render_context.rb', line 18

def component
  @component
end

#render_resultsHash<String, RenderResult> (readonly)

Returns:

  • (Hash<String, RenderResult>)


21
22
23
# File 'lib/live_cable/render_context.rb', line 21

def render_results
  @render_results
end

Instance Method Details

#<<(result) ⇒ Object

Parameters:

  • result (RenderResult)


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

#clearObject



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?

Returns:



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_connectionLiveCable::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.

Parameters:

Returns:



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

Returns:

  • (Boolean)


45
46
47
# File 'lib/live_cable/render_context.rb', line 45

def root?
  root.nil?
end