FSTEventManager.mm 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 "Firestore/Source/Core/FSTEventManager.h"
  17. #include <unordered_map>
  18. #include <utility>
  19. #include <vector>
  20. #import "Firestore/Source/Core/FSTQuery.h"
  21. #import "Firestore/Source/Core/FSTSyncEngine.h"
  22. #include "Firestore/core/src/firebase/firestore/model/document_set.h"
  23. #include "Firestore/core/src/firebase/firestore/util/error_apple.h"
  24. #include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
  25. #include "Firestore/core/src/firebase/firestore/util/objc_compatibility.h"
  26. #include "Firestore/core/src/firebase/firestore/util/status.h"
  27. #include "absl/algorithm/container.h"
  28. #include "absl/types/optional.h"
  29. NS_ASSUME_NONNULL_BEGIN
  30. namespace objc = firebase::firestore::util::objc;
  31. using firebase::firestore::core::DocumentViewChange;
  32. using firebase::firestore::core::ViewSnapshot;
  33. using firebase::firestore::model::OnlineState;
  34. using firebase::firestore::model::TargetId;
  35. using firebase::firestore::util::MakeStatus;
  36. using firebase::firestore::util::Status;
  37. #pragma mark - FSTQueryListenersInfo
  38. namespace {
  39. /**
  40. * Holds the listeners and the last received ViewSnapshot for a query being tracked by
  41. * EventManager.
  42. */
  43. struct QueryListenersInfo {
  44. TargetId target_id;
  45. std::vector<std::shared_ptr<QueryListener>> listeners;
  46. void Erase(const std::shared_ptr<QueryListener> &listener) {
  47. auto found = absl::c_find(listeners, listener);
  48. if (found != listeners.end()) {
  49. listeners.erase(found);
  50. }
  51. }
  52. const absl::optional<ViewSnapshot> &view_snapshot() const {
  53. return snapshot_;
  54. }
  55. void set_view_snapshot(const absl::optional<ViewSnapshot> &snapshot) {
  56. snapshot_ = snapshot;
  57. }
  58. private:
  59. // Other members are public in this struct, ensure that any reads are
  60. // copies by requiring reads to go through a const getter.
  61. absl::optional<ViewSnapshot> snapshot_;
  62. };
  63. } // namespace
  64. #pragma mark - FSTEventManager
  65. @interface FSTEventManager () <FSTSyncEngineDelegate>
  66. - (instancetype)initWithSyncEngine:(FSTSyncEngine *)syncEngine NS_DESIGNATED_INITIALIZER;
  67. @property(nonatomic, strong, readonly) FSTSyncEngine *syncEngine;
  68. @property(nonatomic, assign) OnlineState onlineState;
  69. @end
  70. @implementation FSTEventManager {
  71. objc::unordered_map<FSTQuery *, QueryListenersInfo> _queries;
  72. }
  73. + (instancetype)eventManagerWithSyncEngine:(FSTSyncEngine *)syncEngine {
  74. return [[FSTEventManager alloc] initWithSyncEngine:syncEngine];
  75. }
  76. - (instancetype)initWithSyncEngine:(FSTSyncEngine *)syncEngine {
  77. if (self = [super init]) {
  78. _syncEngine = syncEngine;
  79. _syncEngine.syncEngineDelegate = self;
  80. }
  81. return self;
  82. }
  83. - (TargetId)addListener:(std::shared_ptr<QueryListener>)listener {
  84. FSTQuery *query = listener->query();
  85. auto inserted = _queries.emplace(query, QueryListenersInfo{});
  86. bool first_listen = inserted.second;
  87. QueryListenersInfo &query_info = inserted.first->second;
  88. query_info.listeners.push_back(listener);
  89. listener->OnOnlineStateChanged(self.onlineState);
  90. if (query_info.view_snapshot().has_value()) {
  91. listener->OnViewSnapshot(query_info.view_snapshot().value());
  92. }
  93. if (first_listen) {
  94. query_info.target_id = [self.syncEngine listenToQuery:query];
  95. }
  96. return query_info.target_id;
  97. }
  98. - (void)removeListener:(const std::shared_ptr<QueryListener> &)listener {
  99. FSTQuery *query = listener->query();
  100. bool last_listen = false;
  101. auto found_iter = _queries.find(query);
  102. if (found_iter != _queries.end()) {
  103. QueryListenersInfo &query_info = found_iter->second;
  104. query_info.Erase(listener);
  105. last_listen = query_info.listeners.empty();
  106. }
  107. if (last_listen) {
  108. _queries.erase(found_iter);
  109. [self.syncEngine stopListeningToQuery:query];
  110. }
  111. }
  112. - (void)handleViewSnapshots:(std::vector<ViewSnapshot> &&)viewSnapshots {
  113. for (ViewSnapshot &viewSnapshot : viewSnapshots) {
  114. FSTQuery *query = viewSnapshot.query();
  115. auto found_iter = _queries.find(query);
  116. if (found_iter != _queries.end()) {
  117. QueryListenersInfo &query_info = found_iter->second;
  118. for (const auto &listener : query_info.listeners) {
  119. listener->OnViewSnapshot(viewSnapshot);
  120. }
  121. query_info.set_view_snapshot(std::move(viewSnapshot));
  122. }
  123. }
  124. }
  125. - (void)handleError:(NSError *)error forQuery:(FSTQuery *)query {
  126. auto found_iter = _queries.find(query);
  127. if (found_iter != _queries.end()) {
  128. QueryListenersInfo &query_info = found_iter->second;
  129. for (const auto &listener : query_info.listeners) {
  130. listener->OnError(MakeStatus(error));
  131. }
  132. // Remove all listeners. NOTE: We don't need to call [FSTSyncEngine stopListening] after an
  133. // error.
  134. _queries.erase(found_iter);
  135. }
  136. }
  137. - (void)applyChangedOnlineState:(OnlineState)onlineState {
  138. self.onlineState = onlineState;
  139. for (auto &&kv : _queries) {
  140. QueryListenersInfo &info = kv.second;
  141. for (auto &&listener : info.listeners) {
  142. listener->OnOnlineStateChanged(onlineState);
  143. }
  144. }
  145. }
  146. @end
  147. NS_ASSUME_NONNULL_END