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. #import "Firestore/Source/Core/FSTTypes.h"
  18. #import "Firestore/Source/Model/FSTDocumentDictionary.h"
  19. #import "Firestore/Source/Model/FSTDocumentKeySet.h"
  20. #include "Firestore/core/src/firebase/firestore/model/document_key.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. /** 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. @property(nonatomic, strong, readonly) FSTDocumentKeySet *mutatedKeys;
  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:(FSTDocumentKeySet *)remoteDocuments 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:(FSTMaybeDocumentDictionary *)docChanges;
  82. /**
  83. * Iterates over a set of doc changes, applies the query limit, and computes what the new results
  84. * should be, what the changes were, and whether we may need to go back to the local cache for
  85. * more results. Does not make any changes to the view.
  86. *
  87. * @param docChanges The doc changes to apply to this view.
  88. * @param previousChanges If this is being called with a refill, then start with this set of docs
  89. * and changes instead of the current view.
  90. * @return a new set of docs, changes, and refill flag.
  91. */
  92. - (FSTViewDocumentChanges *)computeChangesWithDocuments:(FSTMaybeDocumentDictionary *)docChanges
  93. previousChanges:
  94. (nullable FSTViewDocumentChanges *)previousChanges;
  95. /**
  96. * Updates the view with the given ViewDocumentChanges.
  97. *
  98. * @param docChanges The set of changes to make to the view's docs.
  99. * @return A new FSTViewChange with the given docs, changes, and sync state.
  100. */
  101. - (FSTViewChange *)applyChangesToDocuments:(FSTViewDocumentChanges *)docChanges;
  102. /**
  103. * Updates the view with the given FSTViewDocumentChanges and updates limbo docs and sync state from
  104. * the given (optional) target change.
  105. *
  106. * @param docChanges The set of changes to make to the view's docs.
  107. * @param targetChange A target change to apply for computing limbo docs and sync state.
  108. * @return A new FSTViewChange with the given docs, changes, and sync state.
  109. */
  110. - (FSTViewChange *)applyChangesToDocuments:(FSTViewDocumentChanges *)docChanges
  111. targetChange:(nullable FSTTargetChange *)targetChange;
  112. /**
  113. * Applies an FSTOnlineState change to the view, potentially generating an FSTViewChange if the
  114. * view's syncState changes as a result.
  115. */
  116. - (FSTViewChange *)applyChangedOnlineState:(FSTOnlineState)onlineState;
  117. /**
  118. * The set of remote documents that the server has told us belongs to the target associated with
  119. * this view.
  120. */
  121. @property(nonatomic, strong, readonly) FSTDocumentKeySet *syncedDocuments;
  122. @end
  123. NS_ASSUME_NONNULL_END