FSTView.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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/Model/FSTDocumentDictionary.h"
  18. #import "Firestore/Source/Model/FSTDocumentKeySet.h"
  19. @class FSTDocumentKey;
  20. @class FSTDocumentSet;
  21. @class FSTDocumentViewChangeSet;
  22. @class FSTMaybeDocument;
  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. /** The new set of docs that should be in the view. */
  32. @property(nonatomic, strong, readonly) FSTDocumentSet *documentSet;
  33. /** The diff of this these docs with the previous set of docs. */
  34. @property(nonatomic, strong, readonly) FSTDocumentViewChangeSet *changeSet;
  35. /**
  36. * Whether the set of documents passed in was not sufficient to calculate the new state of the view
  37. * and there needs to be another pass based on the local cache.
  38. */
  39. @property(nonatomic, assign, readonly) BOOL needsRefill;
  40. @property(nonatomic, strong, readonly) FSTDocumentKeySet *mutatedKeys;
  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 key:(FSTDocumentKey *)key;
  50. - (id)init __attribute__((unavailable("Use a static constructor method.")));
  51. @property(nonatomic, assign, readonly) FSTLimboDocumentChangeType type;
  52. @property(nonatomic, strong, readonly) FSTDocumentKey *key;
  53. @end
  54. #pragma mark - FSTViewChange
  55. // A set of changes to a view.
  56. @interface FSTViewChange : NSObject
  57. - (id)init __attribute__((unavailable("Use a static constructor method.")));
  58. @property(nonatomic, strong, readonly, nullable) FSTViewSnapshot *snapshot;
  59. @property(nonatomic, strong, readonly) NSArray<FSTLimboDocumentChange *> *limboChanges;
  60. @end
  61. #pragma mark - FSTView
  62. /**
  63. * View is responsible for computing the final merged truth of what docs are in a query. It gets
  64. * notified of local and remote changes to docs, and applies the query filters and limits to
  65. * determine the most correct possible results.
  66. */
  67. @interface FSTView : NSObject
  68. - (instancetype)init NS_UNAVAILABLE;
  69. - (instancetype)initWithQuery:(FSTQuery *)query
  70. remoteDocuments:(FSTDocumentKeySet *)remoteDocuments NS_DESIGNATED_INITIALIZER;
  71. /**
  72. * Iterates over a set of doc changes, applies the query limit, and computes what the new results
  73. * should be, what the changes were, and whether we may need to go back to the local cache for
  74. * more results. Does not make any changes to the view.
  75. *
  76. * @param docChanges The doc changes to apply to this view.
  77. * @return a new set of docs, changes, and refill flag.
  78. */
  79. - (FSTViewDocumentChanges *)computeChangesWithDocuments:(FSTMaybeDocumentDictionary *)docChanges;
  80. /**
  81. * Iterates over a set of doc changes, applies the query limit, and computes what the new results
  82. * should be, what the changes were, and whether we may need to go back to the local cache for
  83. * more results. Does not make any changes to the view.
  84. *
  85. * @param docChanges The doc changes to apply to this view.
  86. * @param previousChanges If this is being called with a refill, then start with this set of docs
  87. * and changes instead of the current view.
  88. * @return a new set of docs, changes, and refill flag.
  89. */
  90. - (FSTViewDocumentChanges *)computeChangesWithDocuments:(FSTMaybeDocumentDictionary *)docChanges
  91. previousChanges:
  92. (nullable FSTViewDocumentChanges *)previousChanges;
  93. /**
  94. * Updates the view with the given ViewDocumentChanges.
  95. *
  96. * @param docChanges The set of changes to make to the view's docs.
  97. * @return A new FSTViewChange with the given docs, changes, and sync state.
  98. */
  99. - (FSTViewChange *)applyChangesToDocuments:(FSTViewDocumentChanges *)docChanges;
  100. /**
  101. * Updates the view with the given FSTViewDocumentChanges and updates limbo docs and sync state from
  102. * the given (optional) target change.
  103. *
  104. * @param docChanges The set of changes to make to the view's docs.
  105. * @param targetChange A target change to apply for computing limbo docs and sync state.
  106. * @return A new FSTViewChange with the given docs, changes, and sync state.
  107. */
  108. - (FSTViewChange *)applyChangesToDocuments:(FSTViewDocumentChanges *)docChanges
  109. targetChange:(nullable FSTTargetChange *)targetChange;
  110. @end
  111. NS_ASSUME_NONNULL_END