FSTViewSnapshot.h 4.0 KB

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