FBLPromise+Wrap.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. /**
  16. Different types of completion handlers available to be wrapped with promise.
  17. */
  18. typedef void (^FBLPromiseCompletion)(void) NS_SWIFT_UNAVAILABLE("");
  19. typedef void (^FBLPromiseObjectCompletion)(id __nullable) NS_SWIFT_UNAVAILABLE("");
  20. typedef void (^FBLPromiseErrorCompletion)(NSError* __nullable) NS_SWIFT_UNAVAILABLE("");
  21. typedef void (^FBLPromiseObjectOrErrorCompletion)(id __nullable, NSError* __nullable)
  22. NS_SWIFT_UNAVAILABLE("");
  23. typedef void (^FBLPromiseErrorOrObjectCompletion)(NSError* __nullable, id __nullable)
  24. NS_SWIFT_UNAVAILABLE("");
  25. typedef void (^FBLPromise2ObjectsOrErrorCompletion)(id __nullable, id __nullable,
  26. NSError* __nullable) NS_SWIFT_UNAVAILABLE("");
  27. typedef void (^FBLPromiseBoolCompletion)(BOOL) NS_SWIFT_UNAVAILABLE("");
  28. typedef void (^FBLPromiseBoolOrErrorCompletion)(BOOL, NSError* __nullable) NS_SWIFT_UNAVAILABLE("");
  29. typedef void (^FBLPromiseIntegerCompletion)(NSInteger) NS_SWIFT_UNAVAILABLE("");
  30. typedef void (^FBLPromiseIntegerOrErrorCompletion)(NSInteger, NSError* __nullable)
  31. NS_SWIFT_UNAVAILABLE("");
  32. typedef void (^FBLPromiseDoubleCompletion)(double) NS_SWIFT_UNAVAILABLE("");
  33. typedef void (^FBLPromiseDoubleOrErrorCompletion)(double, NSError* __nullable)
  34. NS_SWIFT_UNAVAILABLE("");
  35. /**
  36. Provides an easy way to convert methods that use common callback patterns into promises.
  37. */
  38. @interface FBLPromise<Value>(WrapAdditions)
  39. /**
  40. @param work A block to perform any operations needed to resolve the promise.
  41. @returns A promise that resolves with `nil` when completion handler is invoked.
  42. */
  43. + (instancetype)wrapCompletion:(void (^)(FBLPromiseCompletion handler))work
  44. NS_SWIFT_UNAVAILABLE("");
  45. /**
  46. @param queue A queue to invoke the `work` block on.
  47. @param work A block to perform any operations needed to resolve the promise.
  48. @returns A promise that resolves with `nil` when completion handler is invoked.
  49. */
  50. + (instancetype)onQueue:(dispatch_queue_t)queue
  51. wrapCompletion:(void (^)(FBLPromiseCompletion handler))work NS_SWIFT_UNAVAILABLE("");
  52. /**
  53. @param work A block to perform any operations needed to resolve the promise.
  54. @returns A promise that resolves with an object provided by completion handler.
  55. */
  56. + (instancetype)wrapObjectCompletion:(void (^)(FBLPromiseObjectCompletion handler))work
  57. NS_SWIFT_UNAVAILABLE("");
  58. /**
  59. @param queue A queue to invoke the `work` block on.
  60. @param work A block to perform any operations needed to resolve the promise.
  61. @returns A promise that resolves with an object provided by completion handler.
  62. */
  63. + (instancetype)onQueue:(dispatch_queue_t)queue
  64. wrapObjectCompletion:(void (^)(FBLPromiseObjectCompletion handler))work
  65. NS_SWIFT_UNAVAILABLE("");
  66. /**
  67. @param work A block to perform any operations needed to resolve the promise.
  68. @returns A promise that resolves with an error provided by completion handler.
  69. If error is `nil`, fulfills with `nil`, otherwise rejects with the error.
  70. */
  71. + (instancetype)wrapErrorCompletion:(void (^)(FBLPromiseErrorCompletion handler))work
  72. NS_SWIFT_UNAVAILABLE("");
  73. /**
  74. @param queue A queue to invoke the `work` block on.
  75. @param work A block to perform any operations needed to resolve the promise.
  76. @returns A promise that resolves with an error provided by completion handler.
  77. If error is `nil`, fulfills with `nil`, otherwise rejects with the error.
  78. */
  79. + (instancetype)onQueue:(dispatch_queue_t)queue
  80. wrapErrorCompletion:(void (^)(FBLPromiseErrorCompletion handler))work NS_SWIFT_UNAVAILABLE("");
  81. /**
  82. @param work A block to perform any operations needed to resolve the promise.
  83. @returns A promise that resolves with an object provided by completion handler if error is `nil`.
  84. Otherwise, rejects with the error.
  85. */
  86. + (instancetype)wrapObjectOrErrorCompletion:
  87. (void (^)(FBLPromiseObjectOrErrorCompletion handler))work NS_SWIFT_UNAVAILABLE("");
  88. /**
  89. @param queue A queue to invoke the `work` block on.
  90. @param work A block to perform any operations needed to resolve the promise.
  91. @returns A promise that resolves with an object provided by completion handler if error is `nil`.
  92. Otherwise, rejects with the error.
  93. */
  94. + (instancetype)onQueue:(dispatch_queue_t)queue
  95. wrapObjectOrErrorCompletion:(void (^)(FBLPromiseObjectOrErrorCompletion handler))work
  96. NS_SWIFT_UNAVAILABLE("");
  97. /**
  98. @param work A block to perform any operations needed to resolve the promise.
  99. @returns A promise that resolves with an error or object provided by completion handler. If error
  100. is not `nil`, rejects with the error.
  101. */
  102. + (instancetype)wrapErrorOrObjectCompletion:
  103. (void (^)(FBLPromiseErrorOrObjectCompletion handler))work NS_SWIFT_UNAVAILABLE("");
  104. /**
  105. @param queue A queue to invoke the `work` block on.
  106. @param work A block to perform any operations needed to resolve the promise.
  107. @returns A promise that resolves with an error or object provided by completion handler. If error
  108. is not `nil`, rejects with the error.
  109. */
  110. + (instancetype)onQueue:(dispatch_queue_t)queue
  111. wrapErrorOrObjectCompletion:(void (^)(FBLPromiseErrorOrObjectCompletion handler))work
  112. NS_SWIFT_UNAVAILABLE("");
  113. /**
  114. @param work A block to perform any operations needed to resolve the promise.
  115. @returns A promise that resolves with an array of objects provided by completion handler in order
  116. if error is `nil`. Otherwise, rejects with the error.
  117. */
  118. + (FBLPromise<NSArray*>*)wrap2ObjectsOrErrorCompletion:
  119. (void (^)(FBLPromise2ObjectsOrErrorCompletion handler))work NS_SWIFT_UNAVAILABLE("");
  120. /**
  121. @param queue A queue to invoke the `work` block on.
  122. @param work A block to perform any operations needed to resolve the promise.
  123. @returns A promise that resolves with an array of objects provided by completion handler in order
  124. if error is `nil`. Otherwise, rejects with the error.
  125. */
  126. + (FBLPromise<NSArray*>*)onQueue:(dispatch_queue_t)queue
  127. wrap2ObjectsOrErrorCompletion:(void (^)(FBLPromise2ObjectsOrErrorCompletion handler))work
  128. NS_SWIFT_UNAVAILABLE("");
  129. /**
  130. @param work A block to perform any operations needed to resolve the promise.
  131. @returns A promise that resolves with an `NSNumber` wrapping YES/NO.
  132. */
  133. + (FBLPromise<NSNumber*>*)wrapBoolCompletion:(void (^)(FBLPromiseBoolCompletion handler))work
  134. NS_SWIFT_UNAVAILABLE("");
  135. /**
  136. @param queue A queue to invoke the `work` block on.
  137. @param work A block to perform any operations needed to resolve the promise.
  138. @returns A promise that resolves with an `NSNumber` wrapping YES/NO.
  139. */
  140. + (FBLPromise<NSNumber*>*)onQueue:(dispatch_queue_t)queue
  141. wrapBoolCompletion:(void (^)(FBLPromiseBoolCompletion handler))work
  142. NS_SWIFT_UNAVAILABLE("");
  143. /**
  144. @param work A block to perform any operations needed to resolve the promise.
  145. @returns A promise that resolves with an `NSNumber` wrapping YES/NO when error is `nil`.
  146. Otherwise rejects with the error.
  147. */
  148. + (FBLPromise<NSNumber*>*)wrapBoolOrErrorCompletion:
  149. (void (^)(FBLPromiseBoolOrErrorCompletion handler))work NS_SWIFT_UNAVAILABLE("");
  150. /**
  151. @param queue A queue to invoke the `work` block on.
  152. @param work A block to perform any operations needed to resolve the promise.
  153. @returns A promise that resolves with an `NSNumber` wrapping YES/NO when error is `nil`.
  154. Otherwise rejects with the error.
  155. */
  156. + (FBLPromise<NSNumber*>*)onQueue:(dispatch_queue_t)queue
  157. wrapBoolOrErrorCompletion:(void (^)(FBLPromiseBoolOrErrorCompletion handler))work
  158. NS_SWIFT_UNAVAILABLE("");
  159. /**
  160. @param work A block to perform any operations needed to resolve the promise.
  161. @returns A promise that resolves with an `NSNumber` wrapping an integer.
  162. */
  163. + (FBLPromise<NSNumber*>*)wrapIntegerCompletion:(void (^)(FBLPromiseIntegerCompletion handler))work
  164. NS_SWIFT_UNAVAILABLE("");
  165. /**
  166. @param queue A queue to invoke the `work` block on.
  167. @param work A block to perform any operations needed to resolve the promise.
  168. @returns A promise that resolves with an `NSNumber` wrapping an integer.
  169. */
  170. + (FBLPromise<NSNumber*>*)onQueue:(dispatch_queue_t)queue
  171. wrapIntegerCompletion:(void (^)(FBLPromiseIntegerCompletion handler))work
  172. NS_SWIFT_UNAVAILABLE("");
  173. /**
  174. @param work A block to perform any operations needed to resolve the promise.
  175. @returns A promise that resolves with an `NSNumber` wrapping an integer when error is `nil`.
  176. Otherwise rejects with the error.
  177. */
  178. + (FBLPromise<NSNumber*>*)wrapIntegerOrErrorCompletion:
  179. (void (^)(FBLPromiseIntegerOrErrorCompletion handler))work NS_SWIFT_UNAVAILABLE("");
  180. /**
  181. @param queue A queue to invoke the `work` block on.
  182. @param work A block to perform any operations needed to resolve the promise.
  183. @returns A promise that resolves with an `NSNumber` wrapping an integer when error is `nil`.
  184. Otherwise rejects with the error.
  185. */
  186. + (FBLPromise<NSNumber*>*)onQueue:(dispatch_queue_t)queue
  187. wrapIntegerOrErrorCompletion:(void (^)(FBLPromiseIntegerOrErrorCompletion handler))work
  188. NS_SWIFT_UNAVAILABLE("");
  189. /**
  190. @param work A block to perform any operations needed to resolve the promise.
  191. @returns A promise that resolves with an `NSNumber` wrapping a double.
  192. */
  193. + (FBLPromise<NSNumber*>*)wrapDoubleCompletion:(void (^)(FBLPromiseDoubleCompletion handler))work
  194. NS_SWIFT_UNAVAILABLE("");
  195. /**
  196. @param queue A queue to invoke the `work` block on.
  197. @param work A block to perform any operations needed to resolve the promise.
  198. @returns A promise that resolves with an `NSNumber` wrapping a double.
  199. */
  200. + (FBLPromise<NSNumber*>*)onQueue:(dispatch_queue_t)queue
  201. wrapDoubleCompletion:(void (^)(FBLPromiseDoubleCompletion handler))work
  202. NS_SWIFT_UNAVAILABLE("");
  203. /**
  204. @param work A block to perform any operations needed to resolve the promise.
  205. @returns A promise that resolves with an `NSNumber` wrapping a double when error is `nil`.
  206. Otherwise rejects with the error.
  207. */
  208. + (FBLPromise<NSNumber*>*)wrapDoubleOrErrorCompletion:
  209. (void (^)(FBLPromiseDoubleOrErrorCompletion handler))work NS_SWIFT_UNAVAILABLE("");
  210. /**
  211. @param queue A queue to invoke the `work` block on.
  212. @param work A block to perform any operations needed to resolve the promise.
  213. @returns A promise that resolves with an `NSNumber` wrapping a double when error is `nil`.
  214. Otherwise rejects with the error.
  215. */
  216. + (FBLPromise<NSNumber*>*)onQueue:(dispatch_queue_t)queue
  217. wrapDoubleOrErrorCompletion:(void (^)(FBLPromiseDoubleOrErrorCompletion handler))work
  218. NS_SWIFT_UNAVAILABLE("");
  219. @end
  220. /**
  221. Convenience dot-syntax wrappers for `FBLPromise` `wrap` operators.
  222. Usage: FBLPromise.wrapCompletion(^(FBLPromiseCompletion handler) {...})
  223. */
  224. @interface FBLPromise<Value>(DotSyntax_WrapAdditions)
  225. + (FBLPromise* (^)(void (^)(FBLPromiseCompletion)))wrapCompletion FBL_PROMISES_DOT_SYNTAX
  226. NS_SWIFT_UNAVAILABLE("");
  227. + (FBLPromise* (^)(dispatch_queue_t, void (^)(FBLPromiseCompletion)))wrapCompletionOn
  228. FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
  229. + (FBLPromise* (^)(void (^)(FBLPromiseObjectCompletion)))wrapObjectCompletion
  230. FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
  231. + (FBLPromise* (^)(dispatch_queue_t, void (^)(FBLPromiseObjectCompletion)))wrapObjectCompletionOn
  232. FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
  233. + (FBLPromise* (^)(void (^)(FBLPromiseErrorCompletion)))wrapErrorCompletion FBL_PROMISES_DOT_SYNTAX
  234. NS_SWIFT_UNAVAILABLE("");
  235. + (FBLPromise* (^)(dispatch_queue_t, void (^)(FBLPromiseErrorCompletion)))wrapErrorCompletionOn
  236. FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
  237. + (FBLPromise* (^)(void (^)(FBLPromiseObjectOrErrorCompletion)))wrapObjectOrErrorCompletion
  238. FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
  239. + (FBLPromise* (^)(dispatch_queue_t,
  240. void (^)(FBLPromiseObjectOrErrorCompletion)))wrapObjectOrErrorCompletionOn
  241. FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
  242. + (FBLPromise* (^)(void (^)(FBLPromiseErrorOrObjectCompletion)))wrapErrorOrObjectCompletion
  243. FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
  244. + (FBLPromise* (^)(dispatch_queue_t,
  245. void (^)(FBLPromiseErrorOrObjectCompletion)))wrapErrorOrObjectCompletionOn
  246. FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
  247. + (FBLPromise<NSArray*>* (^)(void (^)(FBLPromise2ObjectsOrErrorCompletion)))
  248. wrap2ObjectsOrErrorCompletion FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
  249. + (FBLPromise<NSArray*>* (^)(dispatch_queue_t, void (^)(FBLPromise2ObjectsOrErrorCompletion)))
  250. wrap2ObjectsOrErrorCompletionOn FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
  251. + (FBLPromise<NSNumber*>* (^)(void (^)(FBLPromiseBoolCompletion)))wrapBoolCompletion
  252. FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
  253. + (FBLPromise<NSNumber*>* (^)(dispatch_queue_t,
  254. void (^)(FBLPromiseBoolCompletion)))wrapBoolCompletionOn
  255. FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
  256. + (FBLPromise<NSNumber*>* (^)(void (^)(FBLPromiseBoolOrErrorCompletion)))wrapBoolOrErrorCompletion
  257. FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
  258. + (FBLPromise<NSNumber*>* (^)(dispatch_queue_t,
  259. void (^)(FBLPromiseBoolOrErrorCompletion)))wrapBoolOrErrorCompletionOn
  260. FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
  261. + (FBLPromise<NSNumber*>* (^)(void (^)(FBLPromiseIntegerCompletion)))wrapIntegerCompletion
  262. FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
  263. + (FBLPromise<NSNumber*>* (^)(dispatch_queue_t,
  264. void (^)(FBLPromiseIntegerCompletion)))wrapIntegerCompletionOn
  265. FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
  266. + (FBLPromise<NSNumber*>* (^)(void (^)(FBLPromiseIntegerOrErrorCompletion)))
  267. wrapIntegerOrErrorCompletion FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
  268. + (FBLPromise<NSNumber*>* (^)(dispatch_queue_t, void (^)(FBLPromiseIntegerOrErrorCompletion)))
  269. wrapIntegerOrErrorCompletionOn FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
  270. + (FBLPromise<NSNumber*>* (^)(void (^)(FBLPromiseDoubleCompletion)))wrapDoubleCompletion
  271. FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
  272. + (FBLPromise<NSNumber*>* (^)(dispatch_queue_t,
  273. void (^)(FBLPromiseDoubleCompletion)))wrapDoubleCompletionOn
  274. FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
  275. + (FBLPromise<NSNumber*>* (^)(void (^)(FBLPromiseDoubleOrErrorCompletion)))
  276. wrapDoubleOrErrorCompletion FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
  277. + (FBLPromise<NSNumber*>* (^)(dispatch_queue_t, void (^)(FBLPromiseDoubleOrErrorCompletion)))
  278. wrapDoubleOrErrorCompletionOn FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
  279. @end
  280. NS_ASSUME_NONNULL_END