OSSTaskCompletionSource.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. NS_ASSUME_NONNULL_BEGIN
  12. @class OSSTask<ResultType>;
  13. /*!
  14. A OSSTaskCompletionSource represents the producer side of tasks.
  15. It is a task that also has methods for changing the state of the
  16. task by settings its completion values.
  17. */
  18. @interface OSSTaskCompletionSource<__covariant ResultType> : NSObject
  19. /*!
  20. Creates a new unfinished task.
  21. */
  22. + (instancetype)taskCompletionSource;
  23. /*!
  24. The task associated with this TaskCompletionSource.
  25. */
  26. @property (nonatomic, strong, readonly) OSSTask<ResultType> *task;
  27. /*!
  28. Completes the task by setting the result.
  29. Attempting to set this for a completed task will raise an exception.
  30. @param result The result of the task.
  31. */
  32. - (void)setResult:(nullable ResultType)result;
  33. /*!
  34. Completes the task by setting the error.
  35. Attempting to set this for a completed task will raise an exception.
  36. @param error The error for the task.
  37. */
  38. - (void)setError:(NSError *)error;
  39. /*!
  40. Completes the task by setting an exception.
  41. Attempting to set this for a completed task will raise an exception.
  42. @param exception The exception for the task.
  43. */
  44. - (void)setException:(NSException *)exception;
  45. /*!
  46. Completes the task by marking it as cancelled.
  47. Attempting to set this for a completed task will raise an exception.
  48. */
  49. - (void)cancel;
  50. /*!
  51. Sets the result of the task if it wasn't already completed.
  52. @returns whether the new value was set.
  53. */
  54. - (BOOL)trySetResult:(nullable ResultType)result;
  55. /*!
  56. Sets the error of the task if it wasn't already completed.
  57. @param error The error for the task.
  58. @returns whether the new value was set.
  59. */
  60. - (BOOL)trySetError:(NSError *)error;
  61. /*!
  62. Sets the exception of the task if it wasn't already completed.
  63. @param exception The exception for the task.
  64. @returns whether the new value was set.
  65. */
  66. - (BOOL)trySetException:(NSException *)exception;
  67. /*!
  68. Sets the cancellation state of the task if it wasn't already completed.
  69. @returns whether the new value was set.
  70. */
  71. - (BOOL)trySetCancelled;
  72. @end
  73. NS_ASSUME_NONNULL_END