You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.7 KiB

6 years ago
  1. /*
  2. * Copyright 2017 Google
  3. *
  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
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  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.
  15. */
  16. #import "FIRMessagingPubSubRegistrar.h"
  17. #import "FIRMessagingCheckinService.h"
  18. #import "FIRMessagingDefines.h"
  19. #import "FIRMessagingPubSubRegistrar.h"
  20. #import "FIRMessagingTopicsCommon.h"
  21. #import "NSError+FIRMessaging.h"
  22. @interface FIRMessagingPubSubRegistrar ()
  23. @property(nonatomic, readwrite, strong) FIRMessagingCheckinService *checkinService;
  24. @property(nonatomic, readonly, strong) NSOperationQueue *topicOperations;
  25. // Common errors, instantiated, to avoid generating multiple copies
  26. @property(nonatomic, readwrite, strong) NSError *operationInProgressError;
  27. @end
  28. @implementation FIRMessagingPubSubRegistrar
  29. - (instancetype)init {
  30. FIRMessagingInvalidateInitializer();
  31. }
  32. - (instancetype)initWithCheckinService:(FIRMessagingCheckinService *)checkinService {
  33. self = [super init];
  34. if (self) {
  35. _checkinService = checkinService;
  36. _topicOperations = [[NSOperationQueue alloc] init];
  37. // Do 10 topic operations at a time; it's enough to keep the TCP connection to the host alive,
  38. // saving hundreds of milliseconds on each request (compared to a serial queue).
  39. _topicOperations.maxConcurrentOperationCount = 10;
  40. }
  41. return self;
  42. }
  43. - (void)stopAllSubscriptionRequests {
  44. [self.topicOperations cancelAllOperations];
  45. }
  46. - (void)updateSubscriptionToTopic:(NSString *)topic
  47. withToken:(NSString *)token
  48. options:(NSDictionary *)options
  49. shouldDelete:(BOOL)shouldDelete
  50. handler:(FIRMessagingTopicOperationCompletion)handler {
  51. FIRMessagingTopicAction action = FIRMessagingTopicActionSubscribe;
  52. if (shouldDelete) {
  53. action = FIRMessagingTopicActionUnsubscribe;
  54. }
  55. FIRMessagingTopicOperation *operation =
  56. [[FIRMessagingTopicOperation alloc] initWithTopic:topic
  57. action:action
  58. token:token
  59. options:options
  60. checkinService:self.checkinService
  61. completion:handler];
  62. [self.topicOperations addOperation:operation];
  63. }
  64. @end