diff options
author | Kasper Timm Hansen <kaspth@gmail.com> | 2015-07-30 07:33:13 +0200 |
---|---|---|
committer | Kasper Timm Hansen <kaspth@gmail.com> | 2015-07-30 07:33:13 +0200 |
commit | 0c30d87868a8b59c47acbc695ef5e37d6361b3cc (patch) | |
tree | fa0c24e3149a06116079c392f3cea3c3928b05de | |
parent | 65fde3bf75cf82bf18d8cfa36bc07f52c9a866ff (diff) | |
parent | 60e2fa5e955ce819c90f2081320554b5ed0ee83c (diff) | |
download | rails-0c30d87868a8b59c47acbc695ef5e37d6361b3cc.tar.gz rails-0c30d87868a8b59c47acbc695ef5e37d6361b3cc.tar.bz2 rails-0c30d87868a8b59c47acbc695ef5e37d6361b3cc.zip |
Merge pull request #42 from lsylvester/channel-lookup-refactor
Optimize channel class lookup
-rw-r--r-- | lib/action_cable/connection/subscriptions.rb | 4 | ||||
-rw-r--r-- | lib/action_cable/server/base.rb | 4 | ||||
-rw-r--r-- | test/connection/subscriptions_test.rb | 2 |
3 files changed, 4 insertions, 6 deletions
diff --git a/lib/action_cable/connection/subscriptions.rb b/lib/action_cable/connection/subscriptions.rb index 0411d96413..69e3f60706 100644 --- a/lib/action_cable/connection/subscriptions.rb +++ b/lib/action_cable/connection/subscriptions.rb @@ -24,9 +24,7 @@ module ActionCable id_key = data['identifier'] id_options = ActiveSupport::JSON.decode(id_key).with_indifferent_access - subscription_klass = connection.server.channel_classes.detect do |channel_class| - channel_class == id_options[:channel].safe_constantize - end + subscription_klass = connection.server.channel_classes[id_options[:channel]] if subscription_klass subscriptions[id_key] ||= subscription_klass.new(connection, id_key, id_options) diff --git a/lib/action_cable/server/base.rb b/lib/action_cable/server/base.rb index b09fbf6da4..43849928b9 100644 --- a/lib/action_cable/server/base.rb +++ b/lib/action_cable/server/base.rb @@ -36,11 +36,11 @@ module ActionCable @worker_pool ||= ActionCable::Server::Worker.pool(size: config.worker_pool_size) end - # Requires and returns an array of all the channel class constants in this application. + # Requires and returns an hash of all the channel class constants keyed by name. def channel_classes @channel_classes ||= begin config.channel_paths.each { |channel_path| require channel_path } - config.channel_class_names.collect { |name| name.constantize } + config.channel_class_names.each_with_object({}) { |name, hash| hash[name] = name.constantize } end end diff --git a/test/connection/subscriptions_test.rb b/test/connection/subscriptions_test.rb index 4e134b6420..24fe8f9300 100644 --- a/test/connection/subscriptions_test.rb +++ b/test/connection/subscriptions_test.rb @@ -20,7 +20,7 @@ class ActionCable::Connection::SubscriptionsTest < ActiveSupport::TestCase setup do @server = TestServer.new - @server.stubs(:channel_classes).returns([ ChatChannel ]) + @server.stubs(:channel_classes).returns(ChatChannel.name => ChatChannel) env = Rack::MockRequest.env_for "/test", 'HTTP_CONNECTION' => 'upgrade', 'HTTP_UPGRADE' => 'websocket' @connection = Connection.new(@server, env) |