Class: StimulusReflex::Channel

Inherits:
ActionCable::Channel::Base
  • Object
show all
Includes:
CableReady::Broadcaster
Defined in:
lib/stimulus_reflex/channel.rb

Overview

class StimulusReflex::Channel < ApplicationCable::Channel

Instance Method Summary collapse

Instance Method Details

#receive(data) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/stimulus_reflex/channel.rb', line 23

def receive(data)
  logger.debug "StimulusReflex::Channel#receive #{data.inspect}"
  start = Time.current
  url = data["url"].to_s
  target = data["target"].to_s
  stimulus_controller_name, stimulus_method_name = target.split("#")
  stimulus_controller_name = "#{stimulus_controller_name.classify}StimulusController"
  stimulus_controller = nil
  arguments = data["args"]

  begin
    ActiveSupport::Notifications.instrument "delegate_call.stimulus_reflex", url: url, target: target, arguments: arguments do
      stimulus_controller = stimulus_controller_name.constantize.new(self)
      if arguments.present?
        stimulus_controller.send stimulus_method_name, *arguments
      else
        stimulus_controller.send stimulus_method_name
      end
    end

    begin
      html = render_page(url, stimulus_controller)
      broadcast_morph extract_body_html(html)
    rescue StandardError => render_error
      logger.error "StimulusReflex::Channel: #{url} Failed to rerender #{params} after invoking #{target}! #{render_error} #{render_error.backtrace}"
    end
  rescue StandardError => invoke_error
    logger.error "StimulusReflex::Channel: #{url} Failed to invoke #{target}! #{invoke_error}"
  end
end

#stream_nameObject



12
13
14
15
16
17
# File 'lib/stimulus_reflex/channel.rb', line 12

def stream_name
  ids = connection.identifiers.map do |identifier|
    send(identifier).try(:id)
  end
  "#{channel_name}#{ids.join ":"}"
end

#subscribedObject



19
20
21
# File 'lib/stimulus_reflex/channel.rb', line 19

def subscribed
  stream_from stream_name
end