FSTView.h 5.9 KB

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