FSTView.h 5.7 KB

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