2 * Copyright 2017 Google
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #import "FIRMessagingPubSubRegistrar.h"
19 #import "FIRMessagingCheckinService.h"
20 #import "FIRMessagingDefines.h"
21 #import "FIRMessagingPubSubRegistrar.h"
22 #import "FIRMessagingTopicsCommon.h"
23 #import "NSError+FIRMessaging.h"
25 @interface FIRMessagingPubSubRegistrar ()
27 @property(nonatomic, readwrite, strong) FIRMessagingCheckinService *checkinService;
29 @property(nonatomic, readonly, strong) NSOperationQueue *topicOperations;
30 // Common errors, instantiated, to avoid generating multiple copies
31 @property(nonatomic, readwrite, strong) NSError *operationInProgressError;
35 @implementation FIRMessagingPubSubRegistrar
37 - (instancetype)init {
38 FIRMessagingInvalidateInitializer();
41 - (instancetype)initWithCheckinService:(FIRMessagingCheckinService *)checkinService {
44 _checkinService = checkinService;
45 _topicOperations = [[NSOperationQueue alloc] init];
46 // Do 10 topic operations at a time; it's enough to keep the TCP connection to the host alive,
47 // saving hundreds of milliseconds on each request (compared to a serial queue).
48 _topicOperations.maxConcurrentOperationCount = 10;
53 - (void)stopAllSubscriptionRequests {
54 [self.topicOperations cancelAllOperations];
57 - (void)updateSubscriptionToTopic:(NSString *)topic
58 withToken:(NSString *)token
59 options:(NSDictionary *)options
60 shouldDelete:(BOOL)shouldDelete
61 handler:(FIRMessagingTopicOperationCompletion)handler {
63 FIRMessagingTopicAction action = FIRMessagingTopicActionSubscribe;
65 action = FIRMessagingTopicActionUnsubscribe;
67 FIRMessagingTopicOperation *operation =
68 [[FIRMessagingTopicOperation alloc] initWithTopic:topic
72 checkinService:self.checkinService
74 [self.topicOperations addOperation:operation];