Skip to content

Commit

Permalink
refactor(subscribe): revert cancellation handler
Browse files Browse the repository at this point in the history
  • Loading branch information
parfeon committed Jun 26, 2024
1 parent 20ec6b6 commit 7b4f960
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 68 deletions.
9 changes: 3 additions & 6 deletions PubNub/Core/PubNub+Core.m
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ @interface PubNub () <PN_CORE_PROTOCOLS>

#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
Expand Down Expand Up @@ -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) ||
Expand Down Expand Up @@ -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
}
}];
Expand Down
3 changes: 0 additions & 3 deletions PubNub/Core/PubNub+CorePrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
15 changes: 7 additions & 8 deletions PubNub/Core/PubNub+Presence.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
54 changes: 12 additions & 42 deletions PubNub/Core/PubNub+Subscribe.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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];
Expand Down Expand Up @@ -323,9 +316,8 @@ - (void)subscribeToPresenceChannels:(NSArray<NSString *> *)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
Expand Down Expand Up @@ -375,9 +367,8 @@ - (void)unsubscribeFromChannels:(NSArray<NSString *> *)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);
Expand Down Expand Up @@ -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<PNTransportRequest *> *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();
}];
}

Expand Down
4 changes: 1 addition & 3 deletions PubNub/Core/PubNub+SubscribePrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 -

Expand Down
7 changes: 3 additions & 4 deletions PubNub/Data/Managers/PNSubscriber.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions Tests/Tests/Integration/PNSubscribeIntegrationTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 7b4f960

Please sign in to comment.