2017-07-16 13:10:15 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-12-15 07:10:27 -05:00
|
|
|
require "rails"
|
|
|
|
require "action_cable"
|
2017-10-21 09:24:35 -04:00
|
|
|
require "action_cable/helpers/action_cable_helper"
|
2015-12-16 09:28:50 -05:00
|
|
|
require "active_support/core_ext/hash/indifferent_access"
|
2015-10-16 04:02:22 -04:00
|
|
|
|
2015-02-12 10:17:26 -05:00
|
|
|
module ActionCable
|
2016-05-04 07:47:57 -04:00
|
|
|
class Engine < Rails::Engine # :nodoc:
|
2015-10-16 04:02:22 -04:00
|
|
|
config.action_cable = ActiveSupport::OrderedOptions.new
|
2016-03-03 21:27:52 -05:00
|
|
|
config.action_cable.mount_path = ActionCable::INTERNAL[:default_mount_path]
|
2015-12-16 12:15:59 -05:00
|
|
|
|
2015-12-15 07:10:27 -05:00
|
|
|
config.eager_load_namespaces << ActionCable
|
2015-10-16 04:02:22 -04:00
|
|
|
|
2015-12-14 12:54:08 -05:00
|
|
|
initializer "action_cable.helpers" do
|
|
|
|
ActiveSupport.on_load(:action_view) do
|
|
|
|
include ActionCable::Helpers::ActionCableHelper
|
|
|
|
end
|
2015-09-03 20:17:48 -04:00
|
|
|
end
|
|
|
|
|
2015-10-16 04:02:22 -04:00
|
|
|
initializer "action_cable.logger" do
|
|
|
|
ActiveSupport.on_load(:action_cable) { self.logger ||= ::Rails.logger }
|
|
|
|
end
|
|
|
|
|
|
|
|
initializer "action_cable.set_configs" do |app|
|
|
|
|
options = app.config.action_cable
|
2016-06-16 16:04:43 -04:00
|
|
|
options.allowed_request_origins ||= /https?:\/\/localhost:\d+/ if ::Rails.env.development?
|
2015-10-16 04:02:22 -04:00
|
|
|
|
2016-01-06 17:16:02 -05:00
|
|
|
app.paths.add "config/cable", with: "config/cable.yml"
|
2015-12-16 09:42:35 -05:00
|
|
|
|
2015-10-16 04:02:22 -04:00
|
|
|
ActiveSupport.on_load(:action_cable) do
|
2016-01-06 17:16:02 -05:00
|
|
|
if (config_path = Pathname.new(app.config.paths["config/cable"].first)).exist?
|
2016-01-15 17:26:17 -05:00
|
|
|
self.cable = Rails.application.config_for(config_path).with_indifferent_access
|
2015-12-16 09:42:35 -05:00
|
|
|
end
|
2015-12-16 09:31:25 -05:00
|
|
|
|
2017-01-05 03:20:57 -05:00
|
|
|
previous_connection_class = connection_class
|
2016-08-06 13:13:46 -04:00
|
|
|
self.connection_class = -> { "ApplicationCable::Connection".safe_constantize || previous_connection_class.call }
|
2016-02-05 16:48:48 -05:00
|
|
|
|
2016-10-28 23:05:58 -04:00
|
|
|
options.each { |k, v| send("#{k}=", v) }
|
2015-10-16 04:02:22 -04:00
|
|
|
end
|
|
|
|
end
|
2016-02-04 22:33:46 -05:00
|
|
|
|
|
|
|
initializer "action_cable.routes" do
|
|
|
|
config.after_initialize do |app|
|
|
|
|
config = app.config
|
|
|
|
unless config.action_cable.mount_path.nil?
|
|
|
|
app.routes.prepend do
|
|
|
|
mount ActionCable.server => config.action_cable.mount_path, internal: true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-02-24 23:01:19 -05:00
|
|
|
|
|
|
|
initializer "action_cable.set_work_hooks" do |app|
|
|
|
|
ActiveSupport.on_load(:action_cable) do
|
|
|
|
ActionCable::Server::Worker.set_callback :work, :around, prepend: true do |_, inner|
|
|
|
|
app.executor.wrap do
|
|
|
|
# If we took a while to get the lock, we may have been halted
|
|
|
|
# in the meantime. As we haven't started doing any real work
|
|
|
|
# yet, we should pretend that we never made it off the queue.
|
|
|
|
unless stopping?
|
|
|
|
inner.call
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
wrap = lambda do |_, inner|
|
|
|
|
app.executor.wrap(&inner)
|
|
|
|
end
|
|
|
|
ActionCable::Channel::Base.set_callback :subscribe, :around, prepend: true, &wrap
|
|
|
|
ActionCable::Channel::Base.set_callback :unsubscribe, :around, prepend: true, &wrap
|
|
|
|
|
|
|
|
app.reloader.before_class_unload do
|
|
|
|
ActionCable.server.restart
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-02-12 10:17:26 -05:00
|
|
|
end
|
|
|
|
end
|