commit | author | age
|
bad748
|
1 |
/* |
W |
2 |
* Copyright (c) 2014, Facebook, Inc. |
|
3 |
* All rights reserved. |
|
4 |
* |
|
5 |
* This source code is licensed under the BSD-style license found in the |
|
6 |
* LICENSE file in the root directory of this source tree. An additional grant |
|
7 |
* of patent rights can be found in the PATENTS file in the same directory. |
|
8 |
* |
|
9 |
*/ |
|
10 |
|
|
11 |
#import <Foundation/Foundation.h> |
|
12 |
|
|
13 |
#import <Bolts/BFCancellationToken.h> |
|
14 |
|
|
15 |
NS_ASSUME_NONNULL_BEGIN |
|
16 |
|
|
17 |
/*! |
|
18 |
Error domain used if there was multiple errors on <BFTask taskForCompletionOfAllTasks:>. |
|
19 |
*/ |
|
20 |
extern NSString *const BFTaskErrorDomain; |
|
21 |
|
|
22 |
/*! |
|
23 |
An error code used for <BFTask taskForCompletionOfAllTasks:>, if there were multiple errors. |
|
24 |
*/ |
|
25 |
extern NSInteger const kBFMultipleErrorsError; |
|
26 |
|
|
27 |
/*! |
|
28 |
An exception that is thrown if there was multiple exceptions on <BFTask taskForCompletionOfAllTasks:>. |
|
29 |
*/ |
|
30 |
extern NSString *const BFTaskMultipleExceptionsException; |
|
31 |
|
|
32 |
/*! |
|
33 |
An error userInfo key used if there were multiple errors on <BFTask taskForCompletionOfAllTasks:>. |
|
34 |
Value type is `NSArray<NSError *> *`. |
|
35 |
*/ |
|
36 |
extern NSString *const BFTaskMultipleErrorsUserInfoKey; |
|
37 |
|
|
38 |
/*! |
|
39 |
An error userInfo key used if there were multiple exceptions on <BFTask taskForCompletionOfAllTasks:>. |
|
40 |
Value type is `NSArray<NSException *> *`. |
|
41 |
*/ |
|
42 |
extern NSString *const BFTaskMultipleExceptionsUserInfoKey; |
|
43 |
|
|
44 |
@class BFExecutor; |
|
45 |
@class BFTask; |
|
46 |
|
|
47 |
/*! |
|
48 |
The consumer view of a Task. A BFTask has methods to |
|
49 |
inspect the state of the task, and to add continuations to |
|
50 |
be run once the task is complete. |
|
51 |
*/ |
|
52 |
@interface BFTask<__covariant ResultType> : NSObject |
|
53 |
|
|
54 |
/*! |
|
55 |
A block that can act as a continuation for a task. |
|
56 |
*/ |
|
57 |
typedef __nullable id(^BFContinuationBlock)(BFTask<ResultType> *task); |
|
58 |
|
|
59 |
/*! |
|
60 |
Creates a task that is already completed with the given result. |
|
61 |
@param result The result for the task. |
|
62 |
*/ |
|
63 |
+ (instancetype)taskWithResult:(nullable ResultType)result; |
|
64 |
|
|
65 |
/*! |
|
66 |
Creates a task that is already completed with the given error. |
|
67 |
@param error The error for the task. |
|
68 |
*/ |
|
69 |
+ (instancetype)taskWithError:(NSError *)error; |
|
70 |
|
|
71 |
/*! |
|
72 |
Creates a task that is already completed with the given exception. |
|
73 |
@param exception The exception for the task. |
|
74 |
*/ |
|
75 |
+ (instancetype)taskWithException:(NSException *)exception; |
|
76 |
|
|
77 |
/*! |
|
78 |
Creates a task that is already cancelled. |
|
79 |
*/ |
|
80 |
+ (instancetype)cancelledTask; |
|
81 |
|
|
82 |
/*! |
|
83 |
Returns a task that will be completed (with result == nil) once |
|
84 |
all of the input tasks have completed. |
|
85 |
@param tasks An `NSArray` of the tasks to use as an input. |
|
86 |
*/ |
|
87 |
+ (instancetype)taskForCompletionOfAllTasks:(nullable NSArray<BFTask *> *)tasks; |
|
88 |
|
|
89 |
/*! |
|
90 |
Returns a task that will be completed once all of the input tasks have completed. |
|
91 |
If all tasks complete successfully without being faulted or cancelled the result will be |
|
92 |
an `NSArray` of all task results in the order they were provided. |
|
93 |
@param tasks An `NSArray` of the tasks to use as an input. |
|
94 |
*/ |
|
95 |
+ (instancetype)taskForCompletionOfAllTasksWithResults:(nullable NSArray<BFTask *> *)tasks; |
|
96 |
|
|
97 |
/*! |
|
98 |
Returns a task that will be completed once there is at least one successful task. |
|
99 |
The first task to successuly complete will set the result, all other tasks results are |
|
100 |
ignored. |
|
101 |
@param tasks An `NSArray` of the tasks to use as an input. |
|
102 |
*/ |
|
103 |
+ (instancetype)taskForCompletionOfAnyTask:(nullable NSArray<BFTask *> *)tasks; |
|
104 |
|
|
105 |
/*! |
|
106 |
Returns a task that will be completed a certain amount of time in the future. |
|
107 |
@param millis The approximate number of milliseconds to wait before the |
|
108 |
task will be finished (with result == nil). |
|
109 |
*/ |
|
110 |
+ (instancetype)taskWithDelay:(int)millis; |
|
111 |
|
|
112 |
/*! |
|
113 |
Returns a task that will be completed a certain amount of time in the future. |
|
114 |
@param millis The approximate number of milliseconds to wait before the |
|
115 |
task will be finished (with result == nil). |
|
116 |
@param token The cancellation token (optional). |
|
117 |
*/ |
|
118 |
+ (instancetype)taskWithDelay:(int)millis cancellationToken:(nullable BFCancellationToken *)token; |
|
119 |
|
|
120 |
/*! |
|
121 |
Returns a task that will be completed after the given block completes with |
|
122 |
the specified executor. |
|
123 |
@param executor A BFExecutor responsible for determining how the |
|
124 |
continuation block will be run. |
|
125 |
@param block The block to immediately schedule to run with the given executor. |
|
126 |
@returns A task that will be completed after block has run. |
|
127 |
If block returns a BFTask, then the task returned from |
|
128 |
this method will not be completed until that task is completed. |
|
129 |
*/ |
|
130 |
+ (instancetype)taskFromExecutor:(BFExecutor *)executor withBlock:(nullable id (^)())block; |
|
131 |
|
|
132 |
// Properties that will be set on the task once it is completed. |
|
133 |
|
|
134 |
/*! |
|
135 |
The result of a successful task. |
|
136 |
*/ |
|
137 |
@property (nullable, nonatomic, strong, readonly) ResultType result; |
|
138 |
|
|
139 |
/*! |
|
140 |
The error of a failed task. |
|
141 |
*/ |
|
142 |
@property (nullable, nonatomic, strong, readonly) NSError *error; |
|
143 |
|
|
144 |
/*! |
|
145 |
The exception of a failed task. |
|
146 |
*/ |
|
147 |
@property (nullable, nonatomic, strong, readonly) NSException *exception; |
|
148 |
|
|
149 |
/*! |
|
150 |
Whether this task has been cancelled. |
|
151 |
*/ |
|
152 |
@property (nonatomic, assign, readonly, getter=isCancelled) BOOL cancelled; |
|
153 |
|
|
154 |
/*! |
|
155 |
Whether this task has completed due to an error or exception. |
|
156 |
*/ |
|
157 |
@property (nonatomic, assign, readonly, getter=isFaulted) BOOL faulted; |
|
158 |
|
|
159 |
/*! |
|
160 |
Whether this task has completed. |
|
161 |
*/ |
|
162 |
@property (nonatomic, assign, readonly, getter=isCompleted) BOOL completed; |
|
163 |
|
|
164 |
/*! |
|
165 |
Enqueues the given block to be run once this task is complete. |
|
166 |
This method uses a default execution strategy. The block will be |
|
167 |
run on the thread where the previous task completes, unless the |
|
168 |
the stack depth is too deep, in which case it will be run on a |
|
169 |
dispatch queue with default priority. |
|
170 |
@param block The block to be run once this task is complete. |
|
171 |
@returns A task that will be completed after block has run. |
|
172 |
If block returns a BFTask, then the task returned from |
|
173 |
this method will not be completed until that task is completed. |
|
174 |
*/ |
|
175 |
- (BFTask *)continueWithBlock:(BFContinuationBlock)block; |
|
176 |
|
|
177 |
/*! |
|
178 |
Enqueues the given block to be run once this task is complete. |
|
179 |
This method uses a default execution strategy. The block will be |
|
180 |
run on the thread where the previous task completes, unless the |
|
181 |
the stack depth is too deep, in which case it will be run on a |
|
182 |
dispatch queue with default priority. |
|
183 |
@param block The block to be run once this task is complete. |
|
184 |
@param cancellationToken The cancellation token (optional). |
|
185 |
@returns A task that will be completed after block has run. |
|
186 |
If block returns a BFTask, then the task returned from |
|
187 |
this method will not be completed until that task is completed. |
|
188 |
*/ |
|
189 |
- (BFTask *)continueWithBlock:(BFContinuationBlock)block cancellationToken:(nullable BFCancellationToken *)cancellationToken; |
|
190 |
|
|
191 |
/*! |
|
192 |
Enqueues the given block to be run once this task is complete. |
|
193 |
@param executor A BFExecutor responsible for determining how the |
|
194 |
continuation block will be run. |
|
195 |
@param block The block to be run once this task is complete. |
|
196 |
@returns A task that will be completed after block has run. |
|
197 |
If block returns a BFTask, then the task returned from |
|
198 |
this method will not be completed until that task is completed. |
|
199 |
*/ |
|
200 |
- (BFTask *)continueWithExecutor:(BFExecutor *)executor withBlock:(BFContinuationBlock)block; |
|
201 |
/*! |
|
202 |
Enqueues the given block to be run once this task is complete. |
|
203 |
@param executor A BFExecutor responsible for determining how the |
|
204 |
continuation block will be run. |
|
205 |
@param block The block to be run once this task is complete. |
|
206 |
@param cancellationToken The cancellation token (optional). |
|
207 |
@returns A task that will be completed after block has run. |
|
208 |
If block returns a BFTask, then the task returned from |
|
209 |
his method will not be completed until that task is completed. |
|
210 |
*/ |
|
211 |
- (BFTask *)continueWithExecutor:(BFExecutor *)executor |
|
212 |
block:(BFContinuationBlock)block |
|
213 |
cancellationToken:(nullable BFCancellationToken *)cancellationToken; |
|
214 |
|
|
215 |
/*! |
|
216 |
Identical to continueWithBlock:, except that the block is only run |
|
217 |
if this task did not produce a cancellation, error, or exception. |
|
218 |
If it did, then the failure will be propagated to the returned |
|
219 |
task. |
|
220 |
@param block The block to be run once this task is complete. |
|
221 |
@returns A task that will be completed after block has run. |
|
222 |
If block returns a BFTask, then the task returned from |
|
223 |
this method will not be completed until that task is completed. |
|
224 |
*/ |
|
225 |
- (BFTask *)continueWithSuccessBlock:(BFContinuationBlock)block; |
|
226 |
|
|
227 |
/*! |
|
228 |
Identical to continueWithBlock:, except that the block is only run |
|
229 |
if this task did not produce a cancellation, error, or exception. |
|
230 |
If it did, then the failure will be propagated to the returned |
|
231 |
task. |
|
232 |
@param block The block to be run once this task is complete. |
|
233 |
@param cancellationToken The cancellation token (optional). |
|
234 |
@returns A task that will be completed after block has run. |
|
235 |
If block returns a BFTask, then the task returned from |
|
236 |
this method will not be completed until that task is completed. |
|
237 |
*/ |
|
238 |
- (BFTask *)continueWithSuccessBlock:(BFContinuationBlock)block cancellationToken:(nullable BFCancellationToken *)cancellationToken; |
|
239 |
|
|
240 |
/*! |
|
241 |
Identical to continueWithExecutor:withBlock:, except that the block |
|
242 |
is only run if this task did not produce a cancellation, error, or |
|
243 |
exception. If it did, then the failure will be propagated to the |
|
244 |
returned task. |
|
245 |
@param executor A BFExecutor responsible for determining how the |
|
246 |
continuation block will be run. |
|
247 |
@param block The block to be run once this task is complete. |
|
248 |
@returns A task that will be completed after block has run. |
|
249 |
If block returns a BFTask, then the task returned from |
|
250 |
this method will not be completed until that task is completed. |
|
251 |
*/ |
|
252 |
- (BFTask *)continueWithExecutor:(BFExecutor *)executor withSuccessBlock:(BFContinuationBlock)block; |
|
253 |
|
|
254 |
/*! |
|
255 |
Identical to continueWithExecutor:withBlock:, except that the block |
|
256 |
is only run if this task did not produce a cancellation, error, or |
|
257 |
exception. If it did, then the failure will be propagated to the |
|
258 |
returned task. |
|
259 |
@param executor A BFExecutor responsible for determining how the |
|
260 |
continuation block will be run. |
|
261 |
@param block The block to be run once this task is complete. |
|
262 |
@param cancellationToken The cancellation token (optional). |
|
263 |
@returns A task that will be completed after block has run. |
|
264 |
If block returns a BFTask, then the task returned from |
|
265 |
this method will not be completed until that task is completed. |
|
266 |
*/ |
|
267 |
- (BFTask *)continueWithExecutor:(BFExecutor *)executor |
|
268 |
successBlock:(BFContinuationBlock)block |
|
269 |
cancellationToken:(nullable BFCancellationToken *)cancellationToken; |
|
270 |
|
|
271 |
/*! |
|
272 |
Waits until this operation is completed. |
|
273 |
This method is inefficient and consumes a thread resource while |
|
274 |
it's running. It should be avoided. This method logs a warning |
|
275 |
message if it is used on the main thread. |
|
276 |
*/ |
|
277 |
- (void)waitUntilFinished; |
|
278 |
|
|
279 |
@end |
|
280 |
|
|
281 |
NS_ASSUME_NONNULL_END |