class Motion::Channel

Constants

ACTION_METHODS

Attributes

component_connection[R]

Public Class Methods

action_methods() click to toggle source

Don't use the ActionCable huertistic for deciding what actions can be called from JavaScript. Instead, hard-code the list so we can make other methods public without worrying about them being called from JavaScript.

# File lib/motion/channel.rb, line 19
def self.action_methods
  ACTION_METHODS
end

Public Instance Methods

process_broadcast(broadcast, message) click to toggle source
# File lib/motion/channel.rb, line 55
def process_broadcast(broadcast, message)
  component_connection.process_broadcast(broadcast, message)
  synchronize
end
process_motion(data) click to toggle source
# File lib/motion/channel.rb, line 48
def process_motion(data)
  motion, raw_event = data.values_at("name", "event")

  component_connection.process_motion(motion, Event.from_raw(raw_event))
  synchronize
end
process_periodic_timer(timer) click to toggle source
# File lib/motion/channel.rb, line 60
def process_periodic_timer(timer)
  component_connection.process_periodic_timer(timer)
  synchronize
end
subscribed() click to toggle source
# File lib/motion/channel.rb, line 25
def subscribed
  state, client_version = params.values_at("state", "version")

  if Gem::Version.new(Motion::VERSION) < Gem::Version.new(client_version)
    raise IncompatibleClientError.new(Motion::VERSION, client_version)
  end

  @component_connection =
    ComponentConnection.from_state(state, log_helper: log_helper)

  synchronize
rescue => error
  reject

  handle_error(error, "connecting a component")
end
unsubscribed() click to toggle source
# File lib/motion/channel.rb, line 42
def unsubscribed
  component_connection&.close

  @component_connection = nil
end

Private Instance Methods

handle_error(error, context) click to toggle source
# File lib/motion/channel.rb, line 79
def handle_error(error, context)
  log_helper.error("An error occurred while #{context}", error: error)
end
log_helper() click to toggle source
# File lib/motion/channel.rb, line 83
def log_helper
  @log_helper ||= LogHelper.for_channel(self)
end
renderer() click to toggle source

Memoize the renderer on the connection so that it can be shared accross all components. `ActionController::Renderer` is already thread-safe and designed to be reused.

# File lib/motion/channel.rb, line 90
def renderer
  connection.instance_eval do
    @_motion_renderer ||= Motion.build_renderer_for(self)
  end
end
synchronize() click to toggle source
# File lib/motion/channel.rb, line 67
def synchronize
  component_connection.if_render_required do |component|
    transmit(renderer.render(component, layout: nil))
  end

  streaming_from component_connection.broadcasts,
    to: :process_broadcast

  periodically_notify component_connection.periodic_timers,
    via: :process_periodic_timer
end