From 73ede0bdf55346e1262d96049af7511009691367 Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Mon, 8 Jul 2024 13:14:23 +0530 Subject: [PATCH] Fixed channel detach implementation, updated according to spec --- lib/ably/realtime/channel.rb | 10 ++++----- lib/ably/realtime/channel/channel_manager.rb | 23 ++++---------------- 2 files changed, 9 insertions(+), 24 deletions(-) diff --git a/lib/ably/realtime/channel.rb b/lib/ably/realtime/channel.rb index af1772c0..d204c3ff 100644 --- a/lib/ably/realtime/channel.rb +++ b/lib/ably/realtime/channel.rb @@ -283,22 +283,22 @@ def attach(&success_block) # @return [Ably::Util::SafeDeferrable] Deferrable that supports both success (callback) and failure (errback) callback # def detach(&success_block) - if initialized? + if initialized? || detached? # RTL5a success_block.call if block_given? return Ably::Util::SafeDeferrable.new_and_succeed_immediately(logger) end - if failed? || connection.closing? || connection.failed? + if failed? || connection.closing? || connection.failed? # RTL5b, RTL5g return Ably::Util::SafeDeferrable.new_and_fail_immediately(logger, exception_for_state_change_to(:detaching)) end - if !detached? - if attaching? + connection.once_or_if :connected do # RTL5h + if attaching? || detaching? # Let the pending operation complete (#RTL5i) once_state_changed { transition_state_machine :detaching if can_transition_to?(:detaching) } elsif can_transition_to?(:detaching) transition_state_machine :detaching - else + else # RTL5j transition_state_machine! :detached end end diff --git a/lib/ably/realtime/channel/channel_manager.rb b/lib/ably/realtime/channel/channel_manager.rb index 90f00b9e..177c8665 100644 --- a/lib/ably/realtime/channel/channel_manager.rb +++ b/lib/ably/realtime/channel/channel_manager.rb @@ -243,25 +243,10 @@ def send_detach_protocol_message(previous_state) end end - on_disconnected_and_connected = lambda do - connection.unsafe_once(:disconnected) do - connection.unsafe_once(:connected) do - yield if pending_state_change_timer - end if pending_state_change_timer - end - end - - send_detach_message = lambda do - on_disconnected_and_connected.call do - send_detach_message.call - end - connection.send_protocol_message( - action: detach_action.to_i, - channel: channel.name - ) - end - - send_detach_message.call + connection.send_protocol_message_immediately( + action: detach_action.to_i, + channel: channel.name + ) end def logger