FSTView.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. @class FSTDocumentKey;
  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 key:(FSTDocumentKey *)key;
  51. - (id)init __attribute__((unavailable("Use a static constructor method.")));
  52. @property(nonatomic, assign, readonly) FSTLimboDocumentChangeType type;
  53. @property(nonatomic, strong, readonly) FSTDocumentKey *key;
  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:(FSTDocumentKeySet *)remoteDocuments NS_DESIGNATED_INITIALIZER;
  72. /**
  73. * Iterates over a set of doc changes, applies the query limit, and computes what the new results
  74. * should be, what the changes were, and whether we may need to go back to the local cache for
  75. * more results. Does not make any changes to the view.
  76. *
  77. * @param docChanges The doc changes to apply to this view.
  78. * @return a new set of docs, changes, and refill flag.
  79. */
  80. - (FSTViewDocumentChanges *)computeChangesWithDocuments:(FSTMaybeDocumentDictionary *)docChanges;
  81. /**
  82. * Iterates over a set of doc changes, applies the query limit, and computes what the new results
  83. * should be, what the changes were, and whether we may need to go back to the local cache for
  84. * more results. Does not make any changes to the view.
  85. *
  86. * @param docChanges The doc changes to apply to this view.
  87. * @param previousChanges If this is being called with a refill, then start with this set of docs
  88. * and changes instead of the current view.
  89. * @return a new set of docs, changes, and refill flag.
  90. */
  91. - (FSTViewDocumentChanges *)computeChangesWithDocuments:(FSTMaybeDocumentDictionary *)docChanges
  92. previousChanges:
  93. (nullable FSTViewDocumentChanges *)previousChanges;
  94. /**
  95. * Updates the view with the given ViewDocumentChanges.
  96. *
  97. * @param docChanges The set of changes to make to the view's docs.
  98. * @return A new FSTViewChange with the given docs, changes, and sync state.
  99. */
  100. - (FSTViewChange *)applyChangesToDocuments:(FSTViewDocumentChanges *)docChanges;
  101. /**
  102. * Updates the view with the given FSTViewDocumentChanges and updates limbo docs and sync state from
  103. * the given (optional) target change.
  104. *
  105. * @param docChanges The set of changes to make to the view's docs.
  106. * @param targetChange A target change to apply for computing limbo docs and sync state.
  107. * @return A new FSTViewChange with the given docs, changes, and sync state.
  108. */
  109. - (FSTViewChange *)applyChangesToDocuments:(FSTViewDocumentChanges *)docChanges
  110. targetChange:(nullable FSTTargetChange *)targetChange;
  111. /**
  112. * Applies an FSTOnlineState change to the view, potentially generating an FSTViewChange if the
  113. * view's syncState changes as a result.
  114. */
  115. - (FSTViewChange *)applyChangedOnlineState:(FSTOnlineState)onlineState;
  116. @end
  117. NS_ASSUME_NONNULL_END