FSTEventManager.mm 5.4 KB

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