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.

119 lines
5.3 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 <Foundation/Foundation.h>
  17. #import "FIRMessaging.h"
  18. #import "FIRMessagingTopicsCommon.h"
  19. NS_ASSUME_NONNULL_BEGIN
  20. /**
  21. * Represents a single batch of topics, with the same action.
  22. *
  23. * Topic operations which have the same action (subscribe or unsubscribe) can be executed
  24. * simultaneously, as the order of operations do not matter with the same action. The set of
  25. * topics is unique, as it doesn't make sense to apply the same action to the same topic
  26. * repeatedly; the result would be the same as the first time.
  27. */
  28. @interface FIRMessagingTopicBatch : NSObject <NSCoding>
  29. @property(nonatomic, readonly, assign) FIRMessagingTopicAction action;
  30. @property(nonatomic, readonly, copy) NSMutableSet <NSString *> *topics;
  31. - (instancetype)init NS_UNAVAILABLE;
  32. - (instancetype)initWithAction:(FIRMessagingTopicAction)action NS_DESIGNATED_INITIALIZER;
  33. @end
  34. @class FIRMessagingPendingTopicsList;
  35. /**
  36. * This delegate must be supplied to the instance of FIRMessagingPendingTopicsList, via the
  37. * @cdelegate property. It lets the
  38. * pending topics list know whether or not it can begin making requests via
  39. * @c-pendingTopicsListCanRequestTopicUpdates:, and handles the request to actually
  40. * perform the topic operation. The delegate also handles when the pending topics list is updated,
  41. * so that it can be archived or persisted.
  42. *
  43. * @see FIRMessagingPendingTopicsList
  44. */
  45. @protocol FIRMessagingPendingTopicsListDelegate <NSObject>
  46. - (void)pendingTopicsList:(FIRMessagingPendingTopicsList *)list
  47. requestedUpdateForTopic:(NSString *)topic
  48. action:(FIRMessagingTopicAction)action
  49. completion:(FIRMessagingTopicOperationCompletion)completion;
  50. - (void)pendingTopicsListDidUpdate:(FIRMessagingPendingTopicsList *)list;
  51. - (BOOL)pendingTopicsListCanRequestTopicUpdates:(FIRMessagingPendingTopicsList *)list;
  52. @end
  53. /**
  54. * FIRMessagingPendingTopicsList manages a list of topic subscription updates, batched by the same
  55. * action (subscribe or unsubscribe). The list roughly maintains the order of the topic operations,
  56. * batched together whenever the topic action (subscribe or unsubscribe) changes.
  57. *
  58. * Topics operations are batched by action because it is safe to perform the same topic action
  59. * (subscribe or unsubscribe) on many topics simultaneously. After each batch is successfully
  60. * completed, the next batch operations can begin.
  61. *
  62. * When asked to resume its operations, FIRMessagingPendingTopicsList will begin performing updates
  63. * of its current batch of topics. For example, it may begin subscription operations for topics
  64. * [A, B, C] simultaneously.
  65. *
  66. * When the current batch is completed, the next batch of operations will be started. For example
  67. * the list may begin unsubscribe operations for [D, A, E]. Note that because A is in both batches,
  68. * A will be correctly subscribed in the first batch, then unsubscribed as part of the second batch
  69. * of operations. Without batching, it would be ambiguous whether A's subscription operation or the
  70. * unsubscription operation would be completed first.
  71. *
  72. * An app can subscribe and unsubscribe from many topics, and this class helps persist the pending
  73. * topics and perform the operation safely and correctly.
  74. *
  75. * When a topic fails to subscribe or unsubscribe due to a network error, it is considered a
  76. * recoverable error, and so it remains in the current batch until it is succesfully completed.
  77. * Topic updates are completed when they either (a) succeed, (b) are cancelled, or (c) result in an
  78. * unrecoverable error. Any error outside of `NSURLErrorDomain` is considered an unrecoverable
  79. * error.
  80. *
  81. * In addition to maintaining the list of pending topic updates, FIRMessagingPendingTopicsList also
  82. * can track completion handlers for topic operations.
  83. *
  84. * @discussion Completion handlers for topic updates are not maintained if it was restored from a
  85. * keyed archive. They are only called if the topic operation finished within the same app session.
  86. *
  87. * You must supply an object conforming to FIRMessagingPendingTopicsListDelegate in order for the
  88. * topic operations to execute.
  89. *
  90. * @see FIRMessagingPendingTopicsListDelegate
  91. */
  92. @interface FIRMessagingPendingTopicsList : NSObject <NSCoding>
  93. @property(nonatomic, weak) NSObject <FIRMessagingPendingTopicsListDelegate> *delegate;
  94. @property(nonatomic, readonly, strong, nullable) NSDate *archiveDate;
  95. @property(nonatomic, readonly) NSUInteger numberOfBatches;
  96. - (instancetype)init NS_DESIGNATED_INITIALIZER;
  97. - (void)addOperationForTopic:(NSString *)topic
  98. withAction:(FIRMessagingTopicAction)action
  99. completion:(nullable FIRMessagingTopicOperationCompletion)completion;
  100. - (void)resumeOperationsIfNeeded;
  101. @end
  102. NS_ASSUME_NONNULL_END