FSTView.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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/core/view_snapshot.h"
  18. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  19. #include "Firestore/core/src/firebase/firestore/model/document_key_set.h"
  20. #include "Firestore/core/src/firebase/firestore/model/document_map.h"
  21. #include "Firestore/core/src/firebase/firestore/model/types.h"
  22. #include "absl/types/optional.h"
  23. namespace firebase {
  24. namespace firestore {
  25. namespace remote {
  26. class TargetChange;
  27. } // namespace remote
  28. } // namespace firestore
  29. } // namespace firebase
  30. @class FSTDocumentSet;
  31. @class FSTQuery;
  32. NS_ASSUME_NONNULL_BEGIN
  33. #pragma mark - FSTViewDocumentChanges
  34. /** The result of applying a set of doc changes to a view. */
  35. @interface FSTViewDocumentChanges : NSObject
  36. - (instancetype)init NS_UNAVAILABLE;
  37. - (const firebase::firestore::model::DocumentKeySet &)mutatedKeys;
  38. /** The new set of docs that should be in the view. */
  39. @property(nonatomic, strong, readonly) FSTDocumentSet *documentSet;
  40. /** The diff of this these docs with the previous set of docs. */
  41. - (const firebase::firestore::core::DocumentViewChangeSet &)changeSet;
  42. /**
  43. * Whether the set of documents passed in was not sufficient to calculate the new state of the view
  44. * and there needs to be another pass based on the local cache.
  45. */
  46. @property(nonatomic, assign, readonly) BOOL needsRefill;
  47. @end
  48. #pragma mark - FSTLimboDocumentChange
  49. typedef NS_ENUM(NSInteger, FSTLimboDocumentChangeType) {
  50. FSTLimboDocumentChangeTypeAdded = 0,
  51. FSTLimboDocumentChangeTypeRemoved,
  52. };
  53. // A change to a particular document wrt to whether it is in "limbo".
  54. @interface FSTLimboDocumentChange : NSObject
  55. + (instancetype)changeWithType:(FSTLimboDocumentChangeType)type
  56. key:(firebase::firestore::model::DocumentKey)key;
  57. - (id)init __attribute__((unavailable("Use a static constructor method.")));
  58. - (const firebase::firestore::model::DocumentKey &)key;
  59. @property(nonatomic, assign, readonly) FSTLimboDocumentChangeType type;
  60. @end
  61. #pragma mark - FSTViewChange
  62. // A set of changes to a view.
  63. @interface FSTViewChange : NSObject
  64. - (id)init __attribute__((unavailable("Use a static constructor method.")));
  65. - (absl::optional<firebase::firestore::core::ViewSnapshot> &)snapshot;
  66. @property(nonatomic, strong, readonly) NSArray<FSTLimboDocumentChange *> *limboChanges;
  67. @end
  68. #pragma mark - FSTView
  69. /**
  70. * View is responsible for computing the final merged truth of what docs are in a query. It gets
  71. * notified of local and remote changes to docs, and applies the query filters and limits to
  72. * determine the most correct possible results.
  73. */
  74. @interface FSTView : NSObject
  75. - (instancetype)init NS_UNAVAILABLE;
  76. - (instancetype)initWithQuery:(FSTQuery *)query
  77. remoteDocuments:(firebase::firestore::model::DocumentKeySet)remoteDocuments
  78. NS_DESIGNATED_INITIALIZER;
  79. /**
  80. * Iterates over a set of doc changes, applies the query limit, and computes what the new results
  81. * should be, what the changes were, and whether we may need to go back to the local cache for
  82. * more results. Does not make any changes to the view.
  83. *
  84. * @param docChanges The doc changes to apply to this view.
  85. * @return a new set of docs, changes, and refill flag.
  86. */
  87. - (FSTViewDocumentChanges *)computeChangesWithDocuments:
  88. (const firebase::firestore::model::MaybeDocumentMap &)docChanges;
  89. /**
  90. * Iterates over a set of doc changes, applies the query limit, and computes what the new results
  91. * should be, what the changes were, and whether we may need to go back to the local cache for
  92. * more results. Does not make any changes to the view.
  93. *
  94. * @param docChanges The doc changes to apply to this view.
  95. * @param previousChanges If this is being called with a refill, then start with this set of docs
  96. * and changes instead of the current view.
  97. * @return a new set of docs, changes, and refill flag.
  98. */
  99. - (FSTViewDocumentChanges *)
  100. computeChangesWithDocuments:(const firebase::firestore::model::MaybeDocumentMap &)docChanges
  101. previousChanges:(nullable FSTViewDocumentChanges *)previousChanges;
  102. /**
  103. * Updates the view with the given ViewDocumentChanges.
  104. *
  105. * @param docChanges The set of changes to make to the view's docs.
  106. * @return A new FSTViewChange with the given docs, changes, and sync state.
  107. */
  108. - (FSTViewChange *)applyChangesToDocuments:(FSTViewDocumentChanges *)docChanges;
  109. /**
  110. * Updates the view with the given FSTViewDocumentChanges and updates limbo docs and sync state from
  111. * the given (optional) target change.
  112. *
  113. * @param docChanges The set of changes to make to the view's docs.
  114. * @param targetChange A target change to apply for computing limbo docs and sync state.
  115. * @return A new FSTViewChange with the given docs, changes, and sync state.
  116. */
  117. - (FSTViewChange *)
  118. applyChangesToDocuments:(FSTViewDocumentChanges *)docChanges
  119. targetChange:
  120. (const absl::optional<firebase::firestore::remote::TargetChange> &)targetChange;
  121. /**
  122. * Applies an OnlineState change to the view, potentially generating an FSTViewChange if the
  123. * view's syncState changes as a result.
  124. */
  125. - (FSTViewChange *)applyChangedOnlineState:(firebase::firestore::model::OnlineState)onlineState;
  126. /**
  127. * The set of remote documents that the server has told us belongs to the target associated with
  128. * this view.
  129. */
  130. - (const firebase::firestore::model::DocumentKeySet &)syncedDocuments;
  131. @end
  132. NS_ASSUME_NONNULL_END