lib/ably/realtime/channel.rb in ably-0.8.3 vs lib/ably/realtime/channel.rb in ably-0.8.4
- old
+ new
@@ -44,10 +44,11 @@
:detached,
:failed
)
include Ably::Modules::StateEmitter
include Ably::Modules::UsesStateMachine
+ ensure_state_machine_emits 'Ably::Models::ChannelStateChange'
# Max number of messages to bundle in a single ProtocolMessage
MAX_PROTOCOL_MESSAGE_BATCH_SIZE = 50
# {Ably::Realtime::Client} associated with this channel
@@ -104,10 +105,11 @@
#
# When publishing a message, if the channel is not attached, the channel is implicitly attached
#
# @param name [String, Array<Ably::Models::Message|Hash>, nil] The event name of the message to publish, or an Array of [Ably::Model::Message] objects or [Hash] objects with +:name+ and +:data+ pairs
# @param data [String, ByteArray, nil] The message payload unless an Array of [Ably::Model::Message] objects passed in the first argument
+ # @param attributes [Hash, nil] Optional additional message attributes such as :client_id or :connection_id, applied when name attribute is nil or a string
#
# @yield [Ably::Models::Message,Array<Ably::Models::Message>] On success, will call the block with the {Ably::Models::Message} if a single message is publishde, or an Array of {Ably::Models::Message} when multiple messages are published
# @return [Ably::Util::SafeDeferrable] Deferrable that supports both success (callback) and failure (errback) callbacks
#
# @example
@@ -134,11 +136,11 @@
#
# channel.publish('click', 'body').errback do |message, error|
# puts "#{message.name} was not received, error #{error.message}"
# end
#
- def publish(name, data = nil, &success_block)
+ def publish(name, data = nil, attributes = {}, &success_block)
raise Ably::Exceptions::ChannelInactive.new('Cannot publish messages on a detached channel') if detached? || detaching?
raise Ably::Exceptions::ChannelInactive.new('Cannot publish messages on a failed channel') if failed?
if !connection.can_publish_messages?
raise Ably::Exceptions::MessageQueueingDisabled.new("Message cannot be published. Client is configured to disallow queueing of messages and connection is currently #{connection.state}")
@@ -147,11 +149,11 @@
messages = if name.kind_of?(Enumerable)
name
else
ensure_utf_8 :name, name, allow_nil: true
ensure_supported_payload data
- [{ name: name, data: data }]
+ [{ name: name, data: data }.merge(attributes)]
end
queue_messages(messages).tap do |deferrable|
deferrable.callback &success_block if block_given?
end
@@ -188,11 +190,13 @@
#
# @yield [Ably::Realtime::Channel] Block is called as soon as this channel is in the Attached state
# @return [Ably::Util::SafeDeferrable] Deferrable that supports both success (callback) and failure (errback) callback
#
def attach(&success_block)
- raise Ably::Exceptions::InvalidStateChange.new("Cannot ATTACH channel when the connection is in a closed, suspended or failed state. Connection state: #{connection.state}") if connection.closing? || connection.closed? || connection.suspended? || connection.failed?
+ if connection.closing? || connection.closed? || connection.suspended? || connection.failed?
+ raise Ably::Exceptions::InvalidStateChange.new("Cannot ATTACH channel when the connection is in a closed, suspended or failed state. Connection state: #{connection.state}")
+ end
transition_state_machine :attaching if can_transition_to?(:attaching)
deferrable_for_state_change_to(STATE.Attached, &success_block)
end
@@ -282,9 +286,13 @@
# Used by {Ably::Modules::StateEmitter} to debug state changes
# @api private
def logger
client.logger
end
+
+ # As we are using a state machine, do not allow change_state to be used
+ # #transition_state_machine must be used instead
+ private :change_state
private
attr_reader :queue
def setup_event_handlers