FSTViewSnapshot.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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_set.h"
  18. using firebase::firestore::model::DocumentKeySet;
  19. @class FSTDocument;
  20. @class FSTQuery;
  21. @class FSTDocumentSet;
  22. @class FSTViewSnapshot;
  23. NS_ASSUME_NONNULL_BEGIN
  24. #pragma mark - FSTDocumentViewChange
  25. /**
  26. * The types of changes that can happen to a document with respect to a view.
  27. * NOTE: We sort document changes by their type, so the ordering of this enum is significant.
  28. */
  29. typedef NS_ENUM(NSInteger, FSTDocumentViewChangeType) {
  30. FSTDocumentViewChangeTypeRemoved = 0,
  31. FSTDocumentViewChangeTypeAdded,
  32. FSTDocumentViewChangeTypeModified,
  33. FSTDocumentViewChangeTypeMetadata,
  34. };
  35. /** A change to a single document's state within a view. */
  36. @interface FSTDocumentViewChange : NSObject
  37. - (id)init __attribute__((unavailable("Use a static constructor method.")));
  38. + (instancetype)changeWithDocument:(FSTDocument *)document type:(FSTDocumentViewChangeType)type;
  39. /** The type of change for the document. */
  40. @property(nonatomic, assign, readonly) FSTDocumentViewChangeType type;
  41. /** The document whose status changed. */
  42. @property(nonatomic, strong, readonly) FSTDocument *document;
  43. @end
  44. #pragma mark - FSTDocumentChangeSet
  45. /** The possibly states a document can be in w.r.t syncing from local storage to the backend. */
  46. typedef NS_ENUM(NSInteger, FSTSyncState) {
  47. FSTSyncStateNone = 0,
  48. FSTSyncStateLocal,
  49. FSTSyncStateSynced,
  50. };
  51. /** A set of changes to documents with respect to a view. This set is mutable. */
  52. @interface FSTDocumentViewChangeSet : NSObject
  53. /** Returns a new empty change set. */
  54. + (instancetype)changeSet;
  55. /** Takes a new change and applies it to the set. */
  56. - (void)addChange:(FSTDocumentViewChange *)change;
  57. /** Returns the set of all changes tracked in this set. */
  58. - (NSArray<FSTDocumentViewChange *> *)changes;
  59. @end
  60. #pragma mark - FSTViewSnapshot
  61. typedef void (^FSTViewSnapshotHandler)(FSTViewSnapshot *_Nullable snapshot,
  62. NSError *_Nullable error);
  63. /** A view snapshot is an immutable capture of the results of a query and the changes to them. */
  64. @interface FSTViewSnapshot : NSObject
  65. - (instancetype)initWithQuery:(FSTQuery *)query
  66. documents:(FSTDocumentSet *)documents
  67. oldDocuments:(FSTDocumentSet *)oldDocuments
  68. documentChanges:(NSArray<FSTDocumentViewChange *> *)documentChanges
  69. fromCache:(BOOL)fromCache
  70. mutatedKeys:(DocumentKeySet)mutatedKeys
  71. syncStateChanged:(BOOL)syncStateChanged
  72. excludesMetadataChanges:(BOOL)excludesMetadataChanges NS_DESIGNATED_INITIALIZER;
  73. - (instancetype)init NS_UNAVAILABLE;
  74. /** Returns a view snapshot as if all documents in the snapshot were added. */
  75. + (instancetype)snapshotForInitialDocuments:(FSTDocumentSet *)documents
  76. query:(FSTQuery *)query
  77. mutatedKeys:(DocumentKeySet)mutatedKeys
  78. fromCache:(BOOL)fromCache
  79. excludesMetadataChanges:(BOOL)excludesMetadataChanges;
  80. /** The query this view is tracking the results for. */
  81. @property(nonatomic, strong, readonly) FSTQuery *query;
  82. /** The documents currently known to be results of the query. */
  83. @property(nonatomic, strong, readonly) FSTDocumentSet *documents;
  84. /** The documents of the last snapshot. */
  85. @property(nonatomic, strong, readonly) FSTDocumentSet *oldDocuments;
  86. /** The set of changes that have been applied to the documents. */
  87. @property(nonatomic, strong, readonly) NSArray<FSTDocumentViewChange *> *documentChanges;
  88. /** Whether any document in the snapshot was served from the local cache. */
  89. @property(nonatomic, assign, readonly, getter=isFromCache) BOOL fromCache;
  90. /** Whether any document in the snapshot has pending local writes. */
  91. @property(nonatomic, assign, readonly) BOOL hasPendingWrites;
  92. /** Whether the sync state changed as part of this snapshot. */
  93. @property(nonatomic, assign, readonly) BOOL syncStateChanged;
  94. /** Whether this snapshot has been filtered to not include metadata changes */
  95. @property(nonatomic, assign, readonly) BOOL excludesMetadataChanges;
  96. /** The document in this snapshot that have unconfirmed writes. */
  97. @property(nonatomic, assign, readonly) DocumentKeySet mutatedKeys;
  98. @end
  99. NS_ASSUME_NONNULL_END