lib/ably/realtime/presence.rb in ably-0.7.0 vs lib/ably/realtime/presence.rb in ably-0.7.1
- old
+ new
@@ -55,11 +55,11 @@
# @yield [Ably::Realtime::Presence] On success, will call the block with this {Ably::Realtime::Presence} object
# @return [EventMachine::Deferrable] Deferrable that supports both success (callback) and failure (errback) callbacks
#
def enter(options = {}, &success_block)
@client_id = options.fetch(:client_id, client_id)
- @data = options.fetch(:data, data)
+ @data = options.fetch(:data, nil)
deferrable = EventMachine::DefaultDeferrable.new
raise Ably::Exceptions::Standard.new('Unable to enter presence channel without a client_id', 400, 91000) unless client_id
return deferrable_succeed(deferrable, &success_block) if state == STATE.Entered
@@ -113,11 +113,11 @@
#
# @yield (see Presence#enter)
# @return (see Presence#enter)
#
def leave(options = {}, &success_block)
- @data = options.fetch(:data) if options.has_key?(:data)
+ @data = options.fetch(:data, data) # nil value defaults leave data to existing value
deferrable = EventMachine::DefaultDeferrable.new
raise Ably::Exceptions::Standard.new('Unable to leave presence channel that is not entered', 400, 91002) unless able_to_leave?
return deferrable_succeed(deferrable, &success_block) if state == STATE.Left
@@ -166,11 +166,11 @@
#
# @yield (see Presence#enter)
# @return (see Presence#enter)
#
def update(options = {}, &success_block)
- @data = options.fetch(:data) if options.has_key?(:data)
+ @data = options.fetch(:data, nil)
deferrable = EventMachine::DefaultDeferrable.new
ensure_channel_attached(deferrable) do
send_protocol_message_and_transition_state_to(
Ably::Models::PresenceMessage::ACTION.Update,
@@ -209,33 +209,32 @@
# @option options [String] :connection_id optional connection_id for the member
# @option options [String] :wait_for_sync defaults to true, if false the get method returns the current list of members and does not wait for the presence sync to complete
#
# @yield [Array<Ably::Models::PresenceMessage>] array of members or the member
#
- # @return [EventMachine::Deferrable] Deferrable that supports both success (callback) and failure (errback) callback
+ # @return [EventMachine::Deferrable] Deferrable that supports both success (callback) and failure (errback) callbacks
#
- def get(options = {}, &success_block)
+ def get(options = {})
wait_for_sync = options.fetch(:wait_for_sync, true)
deferrable = EventMachine::DefaultDeferrable.new
ensure_channel_attached(deferrable) do
result_block = proc do
members.map { |key, presence| presence }.tap do |filtered_members|
filtered_members.keep_if { |presence| presence.connection_id == options[:connection_id] } if options[:connection_id]
filtered_members.keep_if { |presence| presence.client_id == options[:client_id] } if options[:client_id]
+ end.tap do |current_members|
+ yield current_members if block_given?
+ deferrable.succeed current_members
end
end
if !wait_for_sync || sync_complete?
- result = result_block.call
- success_block.call result if block_given?
- deferrable.succeed result
+ result_block.call
else
sync_pubsub.once(:done) do
- result = result_block.call
- success_block.call result if block_given?
- deferrable.succeed result
+ result_block.call
end
sync_pubsub.once(:failed) do |error|
deferrable.fail error
end
@@ -484,11 +483,11 @@
deferrable
end
def send_protocol_message_and_transition_state_to(action, options = {}, &success_block)
deferrable = options.fetch(:deferrable) { raise ArgumentError, 'option :deferrable is required' }
- client_id = options.fetch(:client_id) { raise ArgumentError, 'option :client_id is required' }
+ client_id = options.fetch(:client_id) { raise ArgumentError, 'option :client_id is required' }
target_state = options.fetch(:target_state, nil)
failed_state = options.fetch(:failed_state, nil)
protocol_message_options = if options.has_key?(:data)
{ data: options.fetch(:data) }
@@ -507,17 +506,17 @@
deferrable_fail deferrable, error
end
end
end
- def deferrable_succeed(deferrable, *args, &block)
- block.call self, *args if block_given?
- EventMachine.next_tick { deferrable.succeed self, *args } # allow callback to be added to the returned Deferrable
+ def deferrable_succeed(deferrable, *args)
+ yield self, *args if block_given?
+ EventMachine.next_tick { deferrable.succeed self, *args } # allow callback to be added to the returned Deferrable before calling succeed
deferrable
end
- def deferrable_fail(deferrable, *args, &block)
- block.call self, *args if block_given?
+ def deferrable_fail(deferrable, *args)
+ yield self, *args if block_given?
EventMachine.next_tick { deferrable.fail self, *args } # allow errback to be added to the returned Deferrable
deferrable
end
def send_presence_action_for_client(action, client_id, options = {}, &success_block)