aliyun-oss-react-native

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