Class: StimulusReflex::Channel

Inherits:
Object
  • Object
show all
Defined in:
app/channels/stimulus_reflex/channel.rb

Instance Method Summary collapse

Instance Method Details

#receive(data) ⇒ Object



13
14
15
16
17
18
19
20
21
22
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/channels/stimulus_reflex/channel.rb', line 13

def receive(data)
  begin
    reflex = StimulusReflex::ReflexFactory.new(self, data).call
    delegate_call_to_reflex reflex
  rescue => exception
    error = exception_with_backtrace(exception)

    if reflex
      error_message = "\e[31mReflex #{reflex.data.target} failed: #{error[:message]} [#{reflex.url}]\e[0m\n#{error[:stack]}"
      reflex.rescue_with_handler(exception)
      reflex.logger&.error error_message
      reflex.broadcast_error data: data, error: "#{exception} #{exception.backtrace.first.split(":in ")[0] if Rails.env.development?}"
    else
      error_message = "\e[31mReflex failed: #{error[:message]} \e[0m\n#{error[:stack]}"
      unless exception.is_a?(StimulusReflex::VersionMismatchError)
        StimulusReflex.config.logger.error error_message
      end

      if error_message.to_s.include? "No route matches"
        initializer_path = Rails.root.join("config", "initializers", "stimulus_reflex.rb")

        StimulusReflex.config.logger.warn "          \\e[33mNOTE: StimulusReflex failed to locate a matching route and could not re-render the page.\n\n          If your app uses Rack middleware to rewrite part of the request path, you must enable those middleware modules in StimulusReflex.\n          The StimulusReflex initializer should be located at \#{initializer_path}, or you can generate it with:\n\n            $ bundle exec rails generate stimulus_reflex:config\n\n          Configure any required middleware:\n\n            StimulusReflex.configure do |config|\n              config.middleware.use FirstRackMiddleware\n              config.middleware.use SecondRackMiddleware\n            end\\e[0m\n\n        NOTE\n      end\n    end\n    return\n  end\n\n  if reflex.halted?\n    reflex.broadcast_halt data: data\n  elsif reflex.forbidden?\n    reflex.broadcast_forbid data: data\n  else\n    begin\n      reflex.broadcast(reflex.selectors, data)\n    rescue => exception\n      reflex.rescue_with_handler(exception)\n      error = exception_with_backtrace(exception)\n      reflex.broadcast_error data: data, error: \"\#{exception} \#{exception.backtrace.first.split(\":in \")[0] if Rails.env.development?}\"\n      reflex.logger&.error \"\\e[31mReflex failed to re-render: \#{error[:message]} [\#{reflex.url}]\\e[0m\\n\#{error[:stack]}\"\n    end\n  end\nensure\n  if reflex\n    commit_session(reflex)\n    report_failed_basic_auth(reflex) if reflex.controller?\n    reflex.logger&.log_all_operations\n  end\nend\n"

#stream_nameObject



4
5
6
# File 'app/channels/stimulus_reflex/channel.rb', line 4

def stream_name
  [params[:channel], connection.connection_identifier].reject(&:blank?).join(":")
end

#subscribedObject



8
9
10
11
# File 'app/channels/stimulus_reflex/channel.rb', line 8

def subscribed
  super
  stream_from stream_name
end