RACScheduler.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // RACScheduler.h
  3. // ReactiveObjC
  4. //
  5. // Created by Josh Abernathy on 4/16/12.
  6. // Copyright (c) 2012 GitHub, Inc. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. NS_ASSUME_NONNULL_BEGIN
  10. /// The priority for the scheduler.
  11. ///
  12. /// RACSchedulerPriorityHigh - High priority.
  13. /// RACSchedulerPriorityDefault - Default priority.
  14. /// RACSchedulerPriorityLow - Low priority.
  15. /// RACSchedulerPriorityBackground - Background priority.
  16. typedef enum : long {
  17. RACSchedulerPriorityHigh = DISPATCH_QUEUE_PRIORITY_HIGH,
  18. RACSchedulerPriorityDefault = DISPATCH_QUEUE_PRIORITY_DEFAULT,
  19. RACSchedulerPriorityLow = DISPATCH_QUEUE_PRIORITY_LOW,
  20. RACSchedulerPriorityBackground = DISPATCH_QUEUE_PRIORITY_BACKGROUND,
  21. } RACSchedulerPriority;
  22. /// Scheduled with -scheduleRecursiveBlock:, this type of block is passed a block
  23. /// with which it can call itself recursively.
  24. typedef void (^RACSchedulerRecursiveBlock)(void (^reschedule)(void));
  25. @class RACDisposable;
  26. /// Schedulers are used to control when and where work is performed.
  27. @interface RACScheduler : NSObject
  28. /// A singleton scheduler that immediately executes the blocks it is given.
  29. ///
  30. /// **Note:** Unlike most other schedulers, this does not set the current
  31. /// scheduler. There may still be a valid +currentScheduler if this is used
  32. /// within a block scheduled on a different scheduler.
  33. + (RACScheduler *)immediateScheduler;
  34. /// A singleton scheduler that executes blocks in the main thread.
  35. + (RACScheduler *)mainThreadScheduler;
  36. /// Creates and returns a new background scheduler with the given priority and
  37. /// name. The name is for debug and instrumentation purposes only.
  38. ///
  39. /// Scheduler creation is cheap. It's unnecessary to save the result of this
  40. /// method call unless you want to serialize some actions on the same background
  41. /// scheduler.
  42. + (RACScheduler *)schedulerWithPriority:(RACSchedulerPriority)priority name:(nullable NSString *)name;
  43. /// Invokes +schedulerWithPriority:name: with a default name.
  44. + (RACScheduler *)schedulerWithPriority:(RACSchedulerPriority)priority;
  45. /// Invokes +schedulerWithPriority: with RACSchedulerPriorityDefault.
  46. + (RACScheduler *)scheduler;
  47. /// The current scheduler. This will only be valid when used from within a
  48. /// -[RACScheduler schedule:] block or when on the main thread.
  49. + (nullable RACScheduler *)currentScheduler;
  50. /// Schedule the given block for execution on the scheduler.
  51. ///
  52. /// Scheduled blocks will be executed in the order in which they were scheduled.
  53. ///
  54. /// block - The block to schedule for execution. Cannot be nil.
  55. ///
  56. /// Returns a disposable which can be used to cancel the scheduled block before
  57. /// it begins executing, or nil if cancellation is not supported.
  58. - (nullable RACDisposable *)schedule:(void (^)(void))block;
  59. /// Schedule the given block for execution on the scheduler at or after
  60. /// a specific time.
  61. ///
  62. /// Note that blocks scheduled for a certain time will not preempt any other
  63. /// scheduled work that is executing at the time.
  64. ///
  65. /// When invoked on the +immediateScheduler, the calling thread **will block**
  66. /// until the specified time.
  67. ///
  68. /// date - The earliest time at which `block` should begin executing. The block
  69. /// may not execute immediately at this time, whether due to system load
  70. /// or another block on the scheduler currently being run. Cannot be nil.
  71. /// block - The block to schedule for execution. Cannot be nil.
  72. ///
  73. /// Returns a disposable which can be used to cancel the scheduled block before
  74. /// it begins executing, or nil if cancellation is not supported.
  75. - (nullable RACDisposable *)after:(NSDate *)date schedule:(void (^)(void))block;
  76. /// Schedule the given block for execution on the scheduler after the delay.
  77. ///
  78. /// Converts the delay into an NSDate, then invokes `-after:schedule:`.
  79. - (nullable RACDisposable *)afterDelay:(NSTimeInterval)delay schedule:(void (^)(void))block;
  80. /// Reschedule the given block at a particular interval, starting at a specific
  81. /// time, and with a given leeway for deferral.
  82. ///
  83. /// Note that blocks scheduled for a certain time will not preempt any other
  84. /// scheduled work that is executing at the time.
  85. ///
  86. /// Regardless of the value of `leeway`, the given block may not execute exactly
  87. /// at `when` or exactly on successive intervals, whether due to system load or
  88. /// because another block is currently being run on the scheduler.
  89. ///
  90. /// It is considered undefined behavior to invoke this method on the
  91. /// +immediateScheduler.
  92. ///
  93. /// date - The earliest time at which `block` should begin executing. The
  94. /// block may not execute immediately at this time, whether due to
  95. /// system load or another block on the scheduler currently being
  96. /// run. Cannot be nil.
  97. /// interval - The interval at which the block should be rescheduled, starting
  98. /// from `date`. This will use the system wall clock, to avoid
  99. /// skew when the computer goes to sleep.
  100. /// leeway - A hint to the system indicating the number of seconds that each
  101. /// scheduling can be deferred. Note that this is just a hint, and
  102. /// there may be some additional latency no matter what.
  103. /// block - The block to repeatedly schedule for execution. Cannot be nil.
  104. ///
  105. /// Returns a disposable which can be used to cancel the automatic scheduling and
  106. /// rescheduling, or nil if cancellation is not supported.
  107. - (nullable RACDisposable *)after:(NSDate *)date repeatingEvery:(NSTimeInterval)interval withLeeway:(NSTimeInterval)leeway schedule:(void (^)(void))block;
  108. /// Schedule the given recursive block for execution on the scheduler. The
  109. /// scheduler will automatically flatten any recursive scheduling into iteration
  110. /// instead, so this can be used without issue for blocks that may keep invoking
  111. /// themselves forever.
  112. ///
  113. /// Scheduled blocks will be executed in the order in which they were scheduled.
  114. ///
  115. /// recursiveBlock - The block to schedule for execution. When invoked, the
  116. /// recursive block will be passed a `void (^)(void)` block
  117. /// which will reschedule the recursive block at the end of the
  118. /// receiver's queue. This passed-in block will automatically
  119. /// skip scheduling if the scheduling of the `recursiveBlock`
  120. /// was disposed in the meantime.
  121. ///
  122. /// Returns a disposable which can be used to cancel the scheduled block before
  123. /// it begins executing, or to stop it from rescheduling if it's already begun
  124. /// execution.
  125. - (nullable RACDisposable *)scheduleRecursiveBlock:(RACSchedulerRecursiveBlock)recursiveBlock;
  126. @end
  127. NS_ASSUME_NONNULL_END