FSTDispatchQueue.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <Foundation/Foundation.h>
  17. #include "Firestore/core/src/firebase/firestore/model/types.h"
  18. NS_ASSUME_NONNULL_BEGIN
  19. /**
  20. * Well-known "timer" IDs used when scheduling delayed callbacks on the FSTDispatchQueue. These IDs
  21. * can then be used from tests to check for the presence of callbacks or to run them early.
  22. */
  23. typedef NS_ENUM(NSInteger, FSTTimerID) {
  24. /** All can be used with runDelayedCallbacksUntil: to run all timers. */
  25. FSTTimerIDAll,
  26. /**
  27. * The following 4 timers are used in FSTStream for the listen and write streams. The "Idle" timer
  28. * is used to close the stream due to inactivity. The "ConnectionBackoff" timer is used to
  29. * restart a stream once the appropriate backoff delay has elapsed.
  30. */
  31. FSTTimerIDListenStreamIdle,
  32. FSTTimerIDListenStreamConnectionBackoff,
  33. FSTTimerIDWriteStreamIdle,
  34. FSTTimerIDWriteStreamConnectionBackoff,
  35. /**
  36. * A timer used in FSTOnlineStateTracker to transition from OnlineState Unknown to Offline
  37. * after a set timeout, rather than waiting indefinitely for success or failure.
  38. */
  39. FSTTimerIDOnlineStateTimeout
  40. };
  41. /**
  42. * Handle to a callback scheduled via [FSTDispatchQueue dispatchAfterDelay:]. Supports cancellation
  43. * via the cancel method.
  44. */
  45. @interface FSTDelayedCallback : NSObject
  46. /**
  47. * Cancels the callback if it hasn't already been executed or canceled.
  48. *
  49. * As long as the callback has not yet been run, calling cancel() (from a callback already running
  50. * on the dispatch queue) provides a guarantee that the operation will not be run.
  51. */
  52. - (void)cancel;
  53. @end
  54. @interface FSTDispatchQueue : NSObject
  55. /** Creates and returns an FSTDispatchQueue wrapping the specified dispatch_queue_t. */
  56. + (instancetype)queueWith:(dispatch_queue_t)dispatchQueue;
  57. - (instancetype)initWithQueue:(dispatch_queue_t)queue NS_DESIGNATED_INITIALIZER;
  58. - (instancetype)init __attribute__((unavailable("Use static constructor method.")));
  59. /**
  60. * Asserts that we are already running on this queue (actually, we can only verify that the
  61. * queue's label is the same, but hopefully that's good enough.)
  62. */
  63. - (void)verifyIsCurrentQueue;
  64. /**
  65. * Declares that we are already executing on the correct dispatch_queue_t and would like to
  66. * officially execute code on behalf of this FSTDispatchQueue. To be used only when called back
  67. * by some other API directly onto our queue. This allows us to safely dispatch directly onto the
  68. * worker queue without destroying the invariants this class helps us maintain.
  69. */
  70. - (void)enterCheckedOperation:(void (^)(void))block;
  71. /**
  72. * Same as dispatch_async() except it asserts that we're not already on the queue, since this
  73. * generally indicates a bug (and can lead to re-ordering of operations, etc).
  74. *
  75. * @param block The block to run.
  76. */
  77. - (void)dispatchAsync:(void (^)(void))block;
  78. /**
  79. * Unlike dispatchAsync: this method does not require you to dispatch to a different queue than
  80. * the current one (thus it is equivalent to a raw dispatch_async()).
  81. *
  82. * This is useful, e.g. for dispatching to the user's queue directly from user API call (in which
  83. * case we don't know if we're already on the user's queue or not).
  84. *
  85. * @param block The block to run.
  86. */
  87. - (void)dispatchAsyncAllowingSameQueue:(void (^)(void))block;
  88. /**
  89. * Wrapper for dispatch_sync(). Mostly meant for use in tests.
  90. *
  91. * @param block The block to run.
  92. */
  93. - (void)dispatchSync:(void (^)(void))block;
  94. /**
  95. * Schedules a callback after the specified delay.
  96. *
  97. * Unlike dispatchAsync: this method does not require you to dispatch to a different queue than
  98. * the current one.
  99. *
  100. * The returned FSTDelayedCallback handle can be used to cancel the callback prior to its running.
  101. *
  102. * @param block The block to run.
  103. * @param delay The delay (in seconds) after which to run the block.
  104. * @param timerID An FSTTimerID that can be used from tests to check for the presence of this
  105. * callback or to schedule it to run early.
  106. * @return A FSTDelayedCallback instance that can be used for cancellation.
  107. */
  108. - (FSTDelayedCallback *)dispatchAfterDelay:(NSTimeInterval)delay
  109. timerID:(FSTTimerID)timerID
  110. block:(void (^)(void))block;
  111. /**
  112. * For Tests: Determine if a delayed callback with a particular FSTTimerID exists.
  113. */
  114. - (BOOL)containsDelayedCallbackWithTimerID:(FSTTimerID)timerID;
  115. /**
  116. * For Tests: Runs delayed callbacks early, blocking until completion.
  117. *
  118. * @param lastTimerID Only delayed callbacks up to and including one that was scheduled using this
  119. * FSTTimerID will be run. Method throws if no matching callback exists.
  120. */
  121. - (void)runDelayedCallbacksUntil:(FSTTimerID)lastTimerID;
  122. /** The underlying wrapped dispatch_queue_t */
  123. @property(nonatomic, strong, readonly) dispatch_queue_t queue;
  124. @end
  125. NS_ASSUME_NONNULL_END