FBRunLoopSpinner.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * Copyright (c) 2015-present, 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. #import <Foundation/Foundation.h>
  10. NS_ASSUME_NONNULL_BEGIN
  11. typedef BOOL (^FBRunLoopSpinnerBlock)(void);
  12. typedef __nullable id (^FBRunLoopSpinnerObjectBlock)(void);
  13. @interface FBRunLoopSpinner : NSObject
  14. /**
  15. Dispatches block and spins the run loop until `completion` block is called.
  16. @param block the block to wait for to finish.
  17. */
  18. + (void)spinUntilCompletion:(void (^)(void(^completion)(void)))block;
  19. /**
  20. Updates the error message to print in the event of a timeout.
  21. @param timeoutErrorMessage the Error Message to print.
  22. @return the receiver, for chaining.
  23. */
  24. - (instancetype)timeoutErrorMessage:(NSString *)timeoutErrorMessage;
  25. /**
  26. Updates the timeout of the receiver.
  27. @param timeout the amount of time to wait before timing out.
  28. @return the receiver, for chaining.
  29. */
  30. - (instancetype)timeout:(NSTimeInterval)timeout;
  31. /**
  32. Updates the interval of the receiver.
  33. @param interval the amount of time to wait before checking condition again.
  34. @return the receiver, for chaining.
  35. */
  36. - (instancetype)interval:(NSTimeInterval)interval;
  37. /**
  38. Spins the Run Loop until `untilTrue` returns YES or a timeout is reached.
  39. @param untilTrue the condition to meet.
  40. @return YES if the condition was met, NO if the timeout was reached first.
  41. */
  42. - (BOOL)spinUntilTrue:(FBRunLoopSpinnerBlock)untilTrue;
  43. /**
  44. Spins the Run Loop until `untilTrue` returns YES or a timeout is reached.
  45. @param untilTrue the condition to meet.
  46. @param error to fill in case of timeout.
  47. @return YES if the condition was met, NO if the timeout was reached first.
  48. */
  49. - (BOOL)spinUntilTrue:(FBRunLoopSpinnerBlock)untilTrue error:(NSError **)error;
  50. /**
  51. Spins the Run Loop until `untilNotNil` returns non nil value or a timeout is reached.
  52. @param untilNotNil the condition to meet.
  53. @param error to fill in case of timeout.
  54. @return YES if the condition was met, NO if the timeout was reached first.
  55. */
  56. - (nullable id)spinUntilNotNil:(FBRunLoopSpinnerObjectBlock)untilNotNil error:(NSError **)error;
  57. @end
  58. NS_ASSUME_NONNULL_END