BFTaskCompletionSource.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 BFTask<ResultType>;
  13. /*!
  14. A BFTaskCompletionSource 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 BFTaskCompletionSource<__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) BFTask<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. @deprecated `BFTask` exception handling is deprecated and will be removed in a future release.
  44. */
  45. - (void)setException:(NSException *)exception
  46. __attribute__((deprecated("`BFTask` exception handling is deprecated and will be removed in a future release.")));
  47. /*!
  48. Completes the task by marking it as cancelled.
  49. Attempting to set this for a completed task will raise an exception.
  50. */
  51. - (void)cancel;
  52. /*!
  53. Sets the result of the task if it wasn't already completed.
  54. @returns whether the new value was set.
  55. */
  56. - (BOOL)trySetResult:(nullable ResultType)result;
  57. /*!
  58. Sets the error of the task if it wasn't already completed.
  59. @param error The error for the task.
  60. @returns whether the new value was set.
  61. */
  62. - (BOOL)trySetError:(NSError *)error;
  63. /*!
  64. Sets the exception of the task if it wasn't already completed.
  65. @param exception The exception for the task.
  66. @returns whether the new value was set.
  67. @deprecated `BFTask` exception handling is deprecated and will be removed in a future release.
  68. */
  69. - (BOOL)trySetException:(NSException *)exception
  70. __attribute__((deprecated("`BFTask` exception handling is deprecated and will be removed in a future release.")));
  71. /*!
  72. Sets the cancellation state of the task if it wasn't already completed.
  73. @returns whether the new value was set.
  74. */
  75. - (BOOL)trySetCancelled;
  76. @end
  77. NS_ASSUME_NONNULL_END