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.

128 lines
4.5 KiB

5 years ago
  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+Retry.h"
  14. #import "FBLPromisePrivate.h"
  15. NSInteger const FBLPromiseRetryDefaultAttemptsCount = 1;
  16. NSTimeInterval const FBLPromiseRetryDefaultDelayInterval = 1.0;
  17. static void FBLPromiseRetryAttempt(FBLPromise *promise, dispatch_queue_t queue, NSInteger count,
  18. NSTimeInterval interval, FBLPromiseRetryPredicateBlock predicate,
  19. FBLPromiseRetryWorkBlock work) {
  20. __auto_type retrier = ^(id __nullable value) {
  21. if ([value isKindOfClass:[NSError class]]) {
  22. if (count <= 0 || (predicate && !predicate(count, value))) {
  23. [promise reject:value];
  24. } else {
  25. dispatch_after(dispatch_time(0, (int64_t)(interval * NSEC_PER_SEC)), queue, ^{
  26. FBLPromiseRetryAttempt(promise, queue, count - 1, interval, predicate, work);
  27. });
  28. }
  29. } else {
  30. [promise fulfill:value];
  31. }
  32. };
  33. id value = work();
  34. if ([value isKindOfClass:[FBLPromise class]]) {
  35. [(FBLPromise *)value observeOnQueue:queue fulfill:retrier reject:retrier];
  36. } else {
  37. retrier(value);
  38. }
  39. }
  40. @implementation FBLPromise (RetryAdditions)
  41. + (FBLPromise *)retry:(FBLPromiseRetryWorkBlock)work {
  42. return [self onQueue:FBLPromise.defaultDispatchQueue retry:work];
  43. }
  44. + (FBLPromise *)onQueue:(dispatch_queue_t)queue retry:(FBLPromiseRetryWorkBlock)work {
  45. return [self onQueue:queue attempts:FBLPromiseRetryDefaultAttemptsCount retry:work];
  46. }
  47. + (FBLPromise *)attempts:(NSInteger)count retry:(FBLPromiseRetryWorkBlock)work {
  48. return [self onQueue:FBLPromise.defaultDispatchQueue attempts:count retry:work];
  49. }
  50. + (FBLPromise *)onQueue:(dispatch_queue_t)queue
  51. attempts:(NSInteger)count
  52. retry:(FBLPromiseRetryWorkBlock)work {
  53. return [self onQueue:queue
  54. attempts:count
  55. delay:FBLPromiseRetryDefaultDelayInterval
  56. condition:nil
  57. retry:work];
  58. }
  59. + (FBLPromise *)attempts:(NSInteger)count
  60. delay:(NSTimeInterval)interval
  61. condition:(nullable FBLPromiseRetryPredicateBlock)predicate
  62. retry:(FBLPromiseRetryWorkBlock)work {
  63. return [self onQueue:FBLPromise.defaultDispatchQueue
  64. attempts:count
  65. delay:interval
  66. condition:predicate
  67. retry:work];
  68. }
  69. + (FBLPromise *)onQueue:(dispatch_queue_t)queue
  70. attempts:(NSInteger)count
  71. delay:(NSTimeInterval)interval
  72. condition:(nullable FBLPromiseRetryPredicateBlock)predicate
  73. retry:(FBLPromiseRetryWorkBlock)work {
  74. NSParameterAssert(queue);
  75. NSParameterAssert(work);
  76. FBLPromise *promise = [[FBLPromise alloc] initPending];
  77. FBLPromiseRetryAttempt(promise, queue, count, interval, predicate, work);
  78. return promise;
  79. }
  80. @end
  81. @implementation FBLPromise (DotSyntax_RetryAdditions)
  82. + (FBLPromise * (^)(FBLPromiseRetryWorkBlock))retry {
  83. return ^id(FBLPromiseRetryWorkBlock work) {
  84. return [self retry:work];
  85. };
  86. }
  87. + (FBLPromise * (^)(dispatch_queue_t, FBLPromiseRetryWorkBlock))retryOn {
  88. return ^id(dispatch_queue_t queue, FBLPromiseRetryWorkBlock work) {
  89. return [self onQueue:queue retry:work];
  90. };
  91. }
  92. + (FBLPromise * (^)(NSInteger, NSTimeInterval, FBLPromiseRetryPredicateBlock,
  93. FBLPromiseRetryWorkBlock))retryAgain {
  94. return ^id(NSInteger count, NSTimeInterval interval, FBLPromiseRetryPredicateBlock predicate,
  95. FBLPromiseRetryWorkBlock work) {
  96. return [self attempts:count delay:interval condition:predicate retry:work];
  97. };
  98. }
  99. + (FBLPromise * (^)(dispatch_queue_t, NSInteger, NSTimeInterval, FBLPromiseRetryPredicateBlock,
  100. FBLPromiseRetryWorkBlock))retryAgainOn {
  101. return ^id(dispatch_queue_t queue, NSInteger count, NSTimeInterval interval,
  102. FBLPromiseRetryPredicateBlock predicate, FBLPromiseRetryWorkBlock work) {
  103. return [self onQueue:queue attempts:count delay:interval condition:predicate retry:work];
  104. };
  105. }
  106. @end