From 7b4f9609366d25ef7ce58f0abe13c857cbe4063e Mon Sep 17 00:00:00 2001 From: Serhii Mamontov Date: Thu, 27 Jun 2024 02:50:25 +0300 Subject: [PATCH] refactor(subscribe): revert cancellation handler --- PubNub/Core/PubNub+Core.m | 9 ++-- PubNub/Core/PubNub+CorePrivate.h | 3 -- PubNub/Core/PubNub+Presence.m | 15 +++--- PubNub/Core/PubNub+Subscribe.m | 54 +++++-------------- PubNub/Core/PubNub+SubscribePrivate.h | 4 +- PubNub/Data/Managers/PNSubscriber.m | 7 ++- .../Integration/PNSubscribeIntegrationTest.m | 3 +- 7 files changed, 27 insertions(+), 68 deletions(-) diff --git a/PubNub/Core/PubNub+Core.m b/PubNub/Core/PubNub+Core.m index b0f27f9cf..7ad54ad9f 100644 --- a/PubNub/Core/PubNub+Core.m +++ b/PubNub/Core/PubNub+Core.m @@ -94,8 +94,6 @@ @interface PubNub () #pragma mark - Properties -// Handler which will be called when subscribe request will be cancelled. -@property(strong, nullable, nonatomic) dispatch_block_t subscribeCancellationHandler; /// Resources access lock. @property(strong, nonatomic) PNLock *lock; #ifndef PUBNUB_DISABLE_LOGGER @@ -320,7 +318,7 @@ - (void)copyWithConfiguration:(PNConfiguration *)configuration if ([self.subscriberManager allObjects].count) { // Stop any interactions on subscription loop. - [self cancelSubscribeOperationsWithCompletion:nil]; + [self cancelSubscribeOperations]; BOOL uuidChanged = ![configuration.userID isEqualToString:self.configuration.userID]; BOOL authKeyChanged = ((self.configuration.authKey && !configuration.authKey) || @@ -404,9 +402,8 @@ - (void)prepareReachability { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Warc-repeated-use-of-weak" [weakSelf.reachability stopServicePing]; - [weakSelf cancelSubscribeOperationsWithCompletion:^{ - [weakSelf.subscriberManager restoreSubscriptionCycleIfRequiredWithCompletion:nil]; - }]; + [weakSelf cancelSubscribeOperations]; + [weakSelf.subscriberManager restoreSubscriptionCycleIfRequiredWithCompletion:nil]; #pragma clang diagnostic pop } }]; diff --git a/PubNub/Core/PubNub+CorePrivate.h b/PubNub/Core/PubNub+CorePrivate.h index dfc5c8721..37dd1cb26 100644 --- a/PubNub/Core/PubNub+CorePrivate.h +++ b/PubNub/Core/PubNub+CorePrivate.h @@ -48,9 +48,6 @@ typedef void(^PNParsedRequestCompletionBlock)(PNTransportRequest *, #pragma mark - Properties -// Handler which will be called when subscribe request will be cancelled. -@property(strong, nullable, nonatomic) dispatch_block_t subscribeCancellationHandler; - /// Recent client state (whether it was connected or not). @property (nonatomic, readonly, assign) PNStatusCategory recentClientStatus; diff --git a/PubNub/Core/PubNub+Presence.m b/PubNub/Core/PubNub+Presence.m index 56bb647fb..d1efd6c20 100644 --- a/PubNub/Core/PubNub+Presence.m +++ b/PubNub/Core/PubNub+Presence.m @@ -412,14 +412,13 @@ - (void)setConnected:(BOOL)connected [self heartbeatWithCompletion:block]; [self.heartbeatManager startHeartbeatIfRequired]; } else { - [self cancelSubscribeOperationsWithCompletion:^{ - [self.subscriberManager unsubscribeFromChannels:presenceChannels - groups:presenceChannelGroups - withQueryParameters:nil - listenersNotification:NO - completion:^(PNSubscribeStatus *status) { - if (block) block((id)status); - }]; + [self cancelSubscribeOperations]; + [self.subscriberManager unsubscribeFromChannels:presenceChannels + groups:presenceChannelGroups + withQueryParameters:nil + listenersNotification:NO + completion:^(PNSubscribeStatus *status) { + if (block) block((id)status); }]; } } else { diff --git a/PubNub/Core/PubNub+Subscribe.m b/PubNub/Core/PubNub+Subscribe.m index 6c65b0751..a8612b958 100644 --- a/PubNub/Core/PubNub+Subscribe.m +++ b/PubNub/Core/PubNub+Subscribe.m @@ -202,9 +202,8 @@ - (void)setFilterExpression:(NSString *)filterExpression { #pragma mark - Subscription - (void)subscribeWithRequest:(PNSubscribeRequest *)request { - [self cancelSubscribeOperationsWithCompletion:^{ - [self.subscriberManager subscribeWithRequest:request]; - }]; + [self cancelSubscribeOperations]; + [self.subscriberManager subscribeWithRequest:request]; } - (void)subscribeWithRequest:(PNSubscribeRequest *)userRequest completion:(PNSubscriberCompletionBlock)handleBlock { @@ -222,14 +221,8 @@ - (void)subscribeWithRequest:(PNSubscribeRequest *)userRequest completion:(PNSub PNStrongify(self); result.status.initialSubscription = [userRequest.timetoken isEqualToNumber:@0]; - [self.lock writeAccessWithBlock:^{ - if (self.subscribeCancellationHandler) { - self.subscribeCancellationHandler(); - self.subscribeCancellationHandler = nil; - } - - [self callBlock:block status:YES withResult:nil andStatus:result.status]; - }]; + + [self callBlock:block status:YES withResult:nil andStatus:result.status]; }; [self performRequest:userRequest withParser:responseParser completion:handler]; @@ -323,9 +316,8 @@ - (void)subscribeToPresenceChannels:(NSArray *)channels withQueryPar - (void)unsubscribeWithRequest:(PNPresenceLeaveRequest *)request { if (request.channels.count == 0 && request.channelGroups.count == 0) return; - [self cancelSubscribeOperationsWithCompletion:^{ - [self.subscriberManager unsubscribeWithRequest:request completion:nil]; - }]; + [self cancelSubscribeOperations]; + [self.subscriberManager unsubscribeWithRequest:request completion:nil]; } - (void)unsubscribeWithRequest:(PNPresenceLeaveRequest *)userRequest @@ -375,9 +367,8 @@ - (void)unsubscribeFromChannels:(NSArray *)channels request.observePresence = shouldObservePresence; if (request.channels.count || request.channelGroups.count) { - [self cancelSubscribeOperationsWithCompletion:^{ - [self.subscriberManager unsubscribeWithRequest:request completion:block]; - }]; + [self cancelSubscribeOperations]; + [self.subscriberManager unsubscribeWithRequest:request completion:block]; } else if (block) { pn_dispatch_async(self.callbackQueue, ^{ block(nil); @@ -405,39 +396,18 @@ - (void)unsubscribeFromAllWithCompletion:(PNStatusBlock)block { } - (void)unsubscribeFromAllWithQueryParameters:(NSDictionary *)queryParameters completion:(PNStatusBlock)block { - [self cancelSubscribeOperationsWithCompletion:^{ - [self.subscriberManager unsubscribeFromAllWithQueryParameters:queryParameters completion:block]; - }]; + [self cancelSubscribeOperations]; + [self.subscriberManager unsubscribeFromAllWithQueryParameters:queryParameters completion:block]; } #pragma mark - Misc -- (void)cancelSubscribeOperationsWithCompletion:(dispatch_block_t)block { +- (void)cancelSubscribeOperations { [self.subscriptionNetwork requestsWithBlock:^(NSArray *requests) { - BOOL hasSubscribeRequests = NO; - for(PNTransportRequest *request in requests) { - if (!hasSubscribeRequests && [request.path hasPrefix:kPNSubscribeAPIPrefix]) { - hasSubscribeRequests = YES; - break; - } + if ([request.path hasPrefix:kPNSubscribeAPIPrefix]) request.cancel(); } - - dispatch_block_t cancelRequests = ^{ - for(PNTransportRequest *request in requests) { - if ([request.path hasPrefix:kPNSubscribeAPIPrefix]) request.cancel(); - } - }; - - if (hasSubscribeRequests) { - if (block) { - [self.lock writeAccessWithBlock:^{ - self.subscribeCancellationHandler = [block copy]; - cancelRequests(); - }]; - } else cancelRequests(); - } else if (block) block(); }]; } diff --git a/PubNub/Core/PubNub+SubscribePrivate.h b/PubNub/Core/PubNub+SubscribePrivate.h index 15a37f739..b1f23ef54 100644 --- a/PubNub/Core/PubNub+SubscribePrivate.h +++ b/PubNub/Core/PubNub+SubscribePrivate.h @@ -90,9 +90,7 @@ NS_ASSUME_NONNULL_BEGIN #pragma mark - Misc /// Cancel any active long-polling subscribe operations scheduled for processing. -/// -/// - Parameter block: Subscribe request cancellatation handler block. -- (void)cancelSubscribeOperationsWithCompletion:(nullable dispatch_block_t)block; +- (void)cancelSubscribeOperations; #pragma mark - diff --git a/PubNub/Data/Managers/PNSubscriber.m b/PubNub/Data/Managers/PNSubscriber.m index 8e90cd3eb..e81a5503c 100644 --- a/PubNub/Data/Managers/PNSubscriber.m +++ b/PubNub/Data/Managers/PNSubscriber.m @@ -839,11 +839,10 @@ - (void)subscribe:(BOOL)initialSubscribe [self updateStateTo:PNDisconnectedSubscriberState withStatus:(PNSubscribeStatus *)status completion:^(PNStatusCategory category) { - [self.client cancelSubscribeOperationsWithCompletion:^{ - [status updateCategory:category]; + [self.client cancelSubscribeOperations]; + [status updateCategory:category]; - [self.client callBlock:nil status:YES withResult:nil andStatus:status]; - }]; + [self.client callBlock:nil status:YES withResult:nil andStatus:status]; }]; } #pragma clang diagnostic pop diff --git a/Tests/Tests/Integration/PNSubscribeIntegrationTest.m b/Tests/Tests/Integration/PNSubscribeIntegrationTest.m index 424a6ecbb..2c0d8f39a 100644 --- a/Tests/Tests/Integration/PNSubscribeIntegrationTest.m +++ b/Tests/Tests/Integration/PNSubscribeIntegrationTest.m @@ -261,8 +261,7 @@ - (void)testItShouldSubscribeToMultipleChannelsAndTriggerOnlineEventWhenSubscrib [client2 subscribeToPresenceChannels:@[channels.lastObject]]; [self waitTask:@"waitForSubscribeOnPresence" completionFor:(YHVVCR.cassette.isNewCassette ? 3.f : 0.f)]; - - + XCTAssertTrue([client2 isSubscribedOn:[channels.lastObject stringByAppendingString:@"-pnpres"]]); [self waitToCompleteIn:self.testCompletionDelay codeBlock:^(dispatch_block_t handler) {