mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Devolve blanket #require to reveal intent and responsibility
* Move specific requires close to where they're needed. * Use the private active_support/rails dep to wrap up common needs like eager autoload and module delegation. * Use a single Rails engine rather than an engine and a railtie. * Prefer `AS::JSON.encode` to `Object#to_json`.
This commit is contained in:
parent
d7ab5c8f1f
commit
acfdcf5568
18 changed files with 103 additions and 76 deletions
|
@ -1,34 +1,21 @@
|
|||
require 'eventmachine'
|
||||
EventMachine.epoll if EventMachine.epoll?
|
||||
EventMachine.kqueue if EventMachine.kqueue?
|
||||
|
||||
require 'set'
|
||||
|
||||
require 'active_support'
|
||||
require 'active_support/json'
|
||||
require 'active_support/concern'
|
||||
require 'active_support/core_ext/hash/indifferent_access'
|
||||
require 'active_support/core_ext/module/delegation'
|
||||
require 'active_support/callbacks'
|
||||
|
||||
require 'faye/websocket'
|
||||
require 'celluloid'
|
||||
require 'em-hiredis'
|
||||
require 'redis'
|
||||
|
||||
require 'action_cable/engine' if defined?(Rails)
|
||||
require 'action_cable/railtie' if defined?(Rails)
|
||||
|
||||
require 'active_support/rails'
|
||||
require 'action_cable/version'
|
||||
|
||||
module ActionCable
|
||||
autoload :Server, 'action_cable/server'
|
||||
autoload :Connection, 'action_cable/connection'
|
||||
autoload :Channel, 'action_cable/channel'
|
||||
autoload :RemoteConnections, 'action_cable/remote_connections'
|
||||
extend ActiveSupport::Autoload
|
||||
|
||||
# Singleton instance of the server
|
||||
module_function def server
|
||||
@server ||= ActionCable::Server::Base.new
|
||||
end
|
||||
|
||||
eager_autoload do
|
||||
autoload :Server
|
||||
autoload :Connection
|
||||
autoload :Channel
|
||||
autoload :RemoteConnections
|
||||
end
|
||||
end
|
||||
|
||||
require 'action_cable/engine' if defined?(Rails)
|
||||
|
|
|
@ -1,10 +1,14 @@
|
|||
module ActionCable
|
||||
module Channel
|
||||
autoload :Base, 'action_cable/channel/base'
|
||||
autoload :Broadcasting, 'action_cable/channel/broadcasting'
|
||||
autoload :Callbacks, 'action_cable/channel/callbacks'
|
||||
autoload :Naming, 'action_cable/channel/naming'
|
||||
autoload :PeriodicTimers, 'action_cable/channel/periodic_timers'
|
||||
autoload :Streams, 'action_cable/channel/streams'
|
||||
extend ActiveSupport::Autoload
|
||||
|
||||
eager_autoload do
|
||||
autoload :Base
|
||||
autoload :Broadcasting
|
||||
autoload :Callbacks
|
||||
autoload :Naming
|
||||
autoload :PeriodicTimers
|
||||
autoload :Streams
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
require 'set'
|
||||
|
||||
module ActionCable
|
||||
module Channel
|
||||
# The channel provides the basic structure of grouping behavior into logical units when communicating over the WebSocket connection.
|
||||
|
@ -159,7 +161,7 @@ module ActionCable
|
|||
# the proper channel identifier marked as the recipient.
|
||||
def transmit(data, via: nil)
|
||||
logger.info "#{self.class.name} transmitting #{data.inspect}".tap { |m| m << " (via #{via})" if via }
|
||||
connection.transmit({ identifier: @identifier, message: data }.to_json)
|
||||
connection.transmit ActiveSupport::JSON.encode(identifier: @identifier, message: data)
|
||||
end
|
||||
|
||||
|
||||
|
|
|
@ -1,12 +1,16 @@
|
|||
module ActionCable
|
||||
module Connection
|
||||
autoload :Authorization, 'action_cable/connection/authorization'
|
||||
autoload :Base, 'action_cable/connection/base'
|
||||
autoload :Identification, 'action_cable/connection/identification'
|
||||
autoload :InternalChannel, 'action_cable/connection/internal_channel'
|
||||
autoload :MessageBuffer, 'action_cable/connection/message_buffer'
|
||||
autoload :WebSocket, 'action_cable/connection/web_socket'
|
||||
autoload :Subscriptions, 'action_cable/connection/subscriptions'
|
||||
autoload :TaggedLoggerProxy, 'action_cable/connection/tagged_logger_proxy'
|
||||
extend ActiveSupport::Autoload
|
||||
|
||||
eager_autoload do
|
||||
autoload :Authorization
|
||||
autoload :Base
|
||||
autoload :Identification
|
||||
autoload :InternalChannel
|
||||
autoload :MessageBuffer
|
||||
autoload :WebSocket
|
||||
autoload :Subscriptions
|
||||
autoload :TaggedLoggerProxy
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -119,7 +119,7 @@ module ActionCable
|
|||
end
|
||||
|
||||
def beat
|
||||
transmit({ identifier: '_ping', message: Time.now.to_i }.to_json)
|
||||
transmit ActiveSupport::JSON.encode(identifier: '_ping', message: Time.now.to_i)
|
||||
end
|
||||
|
||||
|
||||
|
@ -203,7 +203,7 @@ module ActionCable
|
|||
request.filtered_path,
|
||||
websocket.possible? ? ' [WebSocket]' : '',
|
||||
request.ip,
|
||||
Time.now.to_default_s ]
|
||||
Time.now.to_s ]
|
||||
end
|
||||
|
||||
def finished_request_message
|
||||
|
@ -211,7 +211,7 @@ module ActionCable
|
|||
request.filtered_path,
|
||||
websocket.possible? ? ' [WebSocket]' : '',
|
||||
request.ip,
|
||||
Time.now.to_default_s ]
|
||||
Time.now.to_s ]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
require 'set'
|
||||
|
||||
module ActionCable
|
||||
module Connection
|
||||
module Identification
|
||||
|
@ -31,7 +33,13 @@ module ActionCable
|
|||
|
||||
private
|
||||
def connection_gid(ids)
|
||||
ids.map { |o| (o.try(:to_global_id) || o).to_s }.sort.join(":")
|
||||
ids.map do |o|
|
||||
if o.respond_to? :to_global_id
|
||||
o.to_global_id
|
||||
else
|
||||
o.to_s
|
||||
end
|
||||
end.sort.join(":")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
require 'active_support/core_ext/hash/indifferent_access'
|
||||
|
||||
module ActionCable
|
||||
module Connection
|
||||
# Collection class for all the channel subscriptions established on a given connection. Responsible for routing incoming commands that arrive on
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
require 'faye/websocket'
|
||||
|
||||
module ActionCable
|
||||
module Connection
|
||||
# Decorate the Faye::WebSocket with helpers we need.
|
||||
|
|
|
@ -1,4 +1,22 @@
|
|||
require 'rails/engine'
|
||||
require 'active_support/ordered_options'
|
||||
|
||||
module ActionCable
|
||||
class Engine < ::Rails::Engine
|
||||
config.action_cable = ActiveSupport::OrderedOptions.new
|
||||
|
||||
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
|
||||
|
||||
options.allowed_request_origins ||= "http://localhost:3000" if ::Rails.env.development?
|
||||
|
||||
ActiveSupport.on_load(:action_cable) do
|
||||
options.each { |k,v| send("#{k}=", v) }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,19 +0,0 @@
|
|||
module ActionCable
|
||||
class Railtie < Rails::Railtie
|
||||
config.action_cable = ActiveSupport::OrderedOptions.new
|
||||
|
||||
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
|
||||
|
||||
options.allowed_request_origins ||= "http://localhost:3000" if ::Rails.env.development?
|
||||
|
||||
ActiveSupport.on_load(:action_cable) do
|
||||
options.each { |k,v| send("#{k}=", v) }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,11 +1,19 @@
|
|||
require 'eventmachine'
|
||||
EventMachine.epoll if EventMachine.epoll?
|
||||
EventMachine.kqueue if EventMachine.kqueue?
|
||||
|
||||
module ActionCable
|
||||
module Server
|
||||
autoload :Base, 'action_cable/server/base'
|
||||
autoload :Broadcasting, 'action_cable/server/broadcasting'
|
||||
autoload :Connections, 'action_cable/server/connections'
|
||||
autoload :Configuration, 'action_cable/server/configuration'
|
||||
extend ActiveSupport::Autoload
|
||||
|
||||
autoload :Worker, 'action_cable/server/worker'
|
||||
autoload :ActiveRecordConnectionManagement, 'action_cable/server/worker/active_record_connection_management'
|
||||
eager_autoload do
|
||||
autoload :Base
|
||||
autoload :Broadcasting
|
||||
autoload :Connections
|
||||
autoload :Configuration
|
||||
|
||||
autoload :Worker
|
||||
autoload :ActiveRecordConnectionManagement, 'action_cable/server/worker/active_record_connection_management'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
require 'em-hiredis'
|
||||
|
||||
module ActionCable
|
||||
module Server
|
||||
# A singleton ActionCable::Server instance is available via ActionCable.server. It's used by the rack process that starts the cable server, but
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
require 'redis'
|
||||
|
||||
module ActionCable
|
||||
module Server
|
||||
# Broadcasting is how other parts of your application can send messages to the channel subscribers. As explained in Channel, most of the time, these
|
||||
|
@ -44,9 +46,9 @@ module ActionCable
|
|||
|
||||
def broadcast(message)
|
||||
server.logger.info "[ActionCable] Broadcasting to #{broadcasting}: #{message}"
|
||||
server.broadcasting_redis.publish broadcasting, message.to_json
|
||||
server.broadcasting_redis.publish broadcasting, ActiveSupport::JSON.encode(message)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
require 'active_support/core_ext/hash/indifferent_access'
|
||||
|
||||
module ActionCable
|
||||
module Server
|
||||
# An instance of this configuration object is available via ActionCable.server.config, which allows you to tweak the configuration points
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
require 'celluloid'
|
||||
require 'active_support/callbacks'
|
||||
|
||||
module ActionCable
|
||||
module Server
|
||||
# Worker used by Server.send_async to do connection work in threads. Only for internal use.
|
||||
|
@ -36,4 +39,4 @@ module ActionCable
|
|||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -40,7 +40,7 @@ class ActionCable::Connection::IdentifierTest < ActionCable::TestCase
|
|||
open_connection_with_stubbed_pubsub
|
||||
|
||||
@connection.websocket.expects(:close)
|
||||
message = { 'type' => 'disconnect' }.to_json
|
||||
message = ActiveSupport::JSON.encode('type' => 'disconnect')
|
||||
@connection.process_internal_message message
|
||||
end
|
||||
end
|
||||
|
@ -50,7 +50,7 @@ class ActionCable::Connection::IdentifierTest < ActionCable::TestCase
|
|||
open_connection_with_stubbed_pubsub
|
||||
|
||||
@connection.websocket.expects(:close).never
|
||||
message = { 'type' => 'unknown' }.to_json
|
||||
message = ActiveSupport::JSON.encode('type' => 'unknown')
|
||||
@connection.process_internal_message message
|
||||
end
|
||||
end
|
||||
|
|
|
@ -27,7 +27,7 @@ class ActionCable::Connection::SubscriptionsTest < ActionCable::TestCase
|
|||
@server = TestServer.new
|
||||
@server.stubs(:channel_classes).returns(ChatChannel.name => ChatChannel)
|
||||
|
||||
@chat_identifier = { id: 1, channel: 'ActionCable::Connection::SubscriptionsTest::ChatChannel' }.to_json
|
||||
@chat_identifier = ActiveSupport::JSON.encode(id: 1, channel: 'ActionCable::Connection::SubscriptionsTest::ChatChannel')
|
||||
end
|
||||
|
||||
test "subscribe command" do
|
||||
|
@ -77,7 +77,7 @@ class ActionCable::Connection::SubscriptionsTest < ActionCable::TestCase
|
|||
channel = subscribe_to_chat_channel
|
||||
|
||||
data = { 'content' => 'Hello World!', 'action' => 'speak' }
|
||||
@subscriptions.execute_command 'command' => 'message', 'identifier' => @chat_identifier, 'data' => data.to_json
|
||||
@subscriptions.execute_command 'command' => 'message', 'identifier' => @chat_identifier, 'data' => ActiveSupport::JSON.encode(data)
|
||||
|
||||
assert_equal [ data ], channel.lines
|
||||
end
|
||||
|
@ -89,7 +89,7 @@ class ActionCable::Connection::SubscriptionsTest < ActionCable::TestCase
|
|||
|
||||
channel1 = subscribe_to_chat_channel
|
||||
|
||||
channel2_id = { id: 2, channel: 'ActionCable::Connection::SubscriptionsTest::ChatChannel' }.to_json
|
||||
channel2_id = ActiveSupport::JSON.encode(id: 2, channel: 'ActionCable::Connection::SubscriptionsTest::ChatChannel')
|
||||
channel2 = subscribe_to_chat_channel(channel2_id)
|
||||
|
||||
channel1.expects(:unsubscribe_from_channel)
|
||||
|
|
|
@ -18,10 +18,12 @@ ActiveSupport.test_order = :sorted
|
|||
# Require all the stubs and models
|
||||
Dir[File.dirname(__FILE__) + '/stubs/*.rb'].each {|file| require file }
|
||||
|
||||
require 'celluloid'
|
||||
$CELLULOID_DEBUG = false
|
||||
$CELLULOID_TEST = false
|
||||
Celluloid.logger = Logger.new(StringIO.new)
|
||||
|
||||
require 'faye/websocket'
|
||||
class << Faye::WebSocket
|
||||
remove_method :ensure_reactor_running
|
||||
|
||||
|
|
Loading…
Reference in a new issue