lib/submodules/ably-ruby/lib/ably/realtime/presence.rb in ably-rest-0.8.2 vs lib/submodules/ably-ruby/lib/ably/realtime/presence.rb in ably-rest-0.8.3

- old
+ new

@@ -80,10 +80,11 @@ @data = data @client_id = client_id return deferrable_succeed(deferrable, &success_block) if state == STATE.Entered + ensure_presence_publishable_on_connection ensure_channel_attached(deferrable) do if entering? once_or_if(STATE.Entered, else: proc { |args| deferrable_fail deferrable, *args }) do deferrable_succeed deferrable, &success_block end @@ -143,10 +144,11 @@ @data = data return deferrable_succeed(deferrable, &success_block) if state == STATE.Left + ensure_presence_publishable_on_connection ensure_channel_attached(deferrable) do if leaving? once_or_if(STATE.Left, else: proc { |error|deferrable_fail deferrable, *args }) do deferrable_succeed deferrable, &success_block end @@ -199,10 +201,11 @@ ensure_supported_payload data unless data.nil? raise Ably::Exceptions::Standard.new('Unable to update presence channel without a client_id', 400, 91000) unless client_id @data = data + ensure_presence_publishable_on_connection ensure_channel_attached(deferrable) do send_protocol_message_and_transition_state_to( Ably::Models::PresenceMessage::ACTION.Update, deferrable: deferrable, target_state: STATE.Entered, @@ -367,10 +370,16 @@ Ably::Models::PresenceMessage.new(model, logger: logger).tap do |presence_message| presence_message.encode self.channel end end + def ensure_presence_publishable_on_connection + 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}") + end + end + def ensure_channel_attached(deferrable = nil) if channel.attached? yield else attach_channel_then { yield } @@ -411,33 +420,38 @@ def deferrable_fail(deferrable, *args, &block) safe_yield block, self, *args if block_given? EventMachine.next_tick { deferrable.fail self, *args } # allow errback to be added to the returned Deferrable deferrable - end + end def send_presence_action_for_client(action, client_id, options = {}, &success_block) - deferrable = create_deferrable + ensure_presence_publishable_on_connection + deferrable = create_deferrable ensure_channel_attached(deferrable) do send_presence_protocol_message(action, client_id, options).tap do |protocol_message| protocol_message.callback { |message| deferrable_succeed deferrable, &success_block } protocol_message.errback { |message, error| deferrable_fail deferrable, error } end end end def attach_channel_then if channel.detached? || channel.failed? - raise Ably::Exceptions::IncompatibleStateForOperation.new("Operation is not allowed when channel is in #{channel.state}", 400, 91001) + raise Ably::Exceptions::InvalidStateChange.new("Operation is not allowed when channel is in #{channel.state}", 400, 91001) else channel.unsafe_once(Channel::STATE.Attached) { yield } channel.attach end end def client channel.client + end + + def connection + client.connection end def rest_presence client.rest_client.channel(channel.name).presence end