diff options
author | Jeremy Daer <jeremydaer@gmail.com> | 2015-10-16 01:02:22 -0700 |
---|---|---|
committer | Jeremy Daer <jeremydaer@gmail.com> | 2015-10-16 09:06:44 -0700 |
commit | acfdcf556888a88dc2d2026553d585f920fae105 (patch) | |
tree | 9e9bbe0a5b43170d3495653315fe396e546e6dd0 /lib/action_cable | |
parent | d7ab5c8f1ffd04acd2cf610f5756fc18fdfc6160 (diff) | |
download | rails-acfdcf556888a88dc2d2026553d585f920fae105.tar.gz rails-acfdcf556888a88dc2d2026553d585f920fae105.tar.bz2 rails-acfdcf556888a88dc2d2026553d585f920fae105.zip |
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`.
Diffstat (limited to 'lib/action_cable')
-rw-r--r-- | lib/action_cable/channel.rb | 16 | ||||
-rw-r--r-- | lib/action_cable/channel/base.rb | 4 | ||||
-rw-r--r-- | lib/action_cable/connection.rb | 20 | ||||
-rw-r--r-- | lib/action_cable/connection/base.rb | 6 | ||||
-rw-r--r-- | lib/action_cable/connection/identification.rb | 10 | ||||
-rw-r--r-- | lib/action_cable/connection/subscriptions.rb | 2 | ||||
-rw-r--r-- | lib/action_cable/connection/web_socket.rb | 2 | ||||
-rw-r--r-- | lib/action_cable/engine.rb | 18 | ||||
-rw-r--r-- | lib/action_cable/railtie.rb | 19 | ||||
-rw-r--r-- | lib/action_cable/server.rb | 20 | ||||
-rw-r--r-- | lib/action_cable/server/base.rb | 2 | ||||
-rw-r--r-- | lib/action_cable/server/broadcasting.rb | 6 | ||||
-rw-r--r-- | lib/action_cable/server/configuration.rb | 2 | ||||
-rw-r--r-- | lib/action_cable/server/worker.rb | 5 |
14 files changed, 85 insertions, 47 deletions
diff --git a/lib/action_cable/channel.rb b/lib/action_cable/channel.rb index 3b973ba0a7..7ae262ce5f 100644 --- a/lib/action_cable/channel.rb +++ b/lib/action_cable/channel.rb @@ -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 diff --git a/lib/action_cable/channel/base.rb b/lib/action_cable/channel/base.rb index 17ac1a97af..df87064195 100644 --- a/lib/action_cable/channel/base.rb +++ b/lib/action_cable/channel/base.rb @@ -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 diff --git a/lib/action_cable/connection.rb b/lib/action_cable/connection.rb index 3d6ed6a6e8..b672e00682 100644 --- a/lib/action_cable/connection.rb +++ b/lib/action_cable/connection.rb @@ -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 diff --git a/lib/action_cable/connection/base.rb b/lib/action_cable/connection/base.rb index de27628d7d..9f74226f98 100644 --- a/lib/action_cable/connection/base.rb +++ b/lib/action_cable/connection/base.rb @@ -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 diff --git a/lib/action_cable/connection/identification.rb b/lib/action_cable/connection/identification.rb index 95863795dd..431493aa70 100644 --- a/lib/action_cable/connection/identification.rb +++ b/lib/action_cable/connection/identification.rb @@ -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 diff --git a/lib/action_cable/connection/subscriptions.rb b/lib/action_cable/connection/subscriptions.rb index 69e3f60706..229be2a316 100644 --- a/lib/action_cable/connection/subscriptions.rb +++ b/lib/action_cable/connection/subscriptions.rb @@ -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 diff --git a/lib/action_cable/connection/web_socket.rb b/lib/action_cable/connection/web_socket.rb index 135a28cfe4..169b683b8c 100644 --- a/lib/action_cable/connection/web_socket.rb +++ b/lib/action_cable/connection/web_socket.rb @@ -1,3 +1,5 @@ +require 'faye/websocket' + module ActionCable module Connection # Decorate the Faye::WebSocket with helpers we need. diff --git a/lib/action_cable/engine.rb b/lib/action_cable/engine.rb index 6c943c7971..613a9b99f2 100644 --- a/lib/action_cable/engine.rb +++ b/lib/action_cable/engine.rb @@ -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 diff --git a/lib/action_cable/railtie.rb b/lib/action_cable/railtie.rb deleted file mode 100644 index 0be6d19620..0000000000 --- a/lib/action_cable/railtie.rb +++ /dev/null @@ -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 diff --git a/lib/action_cable/server.rb b/lib/action_cable/server.rb index 2278509341..a2a89d5f1e 100644 --- a/lib/action_cable/server.rb +++ b/lib/action_cable/server.rb @@ -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 + + eager_autoload do + autoload :Base + autoload :Broadcasting + autoload :Connections + autoload :Configuration - autoload :Worker, 'action_cable/server/worker' - autoload :ActiveRecordConnectionManagement, 'action_cable/server/worker/active_record_connection_management' + autoload :Worker + autoload :ActiveRecordConnectionManagement, 'action_cable/server/worker/active_record_connection_management' + end end end diff --git a/lib/action_cable/server/base.rb b/lib/action_cable/server/base.rb index 5b7ddf4185..f1585dc776 100644 --- a/lib/action_cable/server/base.rb +++ b/lib/action_cable/server/base.rb @@ -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 diff --git a/lib/action_cable/server/broadcasting.rb b/lib/action_cable/server/broadcasting.rb index 037b98951e..6e0fbae387 100644 --- a/lib/action_cable/server/broadcasting.rb +++ b/lib/action_cable/server/broadcasting.rb @@ -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
\ No newline at end of file +end diff --git a/lib/action_cable/server/configuration.rb b/lib/action_cable/server/configuration.rb index 315782ec3e..b22de273b8 100644 --- a/lib/action_cable/server/configuration.rb +++ b/lib/action_cable/server/configuration.rb @@ -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 diff --git a/lib/action_cable/server/worker.rb b/lib/action_cable/server/worker.rb index 91496775b8..e063b2a2e1 100644 --- a/lib/action_cable/server/worker.rb +++ b/lib/action_cable/server/worker.rb @@ -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
\ No newline at end of file +end |