No Description

AWSTaskCompletionSource.m 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 "AWSTaskCompletionSource.h"
  11. #import "AWSTask.h"
  12. NS_ASSUME_NONNULL_BEGIN
  13. @interface AWSTask (AWSTaskCompletionSource)
  14. - (BOOL)trySetResult:(nullable id)result;
  15. - (BOOL)trySetError:(NSError *)error;
  16. - (BOOL)trySetCancelled;
  17. @end
  18. @implementation AWSTaskCompletionSource
  19. #pragma mark - Initializer
  20. + (instancetype)taskCompletionSource {
  21. return [[self alloc] init];
  22. }
  23. - (instancetype)init {
  24. self = [super init];
  25. if (!self) return self;
  26. _task = [[AWSTask alloc] init];
  27. return self;
  28. }
  29. #pragma mark - Custom Setters/Getters
  30. - (void)setResult:(nullable id)result {
  31. if (![self.task trySetResult:result]) {
  32. [NSException raise:NSInternalInconsistencyException
  33. format:@"Cannot set the result on a completed task."];
  34. }
  35. }
  36. - (void)setError:(NSError *)error {
  37. if (![self.task trySetError:error]) {
  38. [NSException raise:NSInternalInconsistencyException
  39. format:@"Cannot set the error on a completed task."];
  40. }
  41. }
  42. - (void)cancel {
  43. if (![self.task trySetCancelled]) {
  44. [NSException raise:NSInternalInconsistencyException
  45. format:@"Cannot cancel a completed task."];
  46. }
  47. }
  48. - (BOOL)trySetResult:(nullable id)result {
  49. return [self.task trySetResult:result];
  50. }
  51. - (BOOL)trySetError:(NSError *)error {
  52. return [self.task trySetError:error];
  53. }
  54. - (BOOL)trySetCancelled {
  55. return [self.task trySetCancelled];
  56. }
  57. @end
  58. NS_ASSUME_NONNULL_END