FSTView.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #import "Firestore/Source/Core/FSTTypes.h"
  18. #import "Firestore/Source/Model/FSTDocumentDictionary.h"
  19. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  20. #include "Firestore/core/src/firebase/firestore/model/document_key_set.h"
  21. @class FSTDocumentSet;
  22. @class FSTDocumentViewChangeSet;
  23. @class FSTMaybeDocument;
  24. @class FSTQuery;
  25. @class FSTTargetChange;
  26. @class FSTViewSnapshot;
  27. NS_ASSUME_NONNULL_BEGIN
  28. #pragma mark - FSTViewDocumentChanges
  29. /** The result of applying a set of doc changes to a view. */
  30. @interface FSTViewDocumentChanges : NSObject
  31. - (instancetype)init NS_UNAVAILABLE;
  32. - (const firebase::firestore::model::DocumentKeySet &)mutatedKeys;
  33. /** The new set of docs that should be in the view. */
  34. @property(nonatomic, strong, readonly) FSTDocumentSet *documentSet;
  35. /** The diff of this these docs with the previous set of docs. */
  36. @property(nonatomic, strong, readonly) FSTDocumentViewChangeSet *changeSet;
  37. /**
  38. * Whether the set of documents passed in was not sufficient to calculate the new state of the view
  39. * and there needs to be another pass based on the local cache.
  40. */
  41. @property(nonatomic, assign, readonly) BOOL needsRefill;
  42. @end
  43. #pragma mark - FSTLimboDocumentChange
  44. typedef NS_ENUM(NSInteger, FSTLimboDocumentChangeType) {
  45. FSTLimboDocumentChangeTypeAdded = 0,
  46. FSTLimboDocumentChangeTypeRemoved,
  47. };
  48. // A change to a particular document wrt to whether it is in "limbo".
  49. @interface FSTLimboDocumentChange : NSObject
  50. + (instancetype)changeWithType:(FSTLimboDocumentChangeType)type
  51. key:(firebase::firestore::model::DocumentKey)key;
  52. - (id)init __attribute__((unavailable("Use a static constructor method.")));
  53. - (const firebase::firestore::model::DocumentKey &)key;
  54. @property(nonatomic, assign, readonly) FSTLimboDocumentChangeType type;
  55. @end
  56. #pragma mark - FSTViewChange
  57. // A set of changes to a view.
  58. @interface FSTViewChange : NSObject
  59. - (id)init __attribute__((unavailable("Use a static constructor method.")));
  60. @property(nonatomic, strong, readonly, nullable) FSTViewSnapshot *snapshot;
  61. @property(nonatomic, strong, readonly) NSArray<FSTLimboDocumentChange *> *limboChanges;
  62. @end
  63. #pragma mark - FSTView
  64. /**
  65. * View is responsible for computing the final merged truth of what docs are in a query. It gets
  66. * notified of local and remote changes to docs, and applies the query filters and limits to
  67. * determine the most correct possible results.
  68. */
  69. @interface FSTView : NSObject
  70. - (instancetype)init NS_UNAVAILABLE;
  71. - (instancetype)initWithQuery:(FSTQuery *)query
  72. remoteDocuments:(firebase::firestore::model::DocumentKeySet)remoteDocuments
  73. NS_DESIGNATED_INITIALIZER;
  74. /**
  75. * Iterates over a set of doc changes, applies the query limit, and computes what the new results
  76. * should be, what the changes were, and whether we may need to go back to the local cache for
  77. * more results. Does not make any changes to the view.
  78. *
  79. * @param docChanges The doc changes to apply to this view.
  80. * @return a new set of docs, changes, and refill flag.
  81. */
  82. - (FSTViewDocumentChanges *)computeChangesWithDocuments:(FSTMaybeDocumentDictionary *)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 *)computeChangesWithDocuments:(FSTMaybeDocumentDictionary *)docChanges
  94. previousChanges:
  95. (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 FSTOnlineState change to the view, potentially generating an FSTViewChange if the
  115. * view's syncState changes as a result.
  116. */
  117. - (FSTViewChange *)applyChangedOnlineState:(FSTOnlineState)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