Module: LiveCable::Component::Rendering

Extended by:
ActiveSupport::Concern
Included in:
LiveCable::Component
Defined in:
lib/live_cable/component/rendering.rb

Instance Method Summary collapse

Instance Method Details

#renderObject



18
19
20
21
# File 'lib/live_cable/component/rendering.rb', line 18

def render
  @rendered = true
  ApplicationController.renderer.render(self, layout: false)
end

#render_in(view_context) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/live_cable/component/rendering.rb', line 40

def render_in(view_context)
  view, render_context = view_context.with_render_context(self) do
    result = view_context.render(template: to_partial_path, locals:)

    unless result.is_a?(LiveCable::Rendering::Partial)
      warn(
        "[LiveCable Warning] #{to_partial_path} was rendered without using a .live.erb template, " \
        'this will be less performant.'
      )

      next result
    end

    # Track which template we're rendering
    current_template_path = to_partial_path

    # Initialize static_sent hash and track previous template
    @static_sent ||= {}
    @previous_template_path ||= nil

    # Detect template change - if template changed, force dynamic render
    template_changed = previous_template_path && previous_template_path != current_template_path

    # Determine if we should send diffs or full render
    changes = if static_sent[current_template_path]
                # Static already sent for this template
                template_changed ? :dynamic : live_connection&.changeset_for(self)
              else
                :all
              end

    @previous_template_path = current_template_path

    result.for_component(self, view_context).render_changes(changes)
  end

  unless (partial = view.is_a?(Array))
    view = [view]
  end

  # If we are rendering the initial page, we only need to add the attributes to most parent element
  # as it will re-render again anyway when the socket is available. On this render when the live connection
  # is available, the components can be stored properly, so when each of their sockets connects, it won't need
  # to render them again. This prevents multiple re-renders of children when the parent renders, then renders
  # again, and then the child socket connects and renders that again as well.
  if (render_context.root? || live_connection) && !view[0].nil?
    view[0] = insert_root_attributes(view[0], view_context)
  end

  if previous_render_context
    # Children from skipped parts are preserved — their part simply didn't
    # re-evaluate this cycle, so they haven't actually gone away.
    preserved = render_context.preserved_children_from(previous_render_context)
    destroyed = previous_render_context.children - render_context.children - preserved
    destroyed.each(&:destroy)
  end

  # We didn't get a LiveCable Partial, so just return it with one part
  unless partial
    if live_connection && render_context.root?
      return LiveCable::Rendering::RenderResult.new(live_id, view, to_partial_path)
    else
      return view[0]
    end
  end

  if live_connection
    @static_sent[previous_template_path] = true

    @previous_render_context = render_context
    result = LiveCable::Rendering::RenderResult.new(live_id, view, to_partial_path)

    if render_context.root?
      result.child_results = render_context.render_results

      return result
    else
      # Always use LiveCable placeholder for non-root children
      # This allows JavaScript to store the component before subscription connects
      render_context << result

      return "<LiveCable child-live-id=\"#{live_id}\"></LiveCable>".html_safe
    end
  end

  view_context.safe_join(view)
end

#rendered_childrenArray<LiveCable::Component>

Returns:



129
130
131
# File 'lib/live_cable/component/rendering.rb', line 129

def rendered_children
  previous_render_context&.children || []
end

#to_partial_pathObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/live_cable/component/rendering.rb', line 23

def to_partial_path
  base = self.class.name.underscore

  if self.class.is_compound
    state = variant
    raise LiveCable::Error, "#{self.class.name}#variant must return a non-nil value" if state.nil?

    "#{base}/#{state}"
  else
    base
  end
end

#variantObject



36
37
38
# File 'lib/live_cable/component/rendering.rb', line 36

def variant
  'component'
end