Module: LiveCable::Component::Lifecycle

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

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/live_cable/component/lifecycle.rb', line 57

def method_missing(method_name, *, &)
  if channel.nil? || channel.connection.identifiers.exclude?(method_name)
    return super
  end

  channel.connection.public_send(method_name, *, &)
end

Instance Method Details

#connect(channel) ⇒ Object

Parameters:

  • channel (ActionCable::Channel::Base)


23
24
25
26
27
28
# File 'lib/live_cable/component/lifecycle.rb', line 23

def connect(channel)
  run_callbacks :connect do
    @channel = channel
    @subscribed = true
  end
end

#destroyObject



43
44
45
46
47
48
# File 'lib/live_cable/component/lifecycle.rb', line 43

def destroy
  broadcast_destroy

  # Destroy children as well
  previous_render_context&.children&.map(&:destroy)
end

#disconnectObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/live_cable/component/lifecycle.rb', line 30

def disconnect
  run_callbacks :disconnect do
    stop_stream

    @channel = nil
    @previous_render_context&.clear
    @previous_render_context = nil
  end
ensure
  live_connection&.remove_component(self)
  @live_connection = nil
end

#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean

Allow the component to access the identified_by methods from the connection

Returns:

  • (Boolean)


51
52
53
54
55
# File 'lib/live_cable/component/lifecycle.rb', line 51

def respond_to_missing?(method_name, _include_private = false)
  return false unless channel

  channel.connection.identifiers.include?(method_name)
end

#statusObject



14
15
16
# File 'lib/live_cable/component/lifecycle.rb', line 14

def status
  subscribed? ? 'subscribed' : 'disconnected'
end

#subscribed?Boolean

Returns:

  • (Boolean)


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

def subscribed?
  @subscribed
end