FBLPromise+Wrap.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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+Wrap.h"
  14. #import "FBLPromise+Async.h"
  15. @implementation FBLPromise (WrapAdditions)
  16. + (instancetype)wrapCompletion:(void (^)(FBLPromiseCompletion))work {
  17. return [self onQueue:self.defaultDispatchQueue wrapCompletion:work];
  18. }
  19. + (instancetype)onQueue:(dispatch_queue_t)queue
  20. wrapCompletion:(void (^)(FBLPromiseCompletion))work {
  21. NSParameterAssert(queue);
  22. NSParameterAssert(work);
  23. return [self onQueue:queue
  24. async:^(FBLPromiseFulfillBlock fulfill, FBLPromiseRejectBlock __unused _) {
  25. work(^{
  26. fulfill(nil);
  27. });
  28. }];
  29. }
  30. + (instancetype)wrapObjectCompletion:(void (^)(FBLPromiseObjectCompletion))work {
  31. return [self onQueue:self.defaultDispatchQueue wrapObjectCompletion:work];
  32. }
  33. + (instancetype)onQueue:(dispatch_queue_t)queue
  34. wrapObjectCompletion:(void (^)(FBLPromiseObjectCompletion))work {
  35. NSParameterAssert(queue);
  36. NSParameterAssert(work);
  37. return [self onQueue:queue
  38. async:^(FBLPromiseFulfillBlock fulfill, FBLPromiseRejectBlock __unused _) {
  39. work(^(id __nullable value) {
  40. fulfill(value);
  41. });
  42. }];
  43. }
  44. + (instancetype)wrapErrorCompletion:(void (^)(FBLPromiseErrorCompletion))work {
  45. return [self onQueue:self.defaultDispatchQueue wrapErrorCompletion:work];
  46. }
  47. + (instancetype)onQueue:(dispatch_queue_t)queue
  48. wrapErrorCompletion:(void (^)(FBLPromiseErrorCompletion))work {
  49. NSParameterAssert(queue);
  50. NSParameterAssert(work);
  51. return [self onQueue:queue
  52. async:^(FBLPromiseFulfillBlock fulfill, FBLPromiseRejectBlock reject) {
  53. work(^(NSError *__nullable error) {
  54. if (error) {
  55. reject(error);
  56. } else {
  57. fulfill(nil);
  58. }
  59. });
  60. }];
  61. }
  62. + (instancetype)wrapObjectOrErrorCompletion:(void (^)(FBLPromiseObjectOrErrorCompletion))work {
  63. return [self onQueue:self.defaultDispatchQueue wrapObjectOrErrorCompletion:work];
  64. }
  65. + (instancetype)onQueue:(dispatch_queue_t)queue
  66. wrapObjectOrErrorCompletion:(void (^)(FBLPromiseObjectOrErrorCompletion))work {
  67. NSParameterAssert(queue);
  68. NSParameterAssert(work);
  69. return [self onQueue:queue
  70. async:^(FBLPromiseFulfillBlock fulfill, FBLPromiseRejectBlock reject) {
  71. work(^(id __nullable value, NSError *__nullable error) {
  72. if (error) {
  73. reject(error);
  74. } else {
  75. fulfill(value);
  76. }
  77. });
  78. }];
  79. }
  80. + (instancetype)wrapErrorOrObjectCompletion:(void (^)(FBLPromiseErrorOrObjectCompletion))work {
  81. return [self onQueue:self.defaultDispatchQueue wrapErrorOrObjectCompletion:work];
  82. }
  83. + (instancetype)onQueue:(dispatch_queue_t)queue
  84. wrapErrorOrObjectCompletion:(void (^)(FBLPromiseErrorOrObjectCompletion))work {
  85. NSParameterAssert(queue);
  86. NSParameterAssert(work);
  87. return [self onQueue:queue
  88. async:^(FBLPromiseFulfillBlock fulfill, FBLPromiseRejectBlock reject) {
  89. work(^(NSError *__nullable error, id __nullable value) {
  90. if (error) {
  91. reject(error);
  92. } else {
  93. fulfill(value);
  94. }
  95. });
  96. }];
  97. }
  98. + (FBLPromise<NSArray *> *)wrap2ObjectsOrErrorCompletion:
  99. (void (^)(FBLPromise2ObjectsOrErrorCompletion))work {
  100. return [self onQueue:self.defaultDispatchQueue wrap2ObjectsOrErrorCompletion:work];
  101. }
  102. + (FBLPromise<NSArray *> *)onQueue:(dispatch_queue_t)queue
  103. wrap2ObjectsOrErrorCompletion:(void (^)(FBLPromise2ObjectsOrErrorCompletion))work {
  104. NSParameterAssert(queue);
  105. NSParameterAssert(work);
  106. return [self onQueue:queue
  107. async:^(FBLPromiseFulfillBlock fulfill, FBLPromiseRejectBlock reject) {
  108. work(^(id __nullable value1, id __nullable value2, NSError *__nullable error) {
  109. if (error) {
  110. reject(error);
  111. } else {
  112. fulfill(@[ value1 ?: [NSNull null], value2 ?: [NSNull null] ]);
  113. }
  114. });
  115. }];
  116. }
  117. + (FBLPromise<NSNumber *> *)wrapBoolCompletion:(void (^)(FBLPromiseBoolCompletion))work {
  118. return [self onQueue:self.defaultDispatchQueue wrapBoolCompletion:work];
  119. }
  120. + (FBLPromise<NSNumber *> *)onQueue:(dispatch_queue_t)queue
  121. wrapBoolCompletion:(void (^)(FBLPromiseBoolCompletion))work {
  122. NSParameterAssert(queue);
  123. NSParameterAssert(work);
  124. return [self onQueue:queue
  125. async:^(FBLPromiseFulfillBlock fulfill, FBLPromiseRejectBlock __unused _) {
  126. work(^(BOOL value) {
  127. fulfill(@(value));
  128. });
  129. }];
  130. }
  131. + (FBLPromise<NSNumber *> *)wrapBoolOrErrorCompletion:
  132. (void (^)(FBLPromiseBoolOrErrorCompletion))work {
  133. return [self onQueue:self.defaultDispatchQueue wrapBoolOrErrorCompletion:work];
  134. }
  135. + (FBLPromise<NSNumber *> *)onQueue:(dispatch_queue_t)queue
  136. wrapBoolOrErrorCompletion:(void (^)(FBLPromiseBoolOrErrorCompletion))work {
  137. NSParameterAssert(queue);
  138. NSParameterAssert(work);
  139. return [self onQueue:queue
  140. async:^(FBLPromiseFulfillBlock fulfill, FBLPromiseRejectBlock reject) {
  141. work(^(BOOL value, NSError *__nullable error) {
  142. if (error) {
  143. reject(error);
  144. } else {
  145. fulfill(@(value));
  146. }
  147. });
  148. }];
  149. }
  150. + (FBLPromise<NSNumber *> *)wrapIntegerCompletion:(void (^)(FBLPromiseIntegerCompletion))work {
  151. return [self onQueue:self.defaultDispatchQueue wrapIntegerCompletion:work];
  152. }
  153. + (FBLPromise<NSNumber *> *)onQueue:(dispatch_queue_t)queue
  154. wrapIntegerCompletion:(void (^)(FBLPromiseIntegerCompletion))work {
  155. NSParameterAssert(queue);
  156. NSParameterAssert(work);
  157. return [self onQueue:queue
  158. async:^(FBLPromiseFulfillBlock fulfill, FBLPromiseRejectBlock __unused _) {
  159. work(^(NSInteger value) {
  160. fulfill(@(value));
  161. });
  162. }];
  163. }
  164. + (FBLPromise<NSNumber *> *)wrapIntegerOrErrorCompletion:
  165. (void (^)(FBLPromiseIntegerOrErrorCompletion))work {
  166. return [self onQueue:self.defaultDispatchQueue wrapIntegerOrErrorCompletion:work];
  167. }
  168. + (FBLPromise<NSNumber *> *)onQueue:(dispatch_queue_t)queue
  169. wrapIntegerOrErrorCompletion:(void (^)(FBLPromiseIntegerOrErrorCompletion))work {
  170. NSParameterAssert(queue);
  171. NSParameterAssert(work);
  172. return [self onQueue:queue
  173. async:^(FBLPromiseFulfillBlock fulfill, FBLPromiseRejectBlock reject) {
  174. work(^(NSInteger value, NSError *__nullable error) {
  175. if (error) {
  176. reject(error);
  177. } else {
  178. fulfill(@(value));
  179. }
  180. });
  181. }];
  182. }
  183. + (FBLPromise<NSNumber *> *)wrapDoubleCompletion:(void (^)(FBLPromiseDoubleCompletion))work {
  184. return [self onQueue:self.defaultDispatchQueue wrapDoubleCompletion:work];
  185. }
  186. + (FBLPromise<NSNumber *> *)onQueue:(dispatch_queue_t)queue
  187. wrapDoubleCompletion:(void (^)(FBLPromiseDoubleCompletion))work {
  188. NSParameterAssert(queue);
  189. NSParameterAssert(work);
  190. return [self onQueue:(dispatch_queue_t)queue
  191. async:^(FBLPromiseFulfillBlock fulfill, FBLPromiseRejectBlock __unused _) {
  192. work(^(double value) {
  193. fulfill(@(value));
  194. });
  195. }];
  196. }
  197. + (FBLPromise<NSNumber *> *)wrapDoubleOrErrorCompletion:
  198. (void (^)(FBLPromiseDoubleOrErrorCompletion))work {
  199. return [self onQueue:self.defaultDispatchQueue wrapDoubleOrErrorCompletion:work];
  200. }
  201. + (FBLPromise<NSNumber *> *)onQueue:(dispatch_queue_t)queue
  202. wrapDoubleOrErrorCompletion:(void (^)(FBLPromiseDoubleOrErrorCompletion))work {
  203. NSParameterAssert(queue);
  204. NSParameterAssert(work);
  205. return [self onQueue:queue
  206. async:^(FBLPromiseFulfillBlock fulfill, FBLPromiseRejectBlock reject) {
  207. work(^(double value, NSError *__nullable error) {
  208. if (error) {
  209. reject(error);
  210. } else {
  211. fulfill(@(value));
  212. }
  213. });
  214. }];
  215. }
  216. @end
  217. @implementation FBLPromise (DotSyntax_WrapAdditions)
  218. + (FBLPromise * (^)(void (^)(FBLPromiseCompletion)))wrapCompletion {
  219. return ^(void (^work)(FBLPromiseCompletion)) {
  220. return [self wrapCompletion:work];
  221. };
  222. }
  223. + (FBLPromise * (^)(dispatch_queue_t, void (^)(FBLPromiseCompletion)))wrapCompletionOn {
  224. return ^(dispatch_queue_t queue, void (^work)(FBLPromiseCompletion)) {
  225. return [self onQueue:queue wrapCompletion:work];
  226. };
  227. }
  228. + (FBLPromise * (^)(void (^)(FBLPromiseObjectCompletion)))wrapObjectCompletion {
  229. return ^(void (^work)(FBLPromiseObjectCompletion)) {
  230. return [self wrapObjectCompletion:work];
  231. };
  232. }
  233. + (FBLPromise * (^)(dispatch_queue_t, void (^)(FBLPromiseObjectCompletion)))wrapObjectCompletionOn {
  234. return ^(dispatch_queue_t queue, void (^work)(FBLPromiseObjectCompletion)) {
  235. return [self onQueue:queue wrapObjectCompletion:work];
  236. };
  237. }
  238. + (FBLPromise * (^)(void (^)(FBLPromiseErrorCompletion)))wrapErrorCompletion {
  239. return ^(void (^work)(FBLPromiseErrorCompletion)) {
  240. return [self wrapErrorCompletion:work];
  241. };
  242. }
  243. + (FBLPromise * (^)(dispatch_queue_t, void (^)(FBLPromiseErrorCompletion)))wrapErrorCompletionOn {
  244. return ^(dispatch_queue_t queue, void (^work)(FBLPromiseErrorCompletion)) {
  245. return [self onQueue:queue wrapErrorCompletion:work];
  246. };
  247. }
  248. + (FBLPromise * (^)(void (^)(FBLPromiseObjectOrErrorCompletion)))wrapObjectOrErrorCompletion {
  249. return ^(void (^work)(FBLPromiseObjectOrErrorCompletion)) {
  250. return [self wrapObjectOrErrorCompletion:work];
  251. };
  252. }
  253. + (FBLPromise * (^)(dispatch_queue_t,
  254. void (^)(FBLPromiseObjectOrErrorCompletion)))wrapObjectOrErrorCompletionOn {
  255. return ^(dispatch_queue_t queue, void (^work)(FBLPromiseObjectOrErrorCompletion)) {
  256. return [self onQueue:queue wrapObjectOrErrorCompletion:work];
  257. };
  258. }
  259. + (FBLPromise * (^)(void (^)(FBLPromiseErrorOrObjectCompletion)))wrapErrorOrObjectCompletion {
  260. return ^(void (^work)(FBLPromiseErrorOrObjectCompletion)) {
  261. return [self wrapErrorOrObjectCompletion:work];
  262. };
  263. }
  264. + (FBLPromise * (^)(dispatch_queue_t,
  265. void (^)(FBLPromiseErrorOrObjectCompletion)))wrapErrorOrObjectCompletionOn {
  266. return ^(dispatch_queue_t queue, void (^work)(FBLPromiseErrorOrObjectCompletion)) {
  267. return [self onQueue:queue wrapErrorOrObjectCompletion:work];
  268. };
  269. }
  270. + (FBLPromise<NSArray *> * (^)(void (^)(FBLPromise2ObjectsOrErrorCompletion)))
  271. wrap2ObjectsOrErrorCompletion {
  272. return ^(void (^work)(FBLPromise2ObjectsOrErrorCompletion)) {
  273. return [self wrap2ObjectsOrErrorCompletion:work];
  274. };
  275. }
  276. + (FBLPromise<NSArray *> * (^)(dispatch_queue_t, void (^)(FBLPromise2ObjectsOrErrorCompletion)))
  277. wrap2ObjectsOrErrorCompletionOn {
  278. return ^(dispatch_queue_t queue, void (^work)(FBLPromise2ObjectsOrErrorCompletion)) {
  279. return [self onQueue:queue wrap2ObjectsOrErrorCompletion:work];
  280. };
  281. }
  282. + (FBLPromise<NSNumber *> * (^)(void (^)(FBLPromiseBoolCompletion)))wrapBoolCompletion {
  283. return ^(void (^work)(FBLPromiseBoolCompletion)) {
  284. return [self wrapBoolCompletion:work];
  285. };
  286. }
  287. + (FBLPromise<NSNumber *> * (^)(dispatch_queue_t,
  288. void (^)(FBLPromiseBoolCompletion)))wrapBoolCompletionOn {
  289. return ^(dispatch_queue_t queue, void (^work)(FBLPromiseBoolCompletion)) {
  290. return [self onQueue:queue wrapBoolCompletion:work];
  291. };
  292. }
  293. + (FBLPromise<NSNumber *> * (^)(void (^)(FBLPromiseBoolOrErrorCompletion)))
  294. wrapBoolOrErrorCompletion {
  295. return ^(void (^work)(FBLPromiseBoolOrErrorCompletion)) {
  296. return [self wrapBoolOrErrorCompletion:work];
  297. };
  298. }
  299. + (FBLPromise<NSNumber *> * (^)(dispatch_queue_t, void (^)(FBLPromiseBoolOrErrorCompletion)))
  300. wrapBoolOrErrorCompletionOn {
  301. return ^(dispatch_queue_t queue, void (^work)(FBLPromiseBoolOrErrorCompletion)) {
  302. return [self onQueue:queue wrapBoolOrErrorCompletion:work];
  303. };
  304. }
  305. + (FBLPromise<NSNumber *> * (^)(void (^)(FBLPromiseIntegerCompletion)))wrapIntegerCompletion {
  306. return ^(void (^work)(FBLPromiseIntegerCompletion)) {
  307. return [self wrapIntegerCompletion:work];
  308. };
  309. }
  310. + (FBLPromise<NSNumber *> * (^)(dispatch_queue_t,
  311. void (^)(FBLPromiseIntegerCompletion)))wrapIntegerCompletionOn {
  312. return ^(dispatch_queue_t queue, void (^work)(FBLPromiseIntegerCompletion)) {
  313. return [self onQueue:queue wrapIntegerCompletion:work];
  314. };
  315. }
  316. + (FBLPromise<NSNumber *> * (^)(void (^)(FBLPromiseIntegerOrErrorCompletion)))
  317. wrapIntegerOrErrorCompletion {
  318. return ^(void (^work)(FBLPromiseIntegerOrErrorCompletion)) {
  319. return [self wrapIntegerOrErrorCompletion:work];
  320. };
  321. }
  322. + (FBLPromise<NSNumber *> * (^)(dispatch_queue_t, void (^)(FBLPromiseIntegerOrErrorCompletion)))
  323. wrapIntegerOrErrorCompletionOn {
  324. return ^(dispatch_queue_t queue, void (^work)(FBLPromiseIntegerOrErrorCompletion)) {
  325. return [self onQueue:queue wrapIntegerOrErrorCompletion:work];
  326. };
  327. }
  328. + (FBLPromise<NSNumber *> * (^)(void (^)(FBLPromiseDoubleCompletion)))wrapDoubleCompletion {
  329. return ^(void (^work)(FBLPromiseDoubleCompletion)) {
  330. return [self wrapDoubleCompletion:work];
  331. };
  332. }
  333. + (FBLPromise<NSNumber *> * (^)(dispatch_queue_t,
  334. void (^)(FBLPromiseDoubleCompletion)))wrapDoubleCompletionOn {
  335. return ^(dispatch_queue_t queue, void (^work)(FBLPromiseDoubleCompletion)) {
  336. return [self onQueue:queue wrapDoubleCompletion:work];
  337. };
  338. }
  339. + (FBLPromise<NSNumber *> * (^)(void (^)(FBLPromiseDoubleOrErrorCompletion)))
  340. wrapDoubleOrErrorCompletion {
  341. return ^(void (^work)(FBLPromiseDoubleOrErrorCompletion)) {
  342. return [self wrapDoubleOrErrorCompletion:work];
  343. };
  344. }
  345. + (FBLPromise<NSNumber *> * (^)(dispatch_queue_t, void (^)(FBLPromiseDoubleOrErrorCompletion)))
  346. wrapDoubleOrErrorCompletionOn {
  347. return ^(dispatch_queue_t queue, void (^work)(FBLPromiseDoubleOrErrorCompletion)) {
  348. return [self onQueue:queue wrapDoubleOrErrorCompletion:work];
  349. };
  350. }
  351. @end