FBLPromise+Retry.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**
  2. Copyright 2018 Google Inc. All rights reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at:
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #import "FBLPromise.h"
  14. NS_ASSUME_NONNULL_BEGIN
  15. /** The default number of retry attempts is 1. */
  16. FOUNDATION_EXTERN NSInteger const FBLPromiseRetryDefaultAttemptsCount NS_REFINED_FOR_SWIFT;
  17. /** The default delay interval before making a retry attempt is 1.0 second. */
  18. FOUNDATION_EXTERN NSTimeInterval const FBLPromiseRetryDefaultDelayInterval NS_REFINED_FOR_SWIFT;
  19. @interface FBLPromise<Value>(RetryAdditions)
  20. typedef id __nullable (^FBLPromiseRetryWorkBlock)(void) NS_SWIFT_UNAVAILABLE("");
  21. typedef BOOL (^FBLPromiseRetryPredicateBlock)(NSInteger, NSError *) NS_SWIFT_UNAVAILABLE("");
  22. /**
  23. Creates a pending promise that fulfills with the same value as the promise returned from `work`
  24. block, which executes asynchronously, or rejects with the same error after all retry attempts have
  25. been exhausted. Defaults to `FBLPromiseRetryDefaultAttemptsCount` attempt(s) on rejection where the
  26. `work` block is retried after a delay of `FBLPromiseRetryDefaultDelayInterval` second(s).
  27. @param work A block that executes asynchronously on the default queue and returns a value or an
  28. error used to resolve the promise.
  29. @return A new pending promise that fulfills with the same value as the promise returned from `work`
  30. block, or rejects with the same error after all retry attempts have been exhausted.
  31. */
  32. + (FBLPromise *)retry:(FBLPromiseRetryWorkBlock)work NS_SWIFT_UNAVAILABLE("");
  33. /**
  34. Creates a pending promise that fulfills with the same value as the promise returned from `work`
  35. block, which executes asynchronously on the given `queue`, or rejects with the same error after all
  36. retry attempts have been exhausted. Defaults to `FBLPromiseRetryDefaultAttemptsCount` attempt(s) on
  37. rejection where the `work` block is retried on the given `queue` after a delay of
  38. `FBLPromiseRetryDefaultDelayInterval` second(s).
  39. @param queue A queue to invoke the `work` block on.
  40. @param work A block that executes asynchronously on the given `queue` and returns a value or an
  41. error used to resolve the promise.
  42. @return A new pending promise that fulfills with the same value as the promise returned from `work`
  43. block, or rejects with the same error after all retry attempts have been exhausted.
  44. */
  45. + (FBLPromise *)onQueue:(dispatch_queue_t)queue
  46. retry:(FBLPromiseRetryWorkBlock)work NS_SWIFT_UNAVAILABLE("");
  47. /**
  48. Creates a pending promise that fulfills with the same value as the promise returned from `work`
  49. block, which executes asynchronously, or rejects with the same error after all retry attempts have
  50. been exhausted.
  51. @param count Max number of retry attempts. The `work` block will be executed once if the specified
  52. count is less than or equal to zero.
  53. @param work A block that executes asynchronously on the default queue and returns a value or an
  54. error used to resolve the promise.
  55. @return A new pending promise that fulfills with the same value as the promise returned from `work`
  56. block, or rejects with the same error after all retry attempts have been exhausted.
  57. */
  58. + (FBLPromise *)attempts:(NSInteger)count
  59. retry:(FBLPromiseRetryWorkBlock)work NS_SWIFT_UNAVAILABLE("");
  60. /**
  61. Creates a pending promise that fulfills with the same value as the promise returned from `work`
  62. block, which executes asynchronously on the given `queue`, or rejects with the same error after all
  63. retry attempts have been exhausted.
  64. @param queue A queue to invoke the `work` block on.
  65. @param count Max number of retry attempts. The `work` block will be executed once if the specified
  66. count is less than or equal to zero.
  67. @param work A block that executes asynchronously on the given `queue` and returns a value or an
  68. error used to resolve the promise.
  69. @return A new pending promise that fulfills with the same value as the promise returned from `work`
  70. block, or rejects with the same error after all retry attempts have been exhausted.
  71. */
  72. + (FBLPromise *)onQueue:(dispatch_queue_t)queue
  73. attempts:(NSInteger)count
  74. retry:(FBLPromiseRetryWorkBlock)work NS_SWIFT_UNAVAILABLE("");
  75. /**
  76. Creates a pending promise that fulfills with the same value as the promise returned from `work`
  77. block, which executes asynchronously, or rejects with the same error after all retry attempts have
  78. been exhausted. On rejection, the `work` block is retried after the given delay `interval` and will
  79. continue to retry until the number of specified attempts have been exhausted or will bail early if
  80. the given condition is not met.
  81. @param count Max number of retry attempts. The `work` block will be executed once if the specified
  82. count is less than or equal to zero.
  83. @param interval Time to wait before the next retry attempt.
  84. @param predicate Condition to check before the next retry attempt. The predicate block provides the
  85. the number of remaining retry attempts and the error that the promise was rejected
  86. with.
  87. @param work A block that executes asynchronously on the default queue and returns a value or an
  88. error used to resolve the promise.
  89. @return A new pending promise that fulfills with the same value as the promise returned from `work`
  90. block, or rejects with the same error after all retry attempts have been exhausted or if
  91. the given condition is not met.
  92. */
  93. + (FBLPromise *)attempts:(NSInteger)count
  94. delay:(NSTimeInterval)interval
  95. condition:(nullable FBLPromiseRetryPredicateBlock)predicate
  96. retry:(FBLPromiseRetryWorkBlock)work NS_SWIFT_UNAVAILABLE("");
  97. /**
  98. Creates a pending promise that fulfills with the same value as the promise returned from `work`
  99. block, which executes asynchronously on the given `queue`, or rejects with the same error after all
  100. retry attempts have been exhausted. On rejection, the `work` block is retried after the given
  101. delay `interval` and will continue to retry until the number of specified attempts have been
  102. exhausted or will bail early if the given condition is not met.
  103. @param queue A queue to invoke the `work` block on.
  104. @param count Max number of retry attempts. The `work` block will be executed once if the specified
  105. count is less than or equal to zero.
  106. @param interval Time to wait before the next retry attempt.
  107. @param predicate Condition to check before the next retry attempt. The predicate block provides the
  108. the number of remaining retry attempts and the error that the promise was rejected
  109. with.
  110. @param work A block that executes asynchronously on the given `queue` and returns a value or an
  111. error used to resolve the promise.
  112. @return A new pending promise that fulfills with the same value as the promise returned from `work`
  113. block, or rejects with the same error after all retry attempts have been exhausted or if
  114. the given condition is not met.
  115. */
  116. + (FBLPromise *)onQueue:(dispatch_queue_t)queue
  117. attempts:(NSInteger)count
  118. delay:(NSTimeInterval)interval
  119. condition:(nullable FBLPromiseRetryPredicateBlock)predicate
  120. retry:(FBLPromiseRetryWorkBlock)work NS_REFINED_FOR_SWIFT;
  121. @end
  122. /**
  123. Convenience dot-syntax wrappers for `FBLPromise+Retry` operators.
  124. Usage: FBLPromise.retry(^id { ... })
  125. */
  126. @interface FBLPromise<Value>(DotSyntax_RetryAdditions)
  127. + (FBLPromise * (^)(FBLPromiseRetryWorkBlock))retry FBL_PROMISES_DOT_SYNTAX
  128. NS_SWIFT_UNAVAILABLE("");
  129. + (FBLPromise * (^)(dispatch_queue_t, FBLPromiseRetryWorkBlock))retryOn FBL_PROMISES_DOT_SYNTAX
  130. NS_SWIFT_UNAVAILABLE("");
  131. + (FBLPromise * (^)(NSInteger, NSTimeInterval, FBLPromiseRetryPredicateBlock __nullable,
  132. FBLPromiseRetryWorkBlock))retryAgain FBL_PROMISES_DOT_SYNTAX
  133. NS_SWIFT_UNAVAILABLE("");
  134. + (FBLPromise * (^)(dispatch_queue_t, NSInteger, NSTimeInterval,
  135. FBLPromiseRetryPredicateBlock __nullable,
  136. FBLPromiseRetryWorkBlock))retryAgainOn FBL_PROMISES_DOT_SYNTAX
  137. NS_SWIFT_UNAVAILABLE("");
  138. @end
  139. NS_ASSUME_NONNULL_END