FSTDispatchQueue.mm 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 <memory>
  18. #include <utility>
  19. #import "Firestore/Source/Util/FSTDispatchQueue.h"
  20. #include "Firestore/core/src/firebase/firestore/util/executor_libdispatch.h"
  21. #include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
  22. #include "absl/memory/memory.h"
  23. using firebase::firestore::util::AsyncQueue;
  24. using firebase::firestore::util::DelayedOperation;
  25. using firebase::firestore::util::TimerId;
  26. using firebase::firestore::util::internal::Executor;
  27. using firebase::firestore::util::internal::ExecutorLibdispatch;
  28. NS_ASSUME_NONNULL_BEGIN
  29. #pragma mark - FSTDelayedCallback
  30. @interface FSTDelayedCallback () {
  31. DelayedOperation _impl;
  32. }
  33. @end
  34. @implementation FSTDelayedCallback
  35. - (instancetype)initWithImpl:(DelayedOperation &&)impl {
  36. if (self = [super init]) {
  37. _impl = std::move(impl);
  38. }
  39. return self;
  40. }
  41. - (void)cancel {
  42. _impl.Cancel();
  43. }
  44. @end
  45. #pragma mark - FSTDispatchQueue
  46. @implementation FSTDispatchQueue {
  47. std::unique_ptr<AsyncQueue> _impl;
  48. }
  49. - (AsyncQueue *)implementation {
  50. return _impl.get();
  51. }
  52. + (TimerId)convertTimerId:(FSTTimerID)objcTimerID {
  53. const TimerId converted = static_cast<TimerId>(objcTimerID);
  54. switch (converted) {
  55. case TimerId::All:
  56. case TimerId::ListenStreamIdle:
  57. case TimerId::ListenStreamConnectionBackoff:
  58. case TimerId::WriteStreamIdle:
  59. case TimerId::WriteStreamConnectionBackoff:
  60. case TimerId::OnlineStateTimeout:
  61. return converted;
  62. default:
  63. HARD_FAIL("Unknown value of enum FSTTimerID.");
  64. }
  65. }
  66. + (instancetype)queueWith:(dispatch_queue_t)dispatchQueue {
  67. return [[FSTDispatchQueue alloc] initWithQueue:dispatchQueue];
  68. }
  69. - (instancetype)initWithQueue:(dispatch_queue_t)queue {
  70. if (self = [super init]) {
  71. _queue = queue;
  72. auto executor = absl::make_unique<ExecutorLibdispatch>(queue);
  73. _impl = absl::make_unique<AsyncQueue>(std::move(executor));
  74. }
  75. return self;
  76. }
  77. - (void)verifyIsCurrentQueue {
  78. _impl->VerifyIsCurrentQueue();
  79. }
  80. - (void)enterCheckedOperation:(void (^)(void))block {
  81. _impl->ExecuteBlocking([block] { block(); });
  82. }
  83. - (void)dispatchAsync:(void (^)(void))block {
  84. _impl->Enqueue([block] { block(); });
  85. }
  86. - (void)dispatchAsyncAllowingSameQueue:(void (^)(void))block {
  87. _impl->EnqueueRelaxed([block] { block(); });
  88. }
  89. - (void)dispatchSync:(void (^)(void))block {
  90. _impl->EnqueueBlocking([block] { block(); });
  91. }
  92. - (FSTDelayedCallback *)dispatchAfterDelay:(NSTimeInterval)delay
  93. timerID:(FSTTimerID)timerID
  94. block:(void (^)(void))block {
  95. const AsyncQueue::Milliseconds delayMs =
  96. std::chrono::milliseconds(static_cast<long long>(delay * 1000));
  97. const TimerId convertedTimerId = [FSTDispatchQueue convertTimerId:timerID];
  98. DelayedOperation delayed_operation =
  99. _impl->EnqueueAfterDelay(delayMs, convertedTimerId, [block] { block(); });
  100. return [[FSTDelayedCallback alloc] initWithImpl:std::move(delayed_operation)];
  101. }
  102. - (BOOL)containsDelayedCallbackWithTimerID:(FSTTimerID)timerID {
  103. return _impl->IsScheduled([FSTDispatchQueue convertTimerId:timerID]);
  104. }
  105. - (void)runDelayedCallbacksUntil:(FSTTimerID)lastTimerID {
  106. _impl->RunScheduledOperationsUntil([FSTDispatchQueue convertTimerId:lastTimerID]);
  107. }
  108. @end
  109. NS_ASSUME_NONNULL_END