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