FSTView.h 5.9 KB

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