diff options
author | Javan Makhmali <javan@javan.us> | 2015-11-06 15:34:31 -0500 |
---|---|---|
committer | Javan Makhmali <javan@javan.us> | 2015-11-06 15:34:31 -0500 |
commit | 63cdbf87ddaeb6bbc852de7ca2c504b7f6a22321 (patch) | |
tree | 4f7804c0459ec6d37e151610271d56efd4ee9fea | |
parent | e2ed68cc70925372681c2fb50b505b110825c8f7 (diff) | |
parent | 365891d5eff15a53919688ac78ac9af3edc0d664 (diff) | |
download | rails-63cdbf87ddaeb6bbc852de7ca2c504b7f6a22321.tar.gz rails-63cdbf87ddaeb6bbc852de7ca2c504b7f6a22321.tar.bz2 rails-63cdbf87ddaeb6bbc852de7ca2c504b7f6a22321.zip |
Merge pull request #117 from rails/shared-constants
Share internal identifiers and message types with the JavaScript client
-rw-r--r-- | lib/action_cable.rb | 10 | ||||
-rw-r--r-- | lib/action_cable/channel/base.rb | 7 | ||||
-rw-r--r-- | lib/action_cable/connection/base.rb | 2 | ||||
-rw-r--r-- | lib/assets/javascripts/cable.coffee | 11 | ||||
-rw-r--r-- | lib/assets/javascripts/cable.coffee.erb | 8 | ||||
-rw-r--r-- | lib/assets/javascripts/cable/connection.coffee | 15 | ||||
-rw-r--r-- | lib/assets/javascripts/cable/connection_monitor.coffee | 2 | ||||
-rw-r--r-- | lib/assets/javascripts/cable/subscriptions.coffee | 2 |
8 files changed, 30 insertions, 27 deletions
diff --git a/lib/action_cable.rb b/lib/action_cable.rb index 89ffa1fda7..3919812161 100644 --- a/lib/action_cable.rb +++ b/lib/action_cable.rb @@ -5,6 +5,16 @@ require 'action_cable/version' module ActionCable extend ActiveSupport::Autoload + INTERNAL = { + identifiers: { + ping: '_ping'.freeze + }, + message_types: { + confirmation: 'confirm_subscription'.freeze, + rejection: 'reject_subscription'.freeze + } + } + # Singleton instance of the server module_function def server @server ||= ActionCable::Server::Base.new diff --git a/lib/action_cable/channel/base.rb b/lib/action_cable/channel/base.rb index b0e112ce38..ca903a810d 100644 --- a/lib/action_cable/channel/base.rb +++ b/lib/action_cable/channel/base.rb @@ -89,9 +89,6 @@ module ActionCable include Naming include Broadcasting - SUBSCRIPTION_CONFIRMATION_INTERNAL_MESSAGE = 'confirm_subscription'.freeze - SUBSCRIPTION_REJECTION_INTERNAL_MESSAGE = 'reject_subscription'.freeze - attr_reader :params, :connection, :identifier delegate :logger, to: :connection @@ -258,7 +255,7 @@ module ActionCable def transmit_subscription_confirmation unless subscription_confirmation_sent? logger.info "#{self.class.name} is transmitting the subscription confirmation" - connection.transmit ActiveSupport::JSON.encode(identifier: @identifier, type: SUBSCRIPTION_CONFIRMATION_INTERNAL_MESSAGE) + connection.transmit ActiveSupport::JSON.encode(identifier: @identifier, type: ActionCable::INTERNAL[:message_types][:confirmation]) @subscription_confirmation_sent = true end end @@ -270,7 +267,7 @@ module ActionCable def transmit_subscription_rejection logger.info "#{self.class.name} is transmitting the subscription rejection" - connection.transmit ActiveSupport::JSON.encode(identifier: @identifier, type: SUBSCRIPTION_REJECTION_INTERNAL_MESSAGE) + connection.transmit ActiveSupport::JSON.encode(identifier: @identifier, type: ActionCable::INTERNAL[:message_types][:rejection]) end end end diff --git a/lib/action_cable/connection/base.rb b/lib/action_cable/connection/base.rb index a988060d56..6df168e4c3 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 ActiveSupport::JSON.encode(identifier: '_ping', message: Time.now.to_i) + transmit ActiveSupport::JSON.encode(identifier: ActionCable::INTERNAL[:identifiers][:ping], message: Time.now.to_i) end diff --git a/lib/assets/javascripts/cable.coffee b/lib/assets/javascripts/cable.coffee deleted file mode 100644 index fca5e095b5..0000000000 --- a/lib/assets/javascripts/cable.coffee +++ /dev/null @@ -1,11 +0,0 @@ -#= require_self -#= require cable/consumer - -@Cable = - PING_IDENTIFIER: "_ping" - INTERNAL_MESSAGES: - SUBSCRIPTION_CONFIRMATION: 'confirm_subscription' - SUBSCRIPTION_REJECTION: 'reject_subscription' - - createConsumer: (url) -> - new Cable.Consumer url diff --git a/lib/assets/javascripts/cable.coffee.erb b/lib/assets/javascripts/cable.coffee.erb new file mode 100644 index 0000000000..8498233c11 --- /dev/null +++ b/lib/assets/javascripts/cable.coffee.erb @@ -0,0 +1,8 @@ +#= require_self +#= require cable/consumer + +@Cable = + INTERNAL: <%= ActionCable::INTERNAL.to_json %> + + createConsumer: (url) -> + new Cable.Consumer url diff --git a/lib/assets/javascripts/cable/connection.coffee b/lib/assets/javascripts/cable/connection.coffee index 7823240587..b2abe8dcb2 100644 --- a/lib/assets/javascripts/cable/connection.coffee +++ b/lib/assets/javascripts/cable/connection.coffee @@ -1,4 +1,7 @@ # Encapsulate the cable connection held by the consumer. This is an internal class not intended for direct user manipulation. + +{message_types} = Cable.INTERNAL + class Cable.Connection @reopenDelay: 500 @@ -54,17 +57,13 @@ class Cable.Connection message: (event) -> {identifier, message, type} = JSON.parse(event.data) - if type? - @onTypeMessage(type) - else - @consumer.subscriptions.notify(identifier, "received", message) - - onTypeMessage: (type) -> switch type - when Cable.INTERNAL_MESSAGES.SUBSCRIPTION_CONFIRMATION + when message_types.confirmation @consumer.subscriptions.notify(identifier, "connected") - when Cable.INTERNAL_MESSAGES.SUBSCRIPTION_REJECTION + when message_types.rejection @consumer.subscriptions.reject(identifier) + else + @consumer.subscriptions.notify(identifier, "received", message) open: -> @disconnected = false diff --git a/lib/assets/javascripts/cable/connection_monitor.coffee b/lib/assets/javascripts/cable/connection_monitor.coffee index bf99dee34d..435efcc361 100644 --- a/lib/assets/javascripts/cable/connection_monitor.coffee +++ b/lib/assets/javascripts/cable/connection_monitor.coffee @@ -7,7 +7,7 @@ class Cable.ConnectionMonitor @staleThreshold: 6 # Server::Connections::BEAT_INTERVAL * 2 (missed two pings) - identifier: Cable.PING_IDENTIFIER + identifier: Cable.INTERNAL.identifiers.ping constructor: (@consumer) -> @consumer.subscriptions.add(this) diff --git a/lib/assets/javascripts/cable/subscriptions.coffee b/lib/assets/javascripts/cable/subscriptions.coffee index 13db32eb2c..7955565f06 100644 --- a/lib/assets/javascripts/cable/subscriptions.coffee +++ b/lib/assets/javascripts/cable/subscriptions.coffee @@ -63,7 +63,7 @@ class Cable.Subscriptions sendCommand: (subscription, command) -> {identifier} = subscription - if identifier is Cable.PING_IDENTIFIER + if identifier is Cable.INTERNAL.identifiers.ping @consumer.connection.isOpen() else @consumer.send({command, identifier}) |